Edijae Crusar
Edijae Crusar

Reputation: 3541

Recyclerview not showing data untill i scroll up in a viewpager with two fragments

I have an Activity with a ViewPager which has two Fragments A and B This two fragments inflate the same layout. that is they use the same xml layout.

xml shared by the two fragments

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@id/constraintLayout"
android:background="@color/md_white_1000"
android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/propRV"
        android:scrollbars="vertical"
        app:layout_constraintTop_toTopOf="@id/constraintLayout"
        app:layout_constraintLeft_toLeftOf="@id/constraintLayout"
        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"
        app:layout_constraintRight_toRightOf="@id/constraintLayout"
        >
    </android.support.v7.widget.RecyclerView>

  //other views
</android.support.constraint.ConstraintLayout>

fragment A

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_items, container, false);
}

fragment B

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_items, container, false);
}

When I open the activity that contains my two fragments, items in Fragment A shows up well but when I scroll to Fragment B, items in Recyclerview at Fragment B do not show up until i try to scroll up my Recyclerview.

I have checked the adapter for Recyclerview at fragment B and saw that onBindViewHolder() is not being called until I scroll up. below is the adapter

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

private ArrayList<Object> properties;
private Context context;

  public CustomAdapter(Context context,ArrayList<Object> properties){
  this.properties = properties;
  this.context = context;
  }

  public int getItemViewType(int position) {
    Object coreDetails = properties.get(position);
    if(coreDetails instanceof CoreDetails){
      return 0;
    }else{
      return 1;
    }
  }
  @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  if(viewType == 0){
  return new RHolder(LayoutInflater.from(context).inflate(R.layout.r_layout,parent,false));
  }else{
  return new SHolder(LayoutInflater.from(context).inflate(R.layout.s_layout,parent,false));
  }
  }

@Override
public long getItemId(int position) {
  return position;
  }

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

  @Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
  final Object object = properties.get(position);
   //setting data
  }

  }

With that info, what is making items at Fragment B Recyclerview not to show up until I scroll up?

Upvotes: 0

Views: 1231

Answers (1)

Edijae Crusar
Edijae Crusar

Reputation: 3541

I was able to solve the problem by "refreshing the recyclerview"

recyclerview.swapAdapter(myAdapter,false);
recyclerview.setLayoutManager(myLayoutManager);
myAdapter.notifyDataSetChanged();

Upvotes: 1

Related Questions