2017-02-16 18 views
0

我有一個活動與ViewPager具有兩個片段 這兩個片段膨脹相同的佈局。那就是他們使用相同的xml佈局。Recyclerview未示出數據,直到我在viewpager滾動了兩個片段

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

片段乙

@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); 
} 

當我打開包含在片段我的兩個片段,項目一下都很但是當我滾動到碎片,在Recyclerview項目在片段沒有顯示出來,直到我試圖向上滾動的活動我Recyclerview。

我已經檢查了Recyclelerview的適配器,在B處看到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 
    } 

    } 

有了這些信息,什麼是在片段 Recyclerview使得項目沒有露面,直到我滾動起來?

回答

0

我能夠通過「刷新recyclerview」

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

然後你可以將其標記你的答案作爲一個解決問題的方法來解決這個問題。 :) –

+1

@FelixD。我忘了這麼做:) –

相關問題