如标题所述,我需要在第一次进入活动时进入空白屏幕。然后我需要通过按回来退出活动并再次重新进入活动以便能够看到回收者视图。幸运的是,当回收者视图填充时,一切都按顺序显示,它显示我希望它在屏幕上显示的方式。
这是我的活动课程:
@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);
}
}
}
这是我的带有Recycler视图的xml:
<?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中设置recycleler视图以匹配parent,但它给了我相同的结果。如您所见,我没有在许多视图中嵌套我的RecyclerView。可能是我在错误的时间设置适配器,所以它给我一个空列表?
答案 0 :(得分:0)
“hosting_trip.xml”文件中的recyclerview存在问题。您应该将recyclerview的高度更改为match_parent(android:layout_height =“match_parent”)。
由于您尚未上传hosting_trip.xml文件。 您可以尝试让我知道解决方案。
答案 1 :(得分:0)
在搜索其他帖子后,来自Ralph Bergman和Oussema Aroua的综合答案的this链接解决了我的问题。我在活动中需要做的是在调用addListenerForSingleValueEvent方法之前创建我的适配器。之后,我需要在for循环结束后设置notifyDataSetChange和setAdapter。 因此,现在我的活动应该是这样的:
@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) {
}
});
}
}