implementation ‘androidx.recyclerview:recyclerview:1.1.0’
核心代码
adapter内
//再定义一个int类型的返回值方法
public int getthisPosition() {
return thisPosition;
}
// 传入点击的位置
public void setThisPosition(int thisPosition) {
this.thisPosition = thisPosition;
notifyDataSetChanged();
}
@NonNull
@Override
public void onBindViewHolder(HomeAdapter.MyViewHolder holder, int position) {
Log.e("wy", "104onBindViewHolder: " );
if(position==getthisPosition()){
holder.tv.setBackgroundResource(R.drawable.ok);
}else {
holder.tv.setBackgroundResource(R.drawable.room1);
}
holder.tv.setText(objects.get(position).getRoomName());
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemClick(holder.itemView, pos);
}
});
}
全代码
DeviceRoomBean loginResponse = new Gson().fromJson(result, DeviceRoomBean.class);
mDatas = loginResponse.getData();
Log.e("wy", "initData: "+mDatas.get(1).getRoomName()+" getRoomId:" +mDatas.get(1).getRoomId());
Log.e("wy", "initData: "+mDatas.get(1).getRoomName()+" getRoomId:" +mDatas.get(2).getRoomId());
HomeAdapter homeAdapter = new HomeAdapter(MainActivity.this, mDatas);
mRecyclerView.setAdapter(homeAdapter);
// mRecyclerView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Log.e("wy", "405onClick: ");
// replaceFragment(R.id.areafragmentlayout, new LabAreaSZWFragment());
// }
// });
homeAdapter.setOnItemClickLitener(new HomeAdapter.OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
Log.e("wy", "405onClick: "+mDatas.get(position).getRoomId()+" position:"+position);
homeAdapter.setThisPosition(position);
replaceFragment(R.id.areafragmentlayout, new LabAreaSZWFragment(mDatas.get(position).getRoomId()+""));
}
@Override
public void onItemLongClick(View view, int position) {
}
});
private int thisPosition = 0;默认选中第一个
package com.bainuosoft.wlimsapp.adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bainuosoft.wlimsapp.R;
import com.bainuosoft.wlimsapp.activity.MainActivity;
import com.bainuosoft.wlimsapp.model.DeviceRoomBean;
import java.util.ArrayList;
import java.util.List;
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
private Context context;
private LayoutInflater layoutInflater;
private List<DeviceRoomBean.DataDTO> objects = new ArrayList<DeviceRoomBean.DataDTO>();
//先声明一个int成员变量,若为0默认显示第一个
private int thisPosition = -1;
//再定义一个int类型的返回值方法
public int getthisPosition() {
return thisPosition;
}
// 传入点击的位置
public void setThisPosition(int thisPosition) {
this.thisPosition = thisPosition;
notifyDataSetChanged();
}
public HomeAdapter(Context context, List<DeviceRoomBean.DataDTO> objects) {
super();
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
this.objects = objects;
}
public interface OnItemClickLitener
{
void onItemClick(View view, int position);
void onItemLongClick(View view , int position);
}
private HomeAdapter.OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickLitener(HomeAdapter.OnItemClickLitener mOnItemClickLitener)
{
this.mOnItemClickLitener = mOnItemClickLitener;
}
@NonNull
@Override
public HomeAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// HomeAdapter.MyViewHolder holder = new HomeAdapter.MyViewHolder(LayoutInflater.from(
// this).inflate(R.layout.item_home, parent,
// false));
View view = LayoutInflater.from(context).inflate(R.layout.item_home, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@NonNull
@Override
public void onBindViewHolder(HomeAdapter.MyViewHolder holder, int position) {
Log.e("wy", "104onBindViewHolder: " );
if(position==getthisPosition()){
holder.tv.setBackgroundResource(R.drawable.ok);
}else {
holder.tv.setBackgroundResource(R.drawable.room1);
}
holder.tv.setText(objects.get(position).getRoomName());
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemClick(holder.itemView, pos);
}
});
}
@Override
public int getItemCount() {
return objects.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
public MyViewHolder(View view) {
super(view);
tv = (TextView) view.findViewById(R.id.id_num);
}
}
}
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/id_recyclerview_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#ffff0000"
android:dividerHeight="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/id_num"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:background="@drawable/recycler_view_item_tv_selector"
android:textColor="@color/white"
android:text="1222" />
</FrameLayout>