首先我们需要解决一个问题
RecyclerView中的item如何从A到B跳转的问题?还有然后当我们解决了一个问题以后出现了第二个问题当出现多个item的时候如何从中进行跳转?
为此我们需要提前储备一些些知识。
需要先了解一些这篇文章如果对于item的点击事件不太了解的话。
Android中RecyclerView的Item点击事件(总结)
有需要下载源码可以点击:
https://github.com/307572384/recycleitemnew
也可以下这个csdn的
https://download.csdn.net/download/qq_16519957/10905113
首先我们写一个实体类Data来存放我们Adapter所需要的东西
package com.beta.recycleitem;
public class Data {
public static final int TYPE_ONE = 1;//类型1
public static final int TYPE_TWO = 2;//类型2
public int type;//item内容 类型
public String message;
public Data(int type, String message) {
this.type = type;
this.message = message;
}
public static int getTypeOne() {
return TYPE_ONE;
}
public static int getTypeTwo() {
return TYPE_TWO;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我们来写Recyclerview的Adpter主要就是存放点击事件
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Data> list;//数据源
private Context mcontent;//上下文
public MyRecyclerViewAdapter(List<Data> list, Context context) {
this.list = list;
this.mcontent = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//选择类型
switch (viewType) {
case Data.TYPE_ONE:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemone, parent, false);
return new OneViewHolder(view);
case Data.TYPE_TWO:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemtwo, parent, false);
return new TwoViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return list.size();
}
//item类型
@Override
public int getItemViewType(int position) {
return list.get(position).type;
}
//第一步:自定义一个回调接口来实现Click和LongClick事件
public interface OnItemClickListener {
void onItemClick(View v, int position);
void onItemLongClick(View v);
}
public OnItemClickListener mOnItemClickListener;//第二步:声明自定义的接口
//第三步:定义方法并暴露给外面的调用者
public void setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
}
//第一个item类型
class OneViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private Button btnAgree, btnRefuse;
public OneViewHolder(View itemView) {
super(itemView);
btnAgree = itemView.findViewById(R.id.btn_agree);
btnRefuse = itemView.findViewById(R.id.btn_refuse);
// 为item及item内部控件添加点击事件
itemView.setOnClickListener(this);
btnAgree.setOnClickListener(this);
btnRefuse.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v,getAdapterPosition());
}
}
}
//第二个item类型
class TwoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TwoViewHolder(View itemView) {
super(itemView);
// 为item添加点击事件
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
}
我们来写一个Mantivity这里主要是为了实现点击事件进行跳转的
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rv_recy;
private MyRecyclerViewAdapter adpter;
private List<Data> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
rv_recy.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));//布局管理器
adpter = new MyRecyclerViewAdapter(list, this);//从adapter中获取到存储的数据源list
rv_recy.setAdapter(adpter);
//设置item及item中的控件点击事件
adpter.setOnItemClickListener(MyItemClickListener);
}
private void initView() {
rv_recy = (RecyclerView) findViewById(R.id.rv_recy);
}
//存放生成条目
private void initData() {
list = new ArrayList<>();
list.add(new Data(1, "看看这个资源吧"));
list.add(new Data(2, "1"));
list.add(new Data(1, "2"));
list.add(new Data(2, "3"));
list.add(new Data(1, "4"));
list.add(new Data(2, "5"));
list.add(new Data(1, "6"));
list.add(new Data(2, "7"));
}
/**
* item+item里的控件点击监听事件
*/
private MyRecyclerViewAdapter.OnItemClickListener MyItemClickListener = new MyRecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
switch (v.getId()) {
case R.id.btn_agree:
//对item进行判断如果是第一个那么我们进行跳转反之则提示消息
if(position==0) {//这里position用于判断item是第几个条目然后我们对其设置就可以跳转了。
Intent intent = new Intent(MainActivity.this, testActivity.class);
intent.putExtra("name", list.get(position).toString());
startActivity(intent);
}
else{
Toast.makeText(MainActivity.this, "你点击了同意按钮" + (position + 1), Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_refuse:
Toast.makeText(MainActivity.this, "你点击了拒绝按钮" + (position + 1), Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(MainActivity.this, "你点击了item按钮" + (position + 1), Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onItemLongClick(View v) {
}
};
}
这个主要就是testActivity的一个这个非常简单的,这里我推荐大家使用的一个自动生成布局代码插件Android Studio 自动生成布局代码插件
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
public class testActivity extends Activity implements View.OnClickListener {
private Button button;
private Button test2;
private ToggleButton test;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitytwo);
initView();
}
private void initView() {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
test2 = (Button) findViewById(R.id.test2);
test2.setOnClickListener(this);
test = (ToggleButton) findViewById(R.id.test);
test.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
break;
case R.id.test2:
break;
case R.id.test:
break;
}
}
}
activity_main.xml的布局这里非常简单就一个RecyclerView
<?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="com.beta.recycleitem.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_recy"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
itemone
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv_icon">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username" />
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="无双?" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal"
android:paddingRight="5dp"
android:layout_centerVertical="true">
<Button
android:id="@+id/btn_agree"
android:layout_width="60dp"
android:layout_height="40dp"
android:text="同意"
android:textSize="10dp"/>
<Button
android:id="@+id/btn_refuse"
android:layout_width="60dp"
android:layout_height="40dp"
android:text="拒绝"
android:textSize="10dp"/>
</LinearLayout>
</RelativeLayout>
itemtwo
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv_icon">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username" />
</LinearLayout>
</RelativeLayout>
testActivity的布局activitytwo.xml
<?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">
<Button
android:id="@+id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<ToggleButton
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ToggleButton" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/134155.html