Khalid Ali
Khalid Ali

Reputation: 373

ViewPager inside RecycleView Adapter

I tried to add Viewpager inside RecycleView item, then I created the adapter for the recycle view and I created a Pager Adapter that extends from FragmentStatePagerAdapter class inside the the adapter.

Just the first item of the recycle works fine, but the rest items of recycle view do not show the view pager.

When I Log inside the Pager Adapter I can see all the fragment position.

When I scroll horizontally inside any pager of any recycle view item I can see the Log statement, but nothing appear on the screen.

Could any one help me in that case ? Here is my code

public class RentCategoryItemAdapter extends RecyclerView.Adapter<RentCategoryItemAdapter.ViewHolder> {

    Context mContext;
    ArrayList<RentCategoryItem> categoryItems;
    ArrayList<MyPagerAdapter> pagerAdapters = new ArrayList<>();
    /*ArrayList<ViewPager> pagerArrayList = new ArrayList<>();
    ArrayList<MyPagerAdapter> myPagerAdapterArrayList = new ArrayList<>();*/
    public RentCategoryItemAdapter(Context context, ArrayList<RentCategoryItem> categoryItems) {
        mContext = context;
        this.categoryItems = categoryItems;
        AppCompatActivity activity = (AppCompatActivity) mContext;
        for (int i = 0; i < categoryItems.size(); i++) {
            pagerAdapters.add(new MyPagerAdapter(activity.getSupportFragmentManager(), categoryItems.get(i).catItems));
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemLayoutView;

        itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rent_category_item,parent, false);

        RentCategoryItemAdapter.ViewHolder viewHolder = new RentCategoryItemAdapter.ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.catName.setText(categoryItems.get(position).catName);
        //AppCompatActivity activity = (AppCompatActivity) mContext;
        //holder.mPagerAdapter = new MyPagerAdapter(activity.getSupportFragmentManager(), categoryItems.get(position).catItems);
        holder.pager.setAdapter(pagerAdapters.get(position));
        Log.d("Tag", pagerAdapters.get(position).getCount() + " position " + position);
        /*holder.mPagerAdapter = myPagerAdapterArrayList.get(position);
        holder.pager.setAdapter(myPagerAdapterArrayList.get(position));*/
        int margin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10*16, mContext.getResources().getDisplayMetrics());
        holder.pager.setPageMargin(-margin);
    }



    @Override
    public int getItemCount() {
        return categoryItems.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private PagerAdapter mPagerAdapter;
        TextView catName;
        ViewPager pager;
        public ViewHolder(View itemView) {
            super(itemView);
            pager = itemView.findViewById(R.id.pager);
            catName = itemView.findViewById(R.id.cat_name);
            /*pagerArrayList.add(new ViewPager(mContext));
            AppCompatActivity activity = (AppCompatActivity) mContext;
            myPagerAdapterArrayList.add(new MyPagerAdapter(activity.getSupportFragmentManager()));*/
        }
    }


    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        ArrayList<RentItem> items;
        public MyPagerAdapter(FragmentManager fm, ArrayList<RentItem> items) {
            super(fm);
            this.items = items;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new RentCategoryForPagerFragment();
            Bundle bundle = new Bundle();
            bundle.putString("first_last", "first");
            bundle.putString("product_id", items.get(position).productId);
            bundle.putString("product_name", items.get(position).productName);
            bundle.putString("product_price", items.get(position).productPrice);
            bundle.putString("product_image", items.get(position).productImage);
            fragment.setArguments(bundle);
            Log.d("Tag", "inside my pager adapter " + position);
            return fragment;
        }

        @Override
        public int getCount() {
            return items.size();
        }
    }
}

And here is RentCategoryForPagerFragment class

public class RentCategoryForPagerFragment extends Fragment {

    String firstOrLast = "";
    ImageView productImage;
    TextView productName, productPrice;
    String productId;
    Button showDetailsButton;
    public RentCategoryForPagerFragment() {
        //firstOrLast = getArguments().getString("first_last");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.rent_item_in_category_layout, container, false);
        productImage = rootView.findViewById(R.id.product_image);
        productName = rootView.findViewById(R.id.product_name);
        productPrice = rootView.findViewById(R.id.product_price);
        showDetailsButton = rootView.findViewById(R.id.show_details_button);
        Bundle bundle = getArguments();
        String imageURL = bundle.getString("product_image");
        Glide.with(getActivity()).load(imageURL).into(productImage);
        productId = bundle.getString("product_id");
        productName.setText(bundle.getString("product_name"));
        productPrice.setText(bundle.getString("product_price"));
        showDetailsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Clicked ", Toast.LENGTH_LONG).show();
            }
        });
        return rootView;
    }
  }

The result I got in this image

Upvotes: 1

Views: 1008

Answers (1)

Khalid Ali
Khalid Ali

Reputation: 373

Finally I found the solution. I was using the wrong class because it sound that FragmentStatePagerAdapter and FragmentPagerAdapter Classes do not serve my idea. So You should extend from PagerAdapter instead to implement more than one pager in the same screen. And you should override some methods to complete it.

I hope this answer helps someone in someday.

Upvotes: 1

Related Questions