如何通过单击 recyclerview 中的项目来打开新活动 [重复]
Posted
技术标签:
【中文标题】如何通过单击 recyclerview 中的项目来打开新活动 [重复]【英文标题】:How to open a new activity from clicking an item from recyclerview [duplicate] 【发布时间】:2019-08-24 20:16:30 【问题描述】:如何通过单击 recyclerview 中的项目来打开新活动。点击RecyclerView结构中的课程详情,课程详情会显示在下一页。我想为每个回收站打开包含不同信息的页面。
---RecyclerActivity.java
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setConten``tView(R.layout.lessons_layout);
recyclerView=findViewById(R.id.recyclerview);
MyAdapter myAdapter=new
MyAdapter(RecylerActivity.this,Lesson.getdata());
recyclerView.setAdapter(myAdapter);
LinearLayoutManager linearLayoutManager=new
LinearLayoutManager(RecylerActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
--MyAdapter.java
public MyAdapter(Context context,ArrayList<Lesson> data)
this.data = data;
inflater=LayoutInflater.from(context);
this.mDataList=data;
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
View v=inflater.inflate(R.layout.list_item,viewGroup,false);
MyViewHolder holder=new MyViewHolder(v);
return holder;
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i)
Lesson ClickedLesson=mDataList.get(i);
myViewHolder.setData(ClickedLesson,i);
@Override
public int getItemCount()
return mDataList.size();
class MyViewHolder extends RecyclerView.ViewHolder
TextView mLessonName,mLessonPoint;
RelativeLayout rel;
public MyViewHolder(@NonNull final View itemView)
super(itemView);
mLessonName=(TextView)itemView.findViewById(R.id.tvLessonName);
mLessonPoint=(TextView)itemView.findViewById(R.id.tvLessonPoint);
rel=(RelativeLayout)itemView.findViewById(R.id.Rellayout);
public void setData(Lesson ClickedLesson,int i)
this.mLessonName.setText(ClickedLesson.getLessonname());
this.mLessonPoint.setText(ClickedLesson.getPoint());
--Lesson.Java
public Lesson()
public Lesson(String lessonname,String point)
this.lessonname=lessonname;
this.point=point;
public String getLessonname()
return lessonname;
public String getPoint()
return point;
public void setLessonname(String lessonname)
this.lessonname = lessonname;
public void setPoint(String point)
this.point = point;
public static ArrayList<Lesson> getdata()
ArrayList<Lesson> dataList=new ArrayList<Lesson>();
Lesson tmp=new Lesson();
tmp.setLessonname("Matematik");
tmp.setPoint("BA");
dataList.add(tmp);
Lesson tmp2=new Lesson();
tmp2.setLessonname("Mobil programlama");
tmp2.setPoint("AA");
dataList.add(tmp2);
Lesson tmp3=new Lesson();
tmp3.setLessonname("Algoritma");
tmp3.setPoint("BB");
dataList.add(tmp3);
Lesson tmp4=new Lesson();
tmp4.setLessonname("İngilizce");
tmp4.setPoint("DC");
dataList.add(tmp4);
Lesson tmp5=new Lesson();
tmp5.setLessonname("Yapay zeka");
tmp5.setPoint("CC");
dataList.add(tmp5);
return dataList;
--list_item.xml
<RelativeLayout
android:id="@+id/Rellayout"
android:layout_
android:layout_
android:background="@color/cardview_shadow_end_color"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:id="@+id/tvLessonName"
android:layout_
android:layout_
android:text="Ders"
android:textSize="25sp"
android:textStyle="bold"
android:layout_margin="7dp"/>
<TextView
android:id="@+id/tvLessonPoint"
android:layout_
android:layout_
android:text="Not"
android:textSize="20sp"
android:layout_alignParentEnd="true"
android:textStyle="italic"
android:layout_margin="7dp"/>
</RelativeLayout>
【问题讨论】:
见This 【参考方案1】:在您的适配器内添加点击侦听器以启动活动。
holder.yourItem.setOnClickListener(v ->
Intent intent = new Intent(context, YourActivity.class);
Lesspn lesson = lesson.get(position);
intent.putExtra("lesson_details", lesson);
context.startActivity(intent, options.toBundle());
);
然后在下一个活动中
Lesson lesson = (Lesson) intent.getSerializableExtra("lesson_details");
你的课程应该implements Serializable
【讨论】:
【参考方案2】:试试这个,
class MyViewHolder extends RecyclerView.ViewHolder
TextView mLessonName,mLessonPoint;
RelativeLayout rel;
public MyViewHolder(@NonNull final View itemView)
super(itemView);
mLessonName=(TextView)itemView.findViewById(R.id.tvLessonName);
mLessonPoint=(TextView)itemView.findViewById(R.id.tvLessonPoint);
rel=(RelativeLayout)itemView.findViewById(R.id.Rellayout);
itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(v.getContext(),
NextActivity.class);
v.getContext().startActivity(intent);
【讨论】:
【参考方案3】:让我们在你的 onBindViewHolder
中试试这个itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Context mContext = v.getContext();
final Intent intent;
switch (getAdapterPostion())
case 0:
intent = new Intent(context, FirstActivity.class);
break;
case 1:
intent = new Intent(context, SecondActivity.class);
break;
...
default:
intent = new Intent(context, DefaultActivity.class);
break;
mContext.startActivity(intent);
【讨论】:
【参考方案4】:在你的适配器类中定义一个接口为
interface LessonClickListener
public void clickedLession(Lesson lession);
现在在你的适配器构造函数中初始化它,如下所示
public MyAdapter(Context context,ArrayList<Lesson> data, LessonClickListener listener)
this.data = data;
inflater=LayoutInflater.from(context);
this.listener=listener;
this.mDataList=data;
在你的 MyViewHolder 添加点击监听器
itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
listener.clickedLession(mDataList.get(getAdapterPosition()));
);
最终从您的 RecyclerActivity 实现这个 LessionClickListener 并覆盖 clickedLession()。 现在在这个方法中,你可以通过发送 Lession 类数据来启动另一个活动
【讨论】:
以上是关于如何通过单击 recyclerview 中的项目来打开新活动 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何解决单击水平 RecyclerView 中的项目的问题?
如何在MVVM架构中观察RecyclerView适配器中的LiveData?