先看看效果图,是不是你们想要的
1.主活动MainActivity2类代码如下
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.tencent.bugly.crashreport.CrashReport;
import java.util.ArrayList;
import java.util.List;
public class MainActivity2 extends AppCompatActivity {
RecyclerView rc;
HomeAdapter homeAdapter;
List<String> list=new ArrayList<>();
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
for (int i = 0; i < 20; i++) {
list.add("这是第"+i+"个");
}
rc = (RecyclerView) findViewById(R.id.rc);
homeAdapter=new HomeAdapter(this,list);
//新建布局管理器
LinearLayoutManager manager = new LinearLayoutManager(this);
//设置是垂直还是水平,这里设置垂直
manager.setOrientation(LinearLayoutManager.VERTICAL);
//设置布局管理器
rc.setLayoutManager(manager);
rc.setAdapter(homeAdapter);
}
}
2.适配器类HomeAdapter代码如下
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder>{
private Context mContext;
List<String> list;
//选中的列表项索引值
private int lastPressIndex = -1;
public HomeAdapter(Context mContext, List<String> list) {
this.mContext = mContext;
this.list=list;
}
/**
* 设置布局
* @param viewGroup
* @param i
* @return
*/
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_recycler,viewGroup,false));
return holder;
}
/**
* 为控件绑定数据
* @param myViewHolder
* @param position
*/
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, @SuppressLint("RecyclerView") int position) {
myViewHolder.tv.setText("这是第"+position+"行");
if (position == lastPressIndex) {//当前选中的列表项,等于选中状态的索引值
//设置选中状态
myViewHolder.tv.setSelected(true);
} else {//未选中的列表项,不等于选中状态的索引值
//设置未选中状态
myViewHolder.tv.setSelected(false);
}
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里保存选中的列表项索引值,方便适配器刷新以后设置选中状态
lastPressIndex = position;
//这里实现的效果是点击选中,再次点击取消选中
if (lastPressIndex == position) {
// lastPressIndex = -1;
} else {
// lastPressIndex = position;
}
notifyDataSetChanged();
}
});
}
/**
* 返回项个数
* @return
*/
@Override
public int getItemCount() {
return list.size();
}
/**
* 定义控件并初始化
*/
class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv;
View view;
public MyViewHolder(View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
view = itemView.findViewById(R.id.view);
}
}
}
3.适配器的布局item_recycler代码如下
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center"
android:background="@drawable/tv_bk"
android:gravity="center"
android:text="这是一行文字"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:background="@color/black" />
</LinearLayout>
4.适配器的布局item_recycler的TextView用了一个背景选择器,tv_bk,这个文件放在drawable文件夹下。tv_bk的代码如下,如果没有对应的颜色,换成自己本地有的颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<corners android:radius="4dp" />
<solid android:color="@color/purple_200" />
<stroke android:color="@color/purple_200" android:width="1dp"/>
</shape>
</item>
<item android:state_selected="false">
<shape>
<corners android:radius="4dp" />
<solid android:color="@color/teal_200" />
<stroke android:color="@color/teal_200" android:width="1dp"/>
</shape>
</item>
</selector>