FirebaseUI RecycleView 只显示第一项
Posted
技术标签:
【中文标题】FirebaseUI RecycleView 只显示第一项【英文标题】:FirebaseUI RecycleView only displays first item 【发布时间】:2021-02-13 10:05:15 【问题描述】:我更新到 firebase-ui 6.3.0,它不再具有 populateViewHolder 方法。我根据我在网上找到的示例更改了代码。在我将 adapter.startListening() 添加到我的 home Activity 的 onStart 方法之前,RecycleView 没有显示任何内容。
问题是只显示 1 个项目,无法弄清楚原因。由于我使用 linearLayoutManager.setStackFromEnd(true) 显示的项目是最后一个项目,但是如果我注释掉该行,则显示第一个项目(并且上下文手册也不起作用)。
我检查了一些类似的 *** 问题,但找不到问题。
Firebase RecyclerView OnDataChanged only last element is Shown
Recycler View showing only very last item added to realtime database
这是我的类和 xml:
家庭活动:
private RecyclerView transactionsRecyclerView;
private TransactionRecordAdapter transactionRecordAdapter;
private FirebaseHelper firebaseHelper;
private Query query;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
firebaseHelper = new FirebaseHelper();
query = this.firebaseHelper.getTransactionRecordsOrderedByDateQuery();
// FirebaseDatabase.getInstance().getReference().child(ConstantStringValue.DB_CHILD_TRANSACTION_RECORDS).child(auth.getCurrentUser().getUid())
// .orderByChild("date");
transactionsRecyclerView = findViewById(R.id.transactionsRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
transactionsRecyclerView.setLayoutManager(linearLayoutManager);
DividerItemDecoration itemDecoration = new DividerItemDecoration(this, VERTICAL);
transactionsRecyclerView.addItemDecoration(itemDecoration);
registerForContextMenu(transactionsRecyclerView);
@Override
protected void onStart()
super.onStart();
transactionRecordAdapter = new TransactionRecordAdapter(TransactionRecord.class, query, this);
transactionsRecyclerView.setAdapter(transactionRecordAdapter);
transactionRecordAdapter.startListening();
@Override
protected void onStop()
super.onStop();
transactionRecordAdapter.stopListening();
事务记录适配器:
private int position;
private String selectedItemId;
private Context context;
public TransactionRecordAdapter(Class<TransactionRecord> modelClass, Query ref, Context context)
super(new FirebaseRecyclerOptions.Builder<TransactionRecord>()
.setQuery(ref, modelClass)
.build());
this.context = context;
public int getPosition()
return position;
public void setPosition(int position)
this.position = position;
public String getSelectedItemId()
return selectedItemId;
public void setSelectedItemId(String selectedItemId)
this.selectedItemId = selectedItemId;
@Override
protected void onBindViewHolder(@NonNull final TransactionRecordViewHolder holder, int position,
@NonNull TransactionRecord model)
holder.itemView.setOnLongClickListener(v ->
setPosition(holder.getAdapterPosition());
setSelectedItemId(holder.getId());
return false;
);
holder.setAnnouncement(model.getAnnouncement());
...
holder.setId(this.getRef(position).getKey());
@NonNull
@Override
public TransactionRecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.transrecord, parent, false);
return new TransactionRecordViewHolder(view);
@Override
public void onViewRecycled(TransactionRecordViewHolder holder)
holder.itemView.setOnLongClickListener(null);
super.onViewRecycled(holder);
TransactionRecordViewHolder:
private View view;
private String id;
public TransactionRecordViewHolder(View itemView)
super(itemView);
view = itemView;
view.setOnCreateContextMenuListener(this);
// ... setters
public String getId()
return id;
public void setId(String id)
this.id = id;
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo)
contextMenu.add(Menu.NONE, R.id.editTransHomeContext,
Menu.NONE, "Szerkesztés");
contextMenu.add(Menu.NONE, R.id.deleteTransHomeContext,
Menu.NONE, "Törlés");
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="hu.beczdev.cashbudget.activity.HomeActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_
android:layout_
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_
android:layout_
android:id="@+id/transactionsRecyclerView" />
</LinearLayout>
transrecord.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_ >
<TextView
android:id="@+id/transAmountTextView"
android:layout_
android:layout_
android:layout_alignParentStart="true"
android:paddingStart="25dp"
android:paddingTop="10dp"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/transDateTextView"
android:layout_
android:layout_
android:layout_alignParentEnd="true"
android:paddingTop="10dp"
android:paddingEnd="25dp"
android:textSize="14sp" />
<LinearLayout
android:layout_
android:layout_>
<TextView
android:layout_
android:layout_
android:textSize="18sp"
android:paddingStart="25dp"
android:textStyle="bold"
android:id="@+id/transCatNameTextView"/>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_>
<TextView
android:layout_
android:layout_
android:paddingBottom="10dp"
android:textSize="15sp"
android:paddingStart="25dp"
android:id="@+id/transCommentView"/>
</LinearLayout>
</LinearLayout>
【问题讨论】:
【参考方案1】:我认为你的问题不在firebase中,因为它已经返回了一行,你至少需要wrap_content
列表项的高度才能不占据RecyclerView
的整个高度。
所以在 transrecord.xml 中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_ >
【讨论】:
这是一个很好的建议。我改变了高度,现在每个元素都是可见的。谢谢!以上是关于FirebaseUI RecycleView 只显示第一项的主要内容,如果未能解决你的问题,请参考以下文章
将 FirebaseUI 与 AngularFire2 一起使用