1

I am having trouble inflating a recycler view inside a viewPager. The below code is of my MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = findViewById(R.id.pager);
    tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);//setting tab over viewpager

    viewPager.setAdapter(new VeiwPagerAdapter());
}

And the following is my ViewPagerAdapter:

public class VeiwPagerAdapter extends PagerAdapter {
private RecyclerView recyclerView;
private List<Pojo> pojoList = new ArrayList<>();

@Override
public int getCount() {
    return 2;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return false;
}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    if (position == 0) {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        View view = inflater.inflate(R.layout.recycler_view_holder, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
        pojoList.add(new Pojo("Random field 1", "Random field 1"));
        recyclerView.setAdapter(new RecycAdapter(pojoList));
        container.addView(view);
        return view;
    } else {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        View view = inflater.inflate(R.layout.recycler_view_holder, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(container.getContext()));
        pojoList.add(new Pojo("Random field 1", "Random field 1"));
        recyclerView.setAdapter(new RecycAdapter(pojoList));
        container.addView(view);
        return view;
    }
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return position == 0 ? "TAB 1" : "TAB 2";
}

RecycAdapter code:

public class RecycAdapter extends RecyclerView.Adapter<RecycViewHolder> {
    private List<Pojo> pojoArrayList;

    public RecycAdapter(List<Pojo> pojoArrayList) {
        this.pojoArrayList = pojoArrayList;
    }

    @NonNull
    @Override
    public RecycViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        View v = inflater.inflate(R.layout.item_layout, parent, false);

        return new RecycViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecycViewHolder holder, int position) {
        Pojo pojo = pojoArrayList.get(position);
        holder.textView1.setText(pojo.getText1());
        holder.textView2.setText(pojo.getText2());
    }

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

ViewHolder code added as well.Not sure if there is something wrong with the viewHolder implementation.

public class RecycViewHolder extends RecyclerView.ViewHolder {

    public TextView textView1, textView2;

    public RecycViewHolder(View itemView) {
        super(itemView);
        this.textView1 = itemView.findViewById(R.id.textvie1);
        this.textView2 = itemView.findViewById(R.id.textvie2);
    }
}

The output has nothing under each tab. Can someone please help me as to where i went wrong

Codershree
  • 77
  • 9

3 Answers3

1

Why do you have the block of code on your MainActivity?

recyclerView = view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    pojoList.add(new Pojo("Random field 1", "Random field 1"));
    recyclerView.setAdapter(new RecycAdapter(pojoList));
}

I can see some conflicts with the same R.id.recyclerView.

If you want a ViewPager to display two different RecyclerViews on each tab, then your MainActivity should only really have the containers for your views. Can you please give some more context on what you need to achieve?

Have you tried to show a simple and different TextView/Layout on each tab? You might have some XML issues.

Joaquim Ley
  • 4,038
  • 2
  • 24
  • 42
  • Yes you are correct,sorry for the confusion there, I have removed that piece of code from MainActivity(I was trying something else and added it in two places as an attempt).The only place where the recyclerView has been inflated now is inside the ViewPagerAdapter.But i am unable to even inflate a layout inside the viewpager.Below is the code: LayoutInflater layoutInflater=LayoutInflater.from(container.getContext()); View view = layoutInflater.inflate(R.layout.item_layout, container, false); container.addView(view); return view; – Codershree May 25 '18 at 15:00
  • Ok, so your bug is in the XML. You probably should read some tutorials how to setup, it might be easier then you think, also add any relevant code to the question :) – Joaquim Ley May 25 '18 at 20:23
0

You need to inflate the RecyclerView into each page of the ViewPager, in other words put this code in the "instantiateItem" method:

recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
pojoList.add(new Pojo("Random field 1", "Random field 1"));
recyclerView.setAdapter(new RecycAdapter(pojoList));
Fran Dioniz
  • 1,134
  • 9
  • 12
0

Finally,found a solution,the overriden method isViewFromObject was earlier returning false which was causing the issue upon changing to the code as below everything worked fine.

@Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
Codershree
  • 77
  • 9