如何通过单击来自recyclerview的项目来打开新活动

时间:2019-04-03 10:22:04

标签: android android-intent android-activity android-recyclerview

如何通过单击来自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_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/cardview_shadow_end_color"
    android:clickable="true"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tvLessonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ders"
        android:textSize="25sp"
        android:textStyle="bold"
        android:layout_margin="7dp"/>
    <TextView
        android:id="@+id/tvLessonPoint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Not"
        android:textSize="20sp"
        android:layout_alignParentEnd="true"
        android:textStyle="italic"
        android:layout_margin="7dp"/>

</RelativeLayout>

5 个答案:

答案 0 :(得分:1)

由于API发生了根本变化,如果您要为每个项目创建一个OnClickListener,这也不会令我感到惊讶。不过,这并不是一件麻烦事。在RecyclerView.Adapter<MyViewHolder>的实现中,您应该具有:

private final OnClickListener mOnClickListener = new MyOnClickListener();

@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.myview, parent, false);
    view.setOnClickListener(mOnClickListener);
    return new MyViewHolder(view);
}

onClick方法:

@Override
public void onClick(final View view) {
    int itemPosition = mRecyclerView.getChildLayoutPosition(view);
    String item = mList.get(itemPosition);
    Toast.makeText(mContext, item, Toast.LENGTH_LONG).show();
}

答案 1 :(得分:0)

在适配器内部添加单击侦听器以启动活动。

 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 :(得分:0)

尝试一下

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 :(得分:0)

让我们在您的 onBindViewHolder

中尝试一下
#!/bin/sh
hostname
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%%)\n", $3,$2,$3*100/$2 }'
df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}'
systemctl status squid|grep Active

答案 4 :(得分:0)

在适配器类中将接口定义为

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类数据来启动另一个活动