Android 直到我在带有两个片段的viewpager中向上滚动时,Recyclerview才显示数据
我有一个带有ViewPager的活动,它有两个片段a和B 这两个碎片膨胀成相同的布局。也就是说,它们使用相同的xml布局 两个片段共享的xmlAndroid 直到我在带有两个片段的viewpager中向上滚动时,Recyclerview才显示数据,android,android-fragments,android-recyclerview,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:/
<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>
片段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时,片段B中的回收视图中的项目在我尝试向上滚动我的回收视图之前不会显示
我已经在片段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
}
}
公共类CustomAdapter扩展了RecyclerView.Adapter{
私有ArrayList属性;
私人语境;
公共CustomAdapter(上下文上下文、ArrayList属性){
这个。属性=属性;
this.context=上下文;
}
public int getItemViewType(int位置){
对象coreDetails=properties.get(位置);
if(coreDetails实例的coreDetails){
返回0;
}否则{
返回1;
}
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
如果(viewType==0){
返回新的RHolder(LayoutInflater.from(context).充气(R.layout.R_layout,parent,false));
}否则{
返回新的SHolder(LayoutInflater.from(context).充气(R.layout.s_layout,parent,false));
}
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
public int getItemCount(){
返回properties.size();
}
@凌驾
public void onBindViewHolder(final RecyclerView.ViewHolder,int位置){
最终对象=properties.get(位置);
//设置数据
}
}
有了这些信息,是什么让FragmentBRecyclerview上的项目在我向上滚动之前不会显示?我通过“刷新Recyclerview”解决了这个问题
然后,您可以将您的答案标记为问题的解决方案。:)@费利克斯。我忘了这么做:)
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.swapAdapter(myAdapter,false);
recyclerview.setLayoutManager(myLayoutManager);
myAdapter.notifyDataSetChanged();