因为时间不够,先整理一些小知识点,最后再统一排版
1 RecyclerView点击事件
//设置点击事件的接口
private Inspection_Switch_One_Level_Adapter.ItemClickListener mItemClickListener;
public interface ItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(Inspection_Switch_One_Level_Adapter.ItemClickListener itemClickListener) {
this.mItemClickListener = itemClickListener;
}
在onBindViewHolder中加入如下代码
if (mItemClickListener != null) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//if (mEditMode == 1) {
//if (!isSelect.get(position)) {
//holder.ImageView_Switch_Check.setImageResource(R.drawable.ic_checked);
// isSelect.remove(position);
//isSelect.put(position, true);
//} else {
//holder.ImageView_Switch_Check.setImageResource(R.drawable.ic_uncheck);
//isSelect.remove(position);
//isSelect.put(position, false);
//}
//}
//mItemClickListener.onItemClick(position);
}
});
}
2 RecyclerView长按事件
private Inspection_Switch_One_Level_Adapter.OnItemLongClickListener monItemLongClickListener;
public interface OnItemLongClickListener {
void onItemLongClickListener(int position);
}
public void setMonItemLongClickListener(Inspection_Switch_One_Level_Adapter.OnItemLongClickListener monItemLongClickListener) {
this.monItemLongClickListener = monItemLongClickListener;
}
在onBindViewHolder中加入如下代码:
if (monItemLongClickListener != null) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
monItemLongClickListener.onItemLongClickListener(position);
//返回值设为true表示消耗了时间不会传递
return true;
}
});
}
3 RecyclerView 滑动之后导致RadioButton错乱的问题
由于RecyclerView的onBindViewHolder()方法,只有在getItemViewType()返回类型不同时才会调用,这点是跟ListView的getView()方法不同的地方,所以如果想要每次都调用onBindViewHolder()刷新item数据,就要重写getItemViewType(),让其返回position,否则很容易产生数据错乱的现象。
我这是是先用一个HashMap将RadioButton的选中情况与item的position情况存起来 以便后面的调用,然后再重写getItemViewType()方法返回值是position即可保证RadioButton选中情况不发生错乱,
本demo可在日志中完整打印出来结果,便于查看。
下面是代码:
1)适配器的代码:
package com.example.administrator.radiobutton_test_1;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
/**
* Created by Administrator on 2019/1/9 0009.
*/
public class RBandETtest_Adapter extends RecyclerView.Adapter<RBandETtest_Adapter.ViewHolder> {
private List<RBandETtest> mRBandETtestList;
private int layoutPosition;
static class ViewHolder extends RecyclerView.ViewHolder {
RadioGroup radioGroup;
RadioButton radioButton_Yes;
RadioButton radioButton_No;
TextView editText_remark;
public ViewHolder(View view) {
super(view);
radioButton_Yes = (RadioButton) view.findViewById(R.id.RadioButton_Yes);
radioButton_No = (RadioButton) view.findViewById(R.id.RadioButton_No);
radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
editText_remark = (TextView) view.findViewById(R.id.editText_remark);
}
}
public RBandETtest_Adapter(List<RBandETtest> RBandETtestList) {
mRBandETtestList = RBandETtestList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rbandettest_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
//0代表否,1代表是,2代表未选择
private static HashMap<Integer, Integer> isSelect = new HashMap<>();
public void iniChossen() {
//这里取反
for (int i = 0; i < mRBandETtestList.size(); i++) {
isSelect.put(i, 2);
}
}
public void setSelectCondition(int position) {
if (b.equals("是")) {
isSelect.remove(position);
isSelect.put(position, 1);
} else if (b.equals("否")) {
isSelect.remove(position);
isSelect.put(position, 0);
} else {
}
}
public HashMap<Integer, Integer> getSelectCondition() {
return isSelect;
}
//设置点击事件的接口
private RBandETtest_Adapter.ItemClickListener mItemClickListener;
public interface ItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(RBandETtest_Adapter.ItemClickListener itemClickListener) {
this.mItemClickListener = itemClickListener;
}
public String returnRadioButtonText() {
String ss = "";
ss = b;
return ss;
}
String b;
// 由于RecyclerView的onBindViewHolder()方法,只有在getItemViewType()返回类型不同时才会调用,
// 这点是跟ListView的getView()方法不同的地方,所以如果想要每次都调用onBindViewHolder()刷新item数据,
// 就要重写getItemViewType(),让其返回position,否则很容易产生数据错乱的现象。
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
try {
if (isSelect.get(position) == 0) {
holder.radioButton_No.setChecked(true);
} else if (isSelect.get(position) == 1) {
holder.radioButton_Yes.setChecked(true);
} else {
holder.radioGroup.clearCheck();
}
Log.e("-------------------" + String.valueOf(position), String.valueOf(isSelect.get(position)));
//每一次都要重新加载radioButton的选中值
holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
layoutPosition = holder.getLayoutPosition();
mItemClickListener.onItemClick(i);
if (radioGroup.getCheckedRadioButtonId() == R.id.RadioButton_Yes) {
b = "是";
} else if (radioGroup.getCheckedRadioButtonId() == R.id.RadioButton_No) {
b = "否";
} else {
b = "未选择";
}
setSelectCondition(position);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return mRBandETtestList.size();
}
}
2)布局item代码,很简单只有一组RadioButton,MainActivity中我是循环了150组
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioButton_Yes"
android:text="是"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RadioButton_No"
android:text="否"/>
</RadioGroup>
</LinearLayout>
3)MainActivity中的代码
package com.example.administrator.radiobutton_test_1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
public List<RBandETtest> rBandETtestList = new ArrayList<>();
RBandETtest_Adapter rBandETtest_adapter;
ArrayList<RBandETtest> selectlist;
Button button;
SparseBooleanArray sparseBooleanArray;
HashMap<Integer,Integer> isSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
button=(Button)findViewById(R.id.button);
iniData();
selectlist = new ArrayList<>();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
rBandETtest_adapter = new RBandETtest_Adapter(rBandETtestList);
recyclerView.setAdapter(rBandETtest_adapter);
rBandETtest_adapter.iniChossen();
rBandETtest_adapter.setOnItemClickListener(new RBandETtest_Adapter.ItemClickListener() {
@Override
public void onItemClick(int position) {
String ss=rBandETtest_adapter.returnRadioButtonText();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isSelect=rBandETtest_adapter.getSelectCondition();
String s1="是";
String s2="否";
for (int i = 0; i < isSelect.size(); i++) {
if (isSelect.get(i)==0){
Log.e("--------------"+i, s2 );
}else if (isSelect.get(i)==1){
Log.e("--------------"+i, s1 );
}else {
Log.e("--------------"+i, "未选择" );
}
}
}
});
}
public void iniData() {
for (int i = 0; i < 150; i++) {
RBandETtest rbet = new RBandETtest("123");
rBandETtestList.add(rbet);
}
}
}
4)父布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.radiobutton_test_1.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RecyclerView" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="获取选中项"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
</RelativeLayout>
代码比较简单,是我专门做的一个测试demo,有需要的可以尝试迁移到自己的项目中去,欢迎交流
下面是一个demo的链接
防止RecyclerView滑动出现数据错乱包括RadioButton,EditText
关注+私信,我免费发送给你。