RecyclerView 在我第一次进入活动时没有填充,但在我退出并重新进入活动后显示 [英] RecyclerView not populating the first time I enter the activity but showing after i exit and re-enter the activity

查看:21
本文介绍了RecyclerView 在我第一次进入活动时没有填充,但在我退出并重新进入活动后显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所描述的,我需要第一次进入活动到一个空白屏幕.然后我需要通过按回退出活动并再次重新进入活动才能看到回收者视图.幸运的是,当回收器视图填充时,一切都井然有序,它准确地显示了我希望它在屏幕上的显示方式.

As title describes, i need to enter the activity the first time to a blank screen. I then need to exit the activity by pressing back and re-enter the activity again to be able to see the recycler view. Fortunately, when the recycler view populates, everything is in order and it shows exactly how I want it to be shown on screen.

这是我的活动课:

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

    mOwnTripList = (RecyclerView) findViewById(R.id.host_trip_list);
    mOwnTripList.setHasFixedSize(true);
    mOwnTripList.setLayoutManager(new LinearLayoutManager(this));

 mPostIdRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            comPostArrayList.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                PostId postId = snapshot.getValue(PostId.class);
                    tripUniqueId = postId.getPostId();
                Log.d("$$$$$$$$$$$$$" , tripUniqueId);



        mLastRef = FirebaseDatabase.getInstance().getReference().child("comPostsCopy")
                   .child(tripUniqueId);

        mLastRef.addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
         public void onDataChange(DataSnapshot dataSnapshot) {


         OwnHostTripItem ownHostTripPost = dataSnapshot
                                         .getValue(OwnHostTripItem.class);
              comPostArrayList.add(ownHostTripPost);
            Log.d("%%%%%",comPostArrayList.get(0).getTitle());

        }

         @Override
         public void onCancelled(DatabaseError databaseError) {

            }
         });

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

     adapter = new YourHostAdapter( comPostArrayList , this.getApplicationContext());

    adapter.notifyDataSetChanged();

    mOwnTripList.setAdapter(adapter);

} 
  }

这是我的模型类:

  public class OwnHostTripItem {

String thumbPhoto;
String title;
String country;
String pricePerGuest;
String maxGroupSize;

public OwnHostTripItem() {

}

public OwnHostTripItem(String thumbPhoto, String title, String country, String pricePerGuest
                        , String maxGroupSize) {
    this.thumbPhoto = thumbPhoto;
    this.title = title;
    this.country = country;
    this.pricePerGuest = pricePerGuest;
    this.maxGroupSize = maxGroupSize;
}



public String getThumbPhoto() {
    return thumbPhoto;
}

public void setThumbPhoto(String thumbPhoto) {
    this.thumbPhoto = thumbPhoto;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getPricePerGuest() {
    return pricePerGuest;
}

public void setPricePerGuest(String pricePerGuest) {
    this.pricePerGuest = pricePerGuest;
}

public String getMaxGroupSize() {
    return maxGroupSize;
}

public void setMaxGroupSize(String maxGroupSize) {
    this.maxGroupSize = maxGroupSize;
}

}

这是我的适配器:

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

private ArrayList<OwnHostTripItem> ownHostTripList;
private Context context;

public YourHostAdapter(ArrayList<OwnHostTripItem> ownHostTripList, Context context) {
    this.ownHostTripList = ownHostTripList;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
             .inflate(R.layout.host_trip_item, parent , false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    OwnHostTripItem ownHostTripListy = ownHostTripList.get(position);

    holder.mTripTitle.setText(ownHostTripListy.getTitle());
    holder.mTripCountry.setText(ownHostTripListy.getCountry());
    holder.mTripPrice.setText(ownHostTripListy.getPricePerGuest());
    holder.mTripSize.setText(ownHostTripListy.getMaxGroupSize());

    //For Image view
    Picasso.with(context).load(ownHostTripListy.getThumbPhoto())
    .placeholder(R.drawable.placeholder_image).into(holder.mTripImage)


}

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

public class ViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public TextView mTripTitle;
    public TextView mTripCountry;
    public TextView mTripPrice;
    public TextView mTripSize;
    public ImageView mTripImage;

    public ViewHolder(View itemView) {
        super(itemView);

        mView = itemView;

        mTripTitle = (TextView) mView.findViewById(R.id.host_trip_title);
        mTripCountry = (TextView) mView.findViewById(R.id.host_trip_country);
        mTripPrice = (TextView) mView.findViewById(R.id.host_trip_price);
        mTripSize = (TextView) mView.findViewById(R.id.host_trip_size);

        mTripImage = (ImageView) mView.findViewById(R.id.host_trip_image);

          }
        }
       }

这是我的 xml,其中包含回收器视图:

This is my xml with recycler view in it:

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <include layout="@layout/app_bar_layout"
        android:id="@+id/hosting_trip_bar"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Oops, looks like you haven't been hosting any trips"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:id="@+id/hosting_trip_text"
        />


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/host_trip_list"
        android:layout_below="@id/hosting_trip_bar"
        android:layout_marginTop="10dp" />


      </RelativeLayout>

我尝试在 onStart 方法中设置适配器,并且我看到其他帖子指出我们需要在 xml 中设置回收器视图以匹配父级,但它给了我相同的结果.如您所见,我没有将 RecyclerView 嵌套在许多视图中.可能是我在错误的时间设置了适配器,所以它给了我一个空列表?

I've tried setting adapter in onStart method and I've seen other post stating that we need to set the recycler view in xml to match parent but it gives me same result. As you can see, I've not nested my RecyclerView within many views. Could it be that I am setting the adapter at the wrong time so it gives me an empty list?

推荐答案

搜索其他帖子后,这个 来自 Ralph Bergman 和 Oussema Aroua 的综合答案链接解决了我的问题.我需要在我的活动中做的是在调用 addListenerForSingleValueEvent 方法之前创建我的适配器.之后,我需要在 for 循环结束后设置 notifyDataSetChange 和 setAdapter .因此,现在我的活动应该是这样的:

After searching for other posts, this link of combined answers from Ralph Bergman and Oussema Aroua solved my problem. What I needed to do in my activity is to create my adapter before I call the addListenerForSingleValueEvent method. After that, I need to set notifyDataSetChange and setAdapter after the for loop ends. Therefore, now my activity should look like this :

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

    mOwnTripList = (RecyclerView) findViewById(R.id.host_trip_list);
    mOwnTripList.setHasFixedSize(true);
    mOwnTripList.setLayoutManager(new LinearLayoutManager(this));

 mPostIdRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            comPostArrayList.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                PostId postId = snapshot.getValue(PostId.class);
                    tripUniqueId = postId.getPostId();
                Log.d("$$$$$$$$$$$$$" , tripUniqueId);


    adapter = new YourHostAdapter( 
             comPostArrayList,this.getApplicationContext());

        mLastRef = FirebaseDatabase.getInstance().getReference().child("comPostsCopy")
                   .child(tripUniqueId);

        mLastRef.addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
         public void onDataChange(DataSnapshot dataSnapshot) {


         OwnHostTripItem ownHostTripPost = dataSnapshot
                                         .getValue(OwnHostTripItem.class);
              comPostArrayList.add(ownHostTripPost);
            Log.d("%%%%%",comPostArrayList.get(0).getTitle());

        }

         @Override
         public void onCancelled(DatabaseError databaseError) {

            }
         });

            }
         adapter.notifyDataSetChanged();

         mOwnTripList.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });





} 
  }

这篇关于RecyclerView 在我第一次进入活动时没有填充,但在我退出并重新进入活动后显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆