我能够检测到个别回收者查看项目的位置,并能够点击它来烘烤它.现在我不想继续进行新的活动,并显示点击项目的详细信息,我该怎么做?说,我正在显示联系人姓名列表和onclick我不想打开一个新的活动节目,联系人的详细信息...至少,如何在点击该联系人项目时再次举报联系人姓名,当前变量如何开启点击可用给我?我打算捆绑这些变量并发送它们并在那里显示.
我知道我必须在这里实施
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position){
//i know i have to implement here
}
}
Run Code Online (Sandbox Code Playgroud)
让我们以您自己的联系人列表为例来向您解释完整的概念。假设我们有一个名为 Contact 的自定义类,其中包含一个字符串“Name”,如下所示:-
class Contact implements Parcelable{
String name;
public void setName(String name) {
this.name = name;
}
public String getName() { return name; }
// Add your Parcelable code here
}
Run Code Online (Sandbox Code Playgroud)
现在,在您的活动中,您将侦听器附加到您的回收器视图,您的代码将如下所示:-
List<Contact> contacts = new ArrayList<>();
// You have to initialize your contacts list with data, I just gave you an example
recyclerItemClickListener = new RecyclerItemClickListener(this,
new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("Key", contacts.get(position);
startActivity(intent);
}
});
recyclerView.addOnItemTouchListener(recyclerItemClickListener);
Run Code Online (Sandbox Code Playgroud)
更新的答案:-
要在另一个活动中使用 key 的值,请执行以下操作:-
Contact contact = getIntent().getParcelableExtra("Key");
Run Code Online (Sandbox Code Playgroud)
上面的代码行借助您的密钥从意图中检索联系人对象。
| 归档时间: |
|
| 查看次数: |
3725 次 |
| 最近记录: |