Recyclerview没有显示数据,直到我在一个带有两个片段的viewpager中向上滚动

时间:2017-02-16 18:05:21

标签: android android-fragments android-recyclerview

我有一个ViewPager活动,其中有两个片段 A B 这两个片段膨胀相同的布局。那就是他们使用相同的xml布局。

xml由两个片段共享

<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>

片段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);
}

片段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);
}

当我打开包含我的两个片段的活动时,片段 A 中的项目会很好地显示,但当我滚动到片段 B 时,片段中的Recyclerview中的项目 B 在我尝试向上滚动Recyclerview之前不会显示。

我已经在片段 B 检查了适配器的Recyclerview,并且在我向上滚动之前看到onBindViewHolder()没有被调用。下面是适配器

    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
  }

  }

有了这些信息,片段 B Recyclerview的项目是什么才能在我向上滚动之前显示?

1 个答案:

答案 0 :(得分:0)

我能够通过“刷新recyclerview”来解决问题

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