问题描述
我需要为recycler view
的每个列表项设置 OnClickListener .
我想通过单击每个列表视图项来导航到其他活动.
I need to set OnClickListener for each list item of recycler view
.
I want to navigate to a different activity by clicking each list view item.
到目前为止,我已经设法通过回调将点击位置传递给then列表中每个点击项的相同活动.但是,当我从列表中单击一个项目时,我想开始一个不同的活动.
So far I've managed to pass the click position via a callback to the same activity for each click item from then list. However, I want to start a different activity when clicking an item from the list.
假设我想单击文本为"dd"的行,则应转到名为"abc"的活动
请帮助我.请.
这是主要活动:
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);
Intent intent = new Intent(this, abc.class);
this.startActivity(intent);
}
}
这是名为WordAdapter的适配器
This is Adapter named as 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;
}
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);
}
}
推荐答案
因此,当我对原始代码进行一些修改时,我将附上完整的解决方案.
So I will attach the full solution as I modified a bit the original code.
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);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewholder, int position) {
int resource = wordList.get(position).getImageResource();
String title = wordList.get(position).getTitle();
viewholder.bind(resource, title, mOnNoteListener);
}
@Override
public int getItemCount() {
return wordList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView title;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
title = itemView.findViewById(R.id.word);
}
private void bind(final int resource, final String titleText, final OnNoteListener listener) {
imageView.setImageResource(resource);
title.setText(titleText);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onNoteClick(titleText);
}
});
}
}
public interface OnNoteListener {
void onNoteClick(String dataClicked);
}
}
如您所见,我对代码进行了一些更改,以便您可以传递数据而不是位置.在下面,您可以看到启动新活动的代码.
As you can see I changed a bit the code so that you can pass the data and not the position. Below you can see the code that starts the new activity.
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(String dataClicked) {
Log.d(TAG, "clicked on the position:" + dataClicked);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(STRING_EXTRA, dataClicked);
startActivity(intent);
}
}
对于第二项活动,我添加了一些基本内容.
For the second activity I added something basic.
public class SecondActivity extends AppCompatActivity {
public static final String TAG = SecondActivity.class.getSimpleName();
public static String STRING_EXTRA = "string_extra";
private TextView textView = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
textView = findViewById(R.id.tv_received);
parseIntent(getIntent());
}
private void parseIntent(Intent intent) {
if (intent != null) {
Bundle extras = intent.getExtras();
assert extras != null;
String received = extras.getString(STRING_EXTRA);
textView.setText(received);
}
}
}
我添加了一个基本的xml布局(second_activity.xml):
I added a basic xml layout(second_activity.xml):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/tv_received"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="received from activity one"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
现在,您需要将活动注册到清单中,然后就应该完成了.
Now, you need to register the activity to the manifest and you should be done.
<aplication tag here>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<activity android:name=".SecondActivity" />
这篇关于我们如何为回收站视图的每个列表项(每行)设置onClickListener,为列表的不同对象设置不同的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!