我想在单击回收者视图的对象时显示另一个活动。例如:如果我单击“ dd”文本或“该行的图像”,则应转到名为“ AbcActivity”的活动。
它去了很多站点,看到了youtube教程,但是没有帮助。请帮助我。请帮助我,这是我最后的希望:(
我是新手,如果您能提供项目,那就太好了。
这是主要活动:
public class MainActivity extends AppCompatActivity implements WordAdapter.OnNoteListener {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.home_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
List<Word> wordList = new ArrayList<>();
wordList.add(new Word(R.drawable.ic_launcher_background, "dd"));
wordList.add(new Word(R.drawable.ic_launcher_background, "ss"));
WordAdapter adapter = new WordAdapter(wordList, this);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onNoteClick(int position) {
}
}
WordAdapter:
public class WordAdapter extends RecyclerView.Adapter<WordAdapter.viewholder> {
private List<Word> wordList;
private OnNoteListener mOnNoteListener;
public WordAdapter(List<Word> wordList, OnNoteListener onNoteListener) {
this.wordList = wordList;
this.mOnNoteListener = onNoteListener;
}
@NonNull
@Override
public viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_items, viewGroup, false);
return new viewholder(view, mOnNoteListener);
}
@Override
public void onBindViewHolder(@NonNull viewholder viewholder, int position) {
int resource = wordList.get(position).getImageResource();
String title = wordList.get(position).getTitle();
viewholder.setData(resource, title);
}
@Override
public int getItemCount() {
return wordList.size();
}
class viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ImageView imageView;
private TextView title;
OnNoteListener onNoteListener;
public viewholder(@NonNull View itemView, OnNoteListener onNoteListener) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.word);
this.onNoteListener = onNoteListener;
itemView.setOnClickListener(this);
}
private void setData(int resource, String titleText){
imageView.setImageResource(resource);
title.setText(titleText);
}
@Override
public void onClick(View view) {
onNoteListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteListener {
void onNoteClick(int position);
}
}
这是字:
public class Word implements Parcelable {
private int imageResource;
private String title;
public Word(int imageResource, String title) {
this.imageResource = imageResource;
this.title = title;
}
protected Word(Parcel in) {
imageResource = in.readInt();
title = in.readString();
}
public static final Creator<Word> CREATOR = new Creator<Word>() {
@Override
public Word createFromParcel(Parcel in) {
return new Word(in);
}
@Override
public Word[] newArray(int size) {
return new Word[size];
}
};
public int getImageResource() {
return imageResource;
}
public String getTitle() {
return title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(imageResource);
parcel.writeString(title);
}
}
答案 0 :(得分:0)
您可以为OnClickListener
的{{1}}设置viewHolder
。
当您点击itemView
的{{1}}时,这将启动AbcActivity
。
imageView
答案 1 :(得分:0)
您几乎完成了所有操作。
剩下的实际上是处理您传递给适配器的侦听器在活动中让您知道的click事件。
更改
@Override
public void onNoteClick(int position) {
}
对此
@Override
public void onNoteClick(int position) {
Intent intent = new Intent(context, AbcActivity.class);
context.startActivity(intent);
}
您的活动
答案 2 :(得分:0)
您的代码可以正常工作。不知道是什么问题。在启动新活动时,您可能会遇到问题,但是可以肯定,此代码没有问题。 我将在您的示例下面发布,并对ViewHolder进行一些小的修改。
public class MainActivity extends AppCompatActivity implements WordAdapter.OnNoteListener {
private RecyclerView recyclerView;
private static final String TAG = MainActivity.class.getCanonicalName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.home_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
List<Word> wordList = new ArrayList<>();
wordList.add(new Word(R.drawable.ic_launcher_background, "dd"));
wordList.add(new Word(R.drawable.ic_launcher_background, "ss"));
wordList.add(new Word(R.drawable.ic_launcher_background, "cc"));
WordAdapter adapter = new WordAdapter(this);
recyclerView.setAdapter(adapter);
adapter.addItems(wordList);
}
@Override
public void onNoteClick(int position) {
Log.d(TAG, "clicked on the position:" + position);
}
}
public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {
private List<Word> wordList;
private OnNoteListener mOnNoteListener;
public WordAdapter(List<Word> wordList, OnNoteListener onNoteListener) {
this.wordList = wordList;
this.mOnNoteListener = onNoteListener;
}
public WordAdapter(OnNoteListener onNoteListener) {
this(new ArrayList<Word>(), onNoteListener);
}
public void addItems(List<Word> items) {
wordList.addAll(items);
notifyDataSetChanged();
}
public void clear() {
wordList.clear();
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_items, viewGroup, false);
return new ViewHolder(view, mOnNoteListener);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewholder, int position) {
int resource = wordList.get(position).getImageResource();
String title = wordList.get(position).getTitle();
viewholder.setData(resource, title);
}
@Override
public int getItemCount() {
return wordList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView imageView;
private TextView title;
private OnNoteListener onNoteListener;
public ViewHolder(@NonNull View itemView, OnNoteListener onNoteListener) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
title = itemView.findViewById(R.id.word);
this.onNoteListener = onNoteListener;
itemView.setOnClickListener(this);
}
private void setData(int resource, String titleText) {
imageView.setImageResource(resource);
title.setText(titleText);
}
@Override
public void onClick(View view) {
onNoteListener.onNoteClick(getAdapterPosition());
}
}
public interface OnNoteListener {
void onNoteClick(int position);
}
}
“类别”一词保持不变。
现在,关于xml,我在下面添加了一些快速内容(list_item.xml):
<android.support.constraint.ConstraintLayout 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"
android:background="@color/md_green_100"
android:orientation="horizontal"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp">
<TextView
android:id="@+id/word"
android:layout_width="86dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:gravity="center"
android:text="Word 1"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="120dp"
android:layout_height="160dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/ic_launcher_background"
android:contentDescription="Icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/word"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
现在,知道点击事件可以正常运行,无论您要执行什么操作,startActivity,Log等等。