Android 使用Firebase UI RecyclerView打开另一个活动
我看了几个关于这个话题的答案,但没有一个能达到我想要的结果。我有一个使用Firebase UI for RecyclerView的RecyclerView,我想在其中添加一个OnClick事件。因此,用户单击RecyclerView中的一个项目并进入另一个活动。为此,我在adapter类中使用了一个intent,但不知何故它不起作用,如果我单击RecyclerView的某个项,则它应该会打开下一个活动,但它不会。是否有其他方式开展另一项活动 以下是我的适配器类:Android 使用Firebase UI RecyclerView打开另一个活动,android,android-recyclerview,firebaseui,Android,Android Recyclerview,Firebaseui,我看了几个关于这个话题的答案,但没有一个能达到我想要的结果。我有一个使用Firebase UI for RecyclerView的RecyclerView,我想在其中添加一个OnClick事件。因此,用户单击RecyclerView中的一个项目并进入另一个活动。为此,我在adapter类中使用了一个intent,但不知何故它不起作用,如果我单击RecyclerView的某个项,则它应该会打开下一个活动,但它不会。是否有其他方式开展另一项活动 以下是我的适配器类: class UserHolder
class UserHolder extends RecyclerView.ViewHolder {
TextView textViewUsername;
public UserHolder(@NonNull View itemView) {
super(itemView);
textViewUsername = itemView.findViewById(R.id.player_name_search);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener != null){
listener.onItemClick(getSnapshots().getSnapshot(position),(position));
Intent i = new Intent (itemView.getContext(), FriendRequest.class);
itemView.getContext().startActivity(i);
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick (DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
以下是我使用McClickListener的活动:
userAdapter.setOnItemClickListener(new UserAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Intent i = new Intent(PlayerSearch.this, FriendRequest.class);
startActivity(i);
}
});
以下是我的列表项布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardCornerRadius="20dp"
style="@style/CardViewWidgets"
android:clickable="true"
android:focusable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/picture_prize_recycler_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/suezkanal"/>
<TextView
android:id="@+id/listitem_text_view_prize_game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text_normal_size"
android:text="Some text"
android:layout_margin="8dp"
style="@style/TextView"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
多亏了@Mike M的回答。这个问题已经解决了。我必须从列表项中删除我的外线布局:
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardCornerRadius="20dp"
style="@style/CardViewWidgets"
android:clickable="true"
android:focusable="true"
android:layout_margin="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/picture_prize_recycler_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/suezkanal"/>
<TextView
android:id="@+id/listitem_text_view_prize_game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/text_normal_size"
android:text="Some text"
android:layout_margin="8dp"
style="@style/TextView"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
请您的问题解释“不工作”的确切含义。如果我单击RecyclerView中的某个项目,它不会打开下一个活动,您的活动是否在清单中正确声明?是,两个活动都在清单属性中声明从
中删除android:clickable
和android:focusable
属性。或者去掉外部的线性布局
,因为它毫无意义。