這次的適配器用的是BaseQuickAdapter
框架引入
在build.gradle(Project:XXXX):
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
在build.gradle(Module:app):
//BaseQuickAdapter依賴
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.31'
//recyclerview依賴
implementation 'com.android.support:recyclerview-v7:26.1.0'
然后是在Activity中
public class MainActivity extends AppCompatActivity {
private TextView tvAdd;
private RecyclerView recyclerView;
private Button btnCommit;
private CohabitantAdapter cohabitantAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
initView();
initData();
setListener();
}
private void initView() {
tvAdd = (TextView) findViewById(R.id.tv_add);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
btnCommit = (Button) findViewById(R.id.btn_commit);
}
private void initData() {
List<Bean> beans = intiData();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
cohabitantAdapter = new CohabitantAdapter(this);
recyclerView.setAdapter(cohabitantAdapter);
cohabitantAdapter.setNewData(beans);
}
private void setListener() {
//新增
tvAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cohabitantAdapter.addData(0, new Bean());
}
});
//刪除
cohabitantAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
switch (view.getId()) {
case R.id.tv_delete: // 點擊刪除
cohabitantAdapter.remove(position);
break;
}
}
});
//獲取數(shù)據(jù)
btnCommit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Bean> data = cohabitantAdapter.getData();
for (Bean datum : data) {
String name = datum.getName();
String gender = datum.getGender();
}
}
});
}
public List<Bean> intiData() {
List<Bean> mData = new ArrayList<>();
for (int i = 0; i < 1; i++) {
mData.add(new Bean());
}
return mData;
}
}
activity中的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.siyou.RvXinZeng.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_10"
android:gravity="right"
android:text="+"
android:textSize="@dimen/dp_40" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_below="@+id/tv_add"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btn_commit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="20dp"
android:layout_marginTop="@dimen/sp_14"
android:text="獲取數(shù)據(jù)" />
</RelativeLayout>
</LinearLayout>
適配器
public class CohabitantAdapter extends BaseQuickAdapter<CheckInPerson.UnderagesBean.TzrBean, BaseViewHolder> {
public CohabitantAdapter(Activity mActivity) {
super(R.layout.item_fragment_cohabitant);
}
@Override
protected void convert(BaseViewHolder helper, CheckInPerson.UnderagesBean.TzrBean item) {
if (item != null) {
helper.setText(R.id.et_checkIn_name, item.getName());
helper.setText(R.id.et_checkIn_relation, item.getRelationship());
}
helper.addOnClickListener(R.id.delete);
EditText view = helper.getView(R.id.et_checkIn_name);
EditText view1 = helper.getView(R.id.et_checkIn_relation);
view.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String s1 = s.toString();
item.setName(s1);
}
@Override
public void afterTextChanged(Editable s) {
}
});
view1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String s1 = s.toString();
item.setRelationship(s1);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
適配器的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/white"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
style="@style/item_style"
android:layout_width="match_parent"
android:layout_height="@dimen/item_height_50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="*"
android:textColor="@color/red"
android:visibility="visible" />
<TextView
style="@style/BaseStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="姓名  " />
<EditText
android:id="@+id/et_checkIn_name"
style="@style/BaseEditStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="0dp" />
</LinearLayout>
<LinearLayout
style="@style/item_style"
android:layout_width="match_parent"
android:layout_height="@dimen/item_height_50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="*"
android:textColor="@color/red"
android:visibility="visible" />
<TextView
style="@style/BaseStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="與未成人關系" />
<EditText
android:id="@+id/et_checkIn_relation"
style="@style/BaseEditStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="0dp" />
</LinearLayout>
<TextView
android:id="@+id/delete"
android:text="刪除"
android:layout_marginRight="@dimen/sp_16"
android:layout_gravity="right"
android:textColor="@color/red"
android:textSize="@dimen/sp_16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/sp_16" />
</LinearLayout>
最后附上效果圖
效果.gif
各位看官大人末贾,點個贊再走唄!!2芟恰趾浅!