I implemented BottomSheet
using the DialogFragment
approach. I have a TabLayout
and ViewPager
in the BottomSheet
You not need to extends StatisticFragment
as ViewPagerBottomSheetDialogFragment
or no need to use any Library for that.
It's you code i have just made some changes in your Static Fragment
related to View Pager.
Here is the Statistic Fragment
in which i have made changes.
There is not any bugs as stated in all above Answers.
Replace this code with your old Static fragment
only not any other changes it will give you the Desired output.
I have just made changes around with your View Pager
only and made it working as you want.
used OnPageChangeListener
method just get that view.
Statistic Fragment.java
public class StatisticFragment extends BottomSheetDialogFragment {
private BottomSheetBehavior mBehavior;
private TabLayout tabLayout;
private ViewPager viewPager;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
View rootView = View.inflate(getContext(), R.layout.sheet_main, null);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
if (viewPager != null && tabLayout != null) {
initViewPager();
}
final ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
View view = viewPager.findViewWithTag(arg0);
if (view == null) {
return;
}
CustomPagerAdapter adapter = new CustomPagerAdapter(getContext());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(10);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
viewPager.addOnPageChangeListener(pageChangeListener);
viewPager.post(new Runnable() {
@Override
public void run() {
pageChangeListener.onPageSelected(viewPager.getCurrentItem());
}
});
dialog.setContentView(rootView);
mBehavior = BottomSheetBehavior.from((View) rootView.getParent());
return dialog;
}
private void initViewPager() {
CustomPagerAdapter adapter = new CustomPagerAdapter(getContext());
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(10);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void onStart() {
super.onStart();
//mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
//mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
public class ServiceVideHolder extends RecyclerView.ViewHolder {
protected ViewGroup mItemView;
protected TextView mNameView;
protected TextView mCodeView;
public ServiceVideHolder(View v) {
super(v);
//rootView = v;
mItemView = (ViewGroup) v.findViewById(R.id.item);
mNameView = (TextView) v.findViewById(R.id.main_text);
mCodeView = (TextView) v.findViewById(R.id.sub_text);
}
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
protected TextView mMainText;
protected TextView mSubText;
public ItemViewHolder(View v) {
super(v);
mMainText = (TextView) v.findViewById(R.id.main_text);
mSubText = (TextView) v.findViewById(R.id.sub_text);
}
}
public class ItemAdapter extends RecyclerView.Adapter {
private List- items;
public ItemAdapter(List
- items) {
this.items = items;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
return new ItemViewHolder(view);
}
@Override
public void onBindViewHolder(final ItemViewHolder viewHolder, final int position) {
final Item item = items.get(position);
viewHolder.mMainText.setText(item.name);
viewHolder.mSubText.setText(item.value);
viewHolder.mMainText.setTextColor(ResourcesCompat.getColor(getResources(), position % 2 == 0 ? R.color.md_red_500 : R.color.md_blue_500, null));
}
@Override
public int getItemCount() {
return items.size();
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List
mFragmentList = new ArrayList<>();
private final List mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
//CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.adapter, collection, false);
rootView.setTag(position);
Toast.makeText(mContext, "Inside Instanciate Item", Toast.LENGTH_SHORT).show();
//View rootView = View.inflate(getContext(), R.layout.adapter, null);
final RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
ArrayList- items = new ArrayList<>();
if (position == 0) {
items.add(new Item("Coffee 1", "1"));
items.add(new Item("Coffee 2", "2"));
items.add(new Item("Coffee 3", "3"));
items.add(new Item("Coffee 4", "4"));
items.add(new Item("Coffee 5", "5"));
items.add(new Item("Coffee 6", "6"));
items.add(new Item("Coffee 7", "7"));
items.add(new Item("Coffee 8", "8"));
items.add(new Item("Coffee 9", "9"));
items.add(new Item("Coffee 10", "10"));
} else {
items.add(new Item("Milk 1", "1"));
items.add(new Item("Milk 2", "2"));
items.add(new Item("Milk 3", "3"));
items.add(new Item("Milk 4", "4"));
items.add(new Item("Milk 5", "5"));
items.add(new Item("Milk 6", "6"));
items.add(new Item("Milk 7", "7"));
items.add(new Item("Milk 8", "8"));
items.add(new Item("Milk 9", "9"));
items.add(new Item("Milk 10", "10"));
}
final ItemAdapter mAdapter = new ItemAdapter(items);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setAdapter(mAdapter);
Paint paint = new Paint();
paint.setStrokeWidth(1);
paint.setColor(ResourcesCompat.getColor(getResources(), R.color.md_grey_500, null));
paint.setAntiAlias(true);
paint.setPathEffect(new DashPathEffect(new float[]{25.0f, 25.0f}, 0));
mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).showLastDivider().paint(paint).build()); //.marginResId(R.dimen.leftmargin, R.dimen.rightmargin)
collection.addView(rootView);
return rootView;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
//CustomPagerEnum customPagerEnum = CustomPagerEnum.values()[position];
return position == 0 ? "Coffee" : "Milk";
}
}
}
It's Done.