1贬媒、xml文件中設(shè)置
<Button
? ? ? ? android:id="@+id/btn_zxing"
? ? ? ? android:text="二維碼"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? />
? ? <ImageView
? ? ? ? android:id="@+id/img"
? ? ? ? android:layout_margin="100dp"
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="300dp"/>
? ? <ScrollView
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:padding="20dp"
? ? ? ? android:scrollbars="none"
? ? ? ? app:layout_constraintEnd_toEndOf="parent"
? ? ? ? app:layout_constraintStart_toStartOf="parent"
? ? ? ? app:layout_constraintTop_toBottomOf="@+id/include2">
? ? ? ? <LinearLayout
? ? ? ? ? ? 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="vertical">
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:padding="10dp"
? ? ? ? ? ? ? ? ? ? android:text="已添加驗(yàn)證類型(已添加的類型將會在首頁展示)"
? ? ? ? ? ? ? ? ? ? android:textColor="@color/black" />
? ? ? ? ? ? ? ? <View
? ? ? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? ? ? android:layout_height="0.5dp"
? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? <androidx.recyclerview.widget.RecyclerView
? ? ? ? ? ? ? ? ? ? android:id="@+id/certification_manage_add_rv"
? ? ? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_margin="10dp" />
? ? ? ? ? ? </LinearLayout>
? ? ? ? ? ? <LinearLayout
? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:layout_marginTop="30dp"
? ? ? ? ? ? ? ? android:orientation="vertical">
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:padding="10dp"
? ? ? ? ? ? ? ? ? ? android:text="可添加驗(yàn)證類型"
? ? ? ? ? ? ? ? ? ? android:textColor="@color/black" />
? ? ? ? ? ? ? ? <View
? ? ? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? ? ? android:layout_height="0.5dp"
? ? ? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? <androidx.recyclerview.widget.RecyclerView
? ? ? ? ? ? ? ? ? ? android:id="@+id/certification_manage_can_add_rv"
? ? ? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? ? ? android:layout_margin="10dp" />
? ? ? ? ? ? </LinearLayout>
? ? ? ? </LinearLayout>
? ? </ScrollView>
? ? <com.google.android.material.floatingactionbutton.FloatingActionButton
? ? ? ? android:id="@+id/fab"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_gravity="bottom|end"
? ? ? ? android:layout_margin="@dimen/fab_margin"
? ? ? ? app:srcCompat="@android:drawable/ic_dialog_email" />
第二步:設(shè)置布局管理器
/**
? ? ? ? * 已添加
? ? ? ? */
? ? ? ? mCertificationManageAddRv.setLayoutManager(new GridLayoutManager(this, 3));
//? ? ? ? certificationManageAddRv.addItemDecoration(new GridItemDecoration(30));
? ? ? ? mAddAdapter = new CertificationManageadapter(addDatas, -1);
? ? ? ? mCertificationManageAddRv.setAdapter(mAddAdapter);
? ? ? ? mAddAdapter.setOnItemClickListener(setAdapterItemClick(0));
? ? ? ? /**
? ? ? ? * 可添加
? ? ? ? */
? ? ? ? mCertificationManageCanAddRv.setLayoutManager(new GridLayoutManager(this, 3));
//? ? ? ? certificationManageCanAddRv.addItemDecoration(new GridItemDecoration(30));
? ? ? ? mCanAddAdapter = new CertificationManageadapter(canAddDatas, -1);
? ? ? ? mCertificationManageCanAddRv.setAdapter(mCanAddAdapter);
? ? ? ? mCanAddAdapter.setOnItemClickListener(setAdapterItemClick(1));
第三步:設(shè)置適配器
1、先在 build.gradle(Project:XXXX) 的 repositories 添加:
? allprojects {
? ? ? ? repositories {
? ? ? ? ? ? ...
? ? ? ? ? ? maven { url "https://jitpack.io" }
? ? ? ? }
? ? }
2肘习、在 build.gradle(Module:app) 的 dependencies 添加:
dependencies {
? ? ? ? ? ? compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'
? ? }
3际乘、創(chuàng)建適配器
public class CertificationManageadapter extends BaseQuickAdapter<CertificationManageBean, BaseViewHolder> {
? ? public void setAdapterType(int adapterType) {
? ? ? ? this.adapterType = adapterType;
? ? }
? ? /**
? ? * 0 已添加 1可添加
? ? */
? ? private int adapterType;
? ? public CertificationManageadapter(@Nullable List<CertificationManageBean> data, int type) {
? ? ? ? super(R.layout.item_certification_manage, data);
? ? ? ? adapterType = type;
? ? }
? ? @Override
? ? protected void convert(BaseViewHolder helper, CertificationManageBean item) {
? ? ? ? helper.setText(R.id.certification_manage_tv_name, item.getName());
? ? ? ? if (adapterType == -1) {
? ? ? ? ? ? helper.getView(R.id.certification_manage_img).setVisibility(View.GONE);
? ? ? ? } else {
? ? ? ? ? ? helper.getView(R.id.certification_manage_img).setVisibility(View.VISIBLE);
? ? ? ? ? ? if (adapterType == 0) {
? ? ? ? ? ? ? ? helper.setImageResource(R.id.certification_manage_img, R.mipmap.ic_launcher);
? ? ? ? ? ? } else if (adapterType == 1) {
? ? ? ? ? ? ? ? helper.setImageResource(R.id.certification_manage_img, R.mipmap.ic_launcher);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
注意:CertificationManageBean是個(gè)實(shí)體類
public class CertificationManageBean {
? ? private int id;
? ? private String name;
? ? public CertificationManageBean(int id, String name) {
? ? ? ? this.id = id;
? ? ? ? this.name = name;
? ? }
? ? public int getId() {
? ? ? ? return id;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
}
布局文件:
<?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:id="@+id/main_cl"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:gravity="center"
? ? android:layout_margin="5dp"
? ? android:orientation="horizontal"
? ? android:padding="6dp">
? ? <TextView
? ? ? ? android:id="@+id/certification_manage_tv_name"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_weight="1"
? ? ? ? android:textSize="12sp"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_alignParentStart="true"
? ? ? ? android:layout_toStartOf="@+id/certification_manage_img"
? ? ? ? android:gravity="center_horizontal"
? ? ? ? tools:text="TextView"
? ? ? ? android:layout_alignParentLeft="true"
? ? ? ? android:layout_toLeftOf="@+id/certification_manage_img" />
? ? <ImageView
? ? ? ? android:id="@+id/certification_manage_img"
? ? ? ? android:layout_width="10dp"
? ? ? ? android:layout_height="10dp"
? ? ? ? android:layout_alignParentEnd="true"
? ? ? ? android:layout_marginStart="5dp"
? ? ? ? android:src="@mipmap/ic_launcher"
? ? ? ? android:layout_alignParentRight="true"
? ? ? ? android:layout_marginLeft="5dp" />
</LinearLayout>
第三步:創(chuàng)建適配器的監(jiān)聽方法,創(chuàng)建變量
? /**
? ? * 已添加驗(yàn)證類型
? ? */
? ? List<CertificationManageBean> addDatas = new ArrayList<>();
? ? /**
? ? * 可添加驗(yàn)證類型
? ? */
? ? boolean isOperating;
private BaseQuickAdapter.OnItemClickListener setAdapterItemClick(final int type) {
? ? ? ? return new BaseQuickAdapter.OnItemClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
? ? ? ? ? ? ? ? if (isOperating) {
? ? ? ? ? ? ? ? ? ? if (type == 0) {
? ? ? ? ? ? ? ? ? ? ? ? canAddDatas.add(addDatas.get(position));
? ? ? ? ? ? ? ? ? ? ? ? addDatas.remove(position);
? ? ? ? ? ? ? ? ? ? ? ? mAddAdapter.notifyItemRemoved(position);
? ? ? ? ? ? ? ? ? ? ? ? mCanAddAdapter.notifyItemChanged(canAddDatas.size() - 1);
? ? ? ? ? ? ? ? ? ? } else if (type == 1) {
? ? ? ? ? ? ? ? ? ? ? ? addDatas.add(canAddDatas.get(position));
? ? ? ? ? ? ? ? ? ? ? ? canAddDatas.remove(position);
? ? ? ? ? ? ? ? ? ? ? ? mCanAddAdapter.notifyItemRemoved(position);
? ? ? ? ? ? ? ? ? ? ? ? mAddAdapter.notifyItemChanged(addDatas.size() - 1);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? }
第四步:設(shè)置右上角按鈕的監(jiān)聽
? mBtnZxing.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? if (isOperating){
? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity2.this, ""+addDatas.toString(), Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? isOperating =! isOperating;
? ? ? ? ? ? ? ? if (isOperating) {
? ? ? ? ? ? ? ? ? ? mBtnZxing.setText("保存");
? ? ? ? ? ? ? ? ? ? mAddAdapter.setAdapterType(0);
? ? ? ? ? ? ? ? ? ? mAddAdapter.notifyDataSetChanged();
? ? ? ? ? ? ? ? ? ? mCanAddAdapter.setAdapterType(1);
? ? ? ? ? ? ? ? ? ? mCanAddAdapter.notifyDataSetChanged();
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? mBtnZxing.setText("類型管理");
? ? ? ? ? ? ? ? ? ? mAddAdapter.setAdapterType(-1);
? ? ? ? ? ? ? ? ? ? mAddAdapter.notifyDataSetChanged();
? ? ? ? ? ? ? ? ? ? mCanAddAdapter.setAdapterType(-1);
? ? ? ? ? ? ? ? ? ? mCanAddAdapter.notifyDataSetChanged();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });