Skip to content

Commit

Permalink
viewpager2 Poc with issues
Browse files Browse the repository at this point in the history
  • Loading branch information
devanshhooda committed Oct 17, 2024
1 parent 68d2504 commit f1ee84d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 57 deletions.
16 changes: 2 additions & 14 deletions app/res/layout/tabbed_detail_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,10 @@
this ID shouldn't be considered static for layouts, etc. DO NOT REFER
to the tabbed_detail_pager ID in any xml, and only within the pager
class in android until its ID changes. -->
<com.duolingo.open.rtlviewpager.RtlViewPager
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/tabbed_detail_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.viewpager.widget.PagerTabStrip
android:id="@+id/pager_tab_strip"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/content_min_margin"
android:paddingBottom="@dimen/content_min_margin"
android:textColor="@color/cc_brand_text"
android:background="@color/cc_brand_bg"
/>
</com.duolingo.open.rtlviewpager.RtlViewPager>
android:layout_height="wrap_content"/>

</LinearLayout>

Expand Down
27 changes: 13 additions & 14 deletions app/src/org/commcare/adapters/EntityDetailPagerAdapter.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package org.commcare.adapters;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import org.commcare.CommCareApplication;
import org.commcare.cases.entity.EntityUtil;
import org.commcare.dalvik.R;
import org.commcare.fragments.EntityDetailFragment;
import org.commcare.fragments.EntitySubnodeDetailFragment;
import org.commcare.suite.model.Detail;
import org.commcare.utils.SerializationUtil;
import org.javarosa.core.model.instance.TreeReference;

import java.util.Objects;

/**
* Subclass of FragmentStatePagerAdapter for populating a ViewPager (swipe-based paging widget) with entity detail fields.
*
* @author jschweers
*/
public class EntityDetailPagerAdapter extends FragmentStatePagerAdapter {
public class EntityDetailPagerAdapter extends FragmentStateAdapter {

private ListItemViewModifier modifier;
private final Detail detail;
Expand All @@ -28,7 +33,7 @@ public class EntityDetailPagerAdapter extends FragmentStatePagerAdapter {

public EntityDetailPagerAdapter(FragmentManager fm, Detail detail, int detailIndex,
TreeReference reference, ListItemViewModifier modifier) {
super(fm);
super(Objects.requireNonNull(fm.findFragmentById(R.id.entity_detail_tabs)));
this.detail = detail;
this.detailIndex = detailIndex;
this.mEntityReference = reference;
Expand All @@ -38,10 +43,11 @@ public EntityDetailPagerAdapter(FragmentManager fm, Detail detail, int detailInd
CommCareApplication.instance().getCurrentSessionWrapper().getEvaluationContext()));
}

@NonNull
@Override
public Fragment getItem(int i) {
public Fragment createFragment(int position) {
EntityDetailFragment fragment;
if (detail.getNodeset() != null || (detail.isCompound() && displayableChildDetails[i].getNodeset() != null)) {
if (detail.getNodeset() != null || (detail.isCompound() && displayableChildDetails[position].getNodeset() != null)) {
fragment = new EntitySubnodeDetailFragment();
} else {
fragment = new EntityDetailFragment();
Expand All @@ -50,7 +56,7 @@ public Fragment getItem(int i) {
Bundle args = new Bundle();
args.putString(EntityDetailFragment.DETAIL_ID, detail.getId());
if (detail.isCompound()) {
args.putInt(EntityDetailFragment.CHILD_DETAIL_INDEX, i);
args.putInt(EntityDetailFragment.CHILD_DETAIL_INDEX, position);
}
args.putInt(EntityDetailFragment.DETAIL_INDEX, detailIndex);
SerializationUtil.serializeToBundle(args, EntityDetailFragment.CHILD_REFERENCE, mEntityReference);
Expand All @@ -59,14 +65,7 @@ public Fragment getItem(int i) {
}

@Override
public CharSequence getPageTitle(int position) {
Detail detailShowing = detail.isCompound() ? displayableChildDetails[position] : detail;
return detailShowing.getTitle().getText().evaluate();
}

@Override
public int getCount() {
public int getItemCount() {
return detail.isCompound() ? displayableChildDetails.length : 1;
}

}
34 changes: 5 additions & 29 deletions app/src/org/commcare/views/TabbedDetailView.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.javarosa.core.model.instance.TreeReference;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import java.util.Objects;

/**
* Widget that combines a ViewPager with a set of page titles styled to look like tabs.
Expand All @@ -30,9 +32,7 @@ public class TabbedDetailView extends RelativeLayout {
private AppCompatActivity mContext;

private LinearLayout mMenu;
private ViewPager mViewPager;

private View mViewPageTabStrip;
private ViewPager2 mViewPager;

private int mEvenColor;
private int mOddColor;
Expand Down Expand Up @@ -75,25 +75,6 @@ public void setRoot(ViewGroup root) {
mMenu = root.findViewById(R.id.tabbed_detail_menu);
mViewPager = root.findViewById(R.id.tabbed_detail_pager);
mViewPager.setId(AndroidUtil.generateViewId());

mViewPageTabStrip = root.findViewById(R.id.pager_tab_strip);

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
markSelectedTab(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}

});
}

public void showMenu() {
Expand All @@ -108,11 +89,6 @@ public void refresh(Detail detail, TreeReference reference, int index) {
new EntityDetailPagerAdapter(mContext.getSupportFragmentManager(), detail, index, reference,
new ListItemViewStriper(this.mOddColor, this.mEvenColor));
mViewPager.setAdapter(entityDetailPagerAdapter);
if (!detail.isCompound()) {
if (mViewPageTabStrip != null) {
mViewPageTabStrip.setVisibility(GONE);
}
}
}

/**
Expand All @@ -134,6 +110,6 @@ public int getCurrentTab() {
}

public int getTabCount() {
return mViewPager.getAdapter().getCount();
return Objects.requireNonNull(mViewPager.getAdapter()).getItemCount();
}
}

0 comments on commit f1ee84d

Please sign in to comment.