使用 Firebase UI RecyclerView 打开另一个 activity
Opening another activity using Firebase UI RecyclerView
我查看了该主题的几个答案,但 none 产生了我想要的结果。我有一个 RecyclerView,它使用 Firebase UI 作为 RecyclerView,我想在其中添加一个 OnClick 事件。因此,用户单击 RecyclerView 中的一个项目并转到另一个 activity。为此,我在我的适配器 class 中使用了一个意图,但不知何故它不起作用,如果我点击 RecyclerView 的一个项目,它应该打开下一个 activity 但它没有。有没有其他方法可以打开另一个activity?
这是我的适配器 class:
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;
}
这是我使用 onItemClickListener 的 activity:
userAdapter.setOnItemClickListener(new UserAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Intent i = new Intent(PlayerSearch.this, FriendRequest.class);
startActivity(i);
}
});
这是我的 list_item 布局:
<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 的解答,该问题已解决。我不得不删除 ListItem 的外部 LinearLayout:
<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>
我查看了该主题的几个答案,但 none 产生了我想要的结果。我有一个 RecyclerView,它使用 Firebase UI 作为 RecyclerView,我想在其中添加一个 OnClick 事件。因此,用户单击 RecyclerView 中的一个项目并转到另一个 activity。为此,我在我的适配器 class 中使用了一个意图,但不知何故它不起作用,如果我点击 RecyclerView 的一个项目,它应该打开下一个 activity 但它没有。有没有其他方法可以打开另一个activity?
这是我的适配器 class:
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;
}
这是我使用 onItemClickListener 的 activity:
userAdapter.setOnItemClickListener(new UserAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Intent i = new Intent(PlayerSearch.this, FriendRequest.class);
startActivity(i);
}
});
这是我的 list_item 布局:
<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 的解答,该问题已解决。我不得不删除 ListItem 的外部 LinearLayout:
<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>