我在我的活动的一个片段中创建了一个recyclerView。我将它绑定到数据集。我已经设置了LinearLayoutManager和RecyclerView Adapter。我的数据集也正确创建,我的Viewholder也是如此。我的Logcat中没有编译警告,也没有例外。我检查了片段和单个项目的XML布局,并且都将所有项目都设置为Visible。
然而我的recyclerView没有显示在片段中。
导致recyclerView不可见的常见疏忽是什么?感谢我能就这件事得到的任何指导。
初始化我的recylerView的相关方法如下:
private void loadMyPlacardView(View view){
List<Post> postList = new ArrayList<>();
RecyclerView placardsRecyclerView = view.findViewById(R.id.fp_placards_recyclerView);
LinearLayoutManager postsLayoutManager = new LinearLayoutManager(view.getContext());
placardsRecyclerView.setHasFixedSize(true);
placardsRecyclerView.setLayoutManager(postsLayoutManager);
DataSnapshot dataSnapshot = FirebaseStorageUtils.postDataSnapshot;
for (DataSnapshot singleSnapShot: dataSnapshot.getChildren()) {
Post thisPost = singleSnapShot.getValue(Post.class);
postList.add(thisPost);
}
PlacardsRecyclerViewAdapter placardsRecyclerViewAdapter = new PlacardsRecyclerViewAdapter(postList);
placardsRecyclerView.setAdapter(placardsRecyclerViewAdapter);
}
RecyclerviewAdapter的代码在这里
public class PlacardsRecyclerViewAdapter
extends RecyclerView.Adapter<PlacardHolder> {
private static final String TAG = "PlacardsViewAdapter";
private List<Post> listofPosts;
public PlacardsRecyclerViewAdapter(List <Post> list) {
listofPosts = list;
}
@Override
public PlacardHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.placard_item, parent, false);
return new PlacardHolder(view);
}
@Override
public void onBindViewHolder(PlacardHolder placard, int position) {
Post currentPost = listofPosts.get(position);
String username = currentPost.getUserName();
placard.userName.setText(username);
}
@Override
public int getItemCount() {
return listofPosts.size();
}
}
Recyclerviewholder的代码为PlacardHolder.java类,位于
之下public class PlacardHolder extends RecyclerView.ViewHolder {
private static final String TAG = "PlacardsHolder";
public TextView userName;
public PlacardHolder(View view){
super(view);
this.userName = view.findViewById(R.id.pi_profile_name);
}
}
Placard Item的XML是下面的placard_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/pi_profile_name"
android:text="@string/default_username"
android:textAlignment="center"
android:textSize="25sp"
android:textStyle="bold"
android:layout_weight="7"/></android.support.constraint.ConstraintLayout>
和fragment_placard.xml的代码是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="testing recyclerview"/>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fp_placards_recyclerView"
android:scrollbars="vertical"
android:visibility="visible"
android:layout_margin="10dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
fragment_placard.xml中的textview正在加载,但不是recyclelerview。
答案 0 :(得分:1)
好!它与 Firebase 有关。将数据添加到数据集后,您不会通知适配器。做
if(placardsRecyclerViewAdapter != null)
placardsRecyclerViewAdapter.notifyDataSetChanged();
将数据添加到postList
后。
已更新
完整结构:
private ValueEventListener postListener = null;
private void loadMyPlacardView(View view) {
List<Post> postList = new ArrayList<>();
RecyclerView placardsRecyclerView = view.findViewById(R.id.fp_placards_recyclerView);
LinearLayoutManager postsLayoutManager = new LinearLayoutManager(view.getContext());
placardsRecyclerView.setHasFixedSize(true);
placardsRecyclerView.setLayoutManager(postsLayoutManager);
PlacardsRecyclerViewAdapter placardsRecyclerViewAdapter = new PlacardsRecyclerViewAdapter(postList);
placardsRecyclerView.setAdapter(placardsRecyclerViewAdapter);
//Fetching data from Firebase
postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Post thisPost = ds.getValue(Post.class);
postList.add(thisPost);
}
if(placardsRecyclerViewAdapter != null)
placardsRecyclerViewAdapter.notifyDataSetChanged(); //Now postList has data so notify adapter.
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.e("DB error", "loadPost:onCancelled", databaseError.toException());
}
};
databaseReference.addListenerForSingleValueEvent(valueEventListener);
}
答案 1 :(得分:0)
我找到了问题的答案。有2个错误。第一个问题出现在placard_item.xml中,我在其中声明了Constraintview,如下所示:
android:layout_height ="match parent"
因此布局占据了屏幕的整个空间。第二个错误是我将TextView定义如下
android:layout_width = "0dp"
这导致TextView在运行时在recyclelerview中消失,尽管它在AndroidStudio IDE中的xml的“Design”视图中表现得非常好。我在这里发帖回答以防其他人面临同样的问题。在使用Recyclerview时,“ConstraintView”通常难以处理。所以回到更简单的LinearLayout。