效果图
Adapter
package com.ink.test;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class EditTextAndCheckBoxRecycleView extends RecyclerView.Adapter<EditTextAndCheckBoxRecycleView.MyViewHolder> {
private LayoutInflater mInflater;
private ArrayList<TestDataBean> datas = new ArrayList<>();
public EditTextAndCheckBoxRecycleView(Context context, ArrayList<TestDataBean> datas) {
mInflater = LayoutInflater.from(context);
this.datas = datas;
}
public void setCheck(int position) {
datas.get(position).setCheck(!datas.get(position).isCheck());
notifyDataSetChanged();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_item, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.name.setText(datas.get(position).getName());
if (datas.get(position).isCheck()) {
holder.check.setImageResource(R.drawable.check_on);
} else {
holder.check.setImageResource(R.drawable.check_off);
}
holder.editText.setText(datas.get(position).getText());
holder.editText.setTag(position);
holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
holder.editText.addTextChangedListener(new TextSwitcher(position,holder.editText));
}else{
holder.editText.removeTextChangedListener(new TextSwitcher(position,holder.editText));
}
}
});
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setCheck(position);
}
});
}
@Override
public int getItemCount() {
return datas.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView check;
EditText editText;
TextView name;
public MyViewHolder(View itemView) {
super(itemView);
check = itemView.findViewById(R.id.check);
editText = itemView.findViewById(R.id.edit);
name = itemView.findViewById(R.id.name);
}
}
class TextSwitcher implements TextWatcher {
private int mPosition;
private EditText editText;
TextSwitcher(int position, EditText editText) { mPosition = position;
this.editText = editText;}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(editText.isFocused()){
datas.get((Integer) editText.getTag()).setText(s.toString());
Log.e("onTextChanged",(Integer) editText.getTag()+" " + s.toString());
}
}
@Override
public void afterTextChanged(Editable s) {
}
}
}
activity
package com.ink.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends Activity {
private RecyclerView recyclerView;
private EditTextAndCheckBoxRecycleView adapter;
private ArrayList<TestDataBean> dataBeans = new ArrayList<>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.RecyclerView);
for (int i = 0; i <20 ; i++) {
TestDataBean testDataBean = new TestDataBean();
testDataBean.setName("这是第"+i+"条");
dataBeans.add(testDataBean);
}
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(gridLayoutManager);
adapter = new EditTextAndCheckBoxRecycleView(this,dataBeans);
recyclerView.setAdapter(adapter);
}
public void click(View view) {
for (int i = 0; i <dataBeans.size() ; i++) {
Log.e("ts", dataBeans.get(i).toString());
}
}
}
bean
package com.ink.test;
import androidx.annotation.NonNull;
/**
* ProjectName: test
* Package: com.ink.test
* ClassName: TestDataBean
* Description: java类作用描述
* CreateDate: 2020-12-11 14:23
* UpdateDate: 2020-12-11 14:23
* UpdateRemark: 更新说明
* Version: 1.0
*/
public class TestDataBean {
private String name ="";
private boolean check = false;
private String text = "";
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NonNull
@Override
public String toString() {
return name +" "+ text;
}
}
adapter_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/check"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="center_vertical"
android:src="@drawable/check_off"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="10dp"
android:textColor="#222222"
android:textSize="18sp"/>
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="5"
android:hint="请输入"
android:textSize="14sp"
android:singleLine="true"
android:maxLines="1"/>
</LinearLayout>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</androidx.recyclerview.widget.RecyclerView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click">
</Button>
</LinearLayout>