一、思路简述
1.设计xml文件
需要跳转到一个新的Activity页面,而页面设计需要在创建Activity的时候生成的xml文件中进行
package com.example.myapplication5;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.information_weixin);
}
}
2、对item设置监听 即在onBindViewHolder中对item设置点击事件,当点击后执行所要跳转Activity中的代码。
3.外部使用该方法
最后需要对设置的adapter实现这个点击事件
二、代码展示
Adapter
public class MainActivity2 extends RecyclerView.Adapter<MySecondAdapter.InnerHolder> {
private final Context mContext;
private OnItemClickListener mListener;
public MainActivity2(Context context){
this.mContext=context;
}
@NonNull
@Override
public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(mContext, R.layout.item_my_recycler_view, null);
return new InnerHolder(view);
}
@Override
public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
holder.mTextView.setText("点击跳转");
holder.mImageView.setImageResource(R.mipmap.ic_launcher_round);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClick(position);
}
});
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener=listener;
}
public interface OnItemClickListener {
void onItemClick(int position);
}
@Override
public int getItemCount() {
return 30;
}
public class InnerHolder extends RecyclerView.ViewHolder {
private final TextView mTextView;
private final ImageView mImageView;
public InnerHolder(@NonNull View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.tv_title);
mImageView = itemView.findViewById(R.id.iv_pic);
}
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_pull_down_refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Weixin"
android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp" />
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
item_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:text="点击跳转"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
?
|