Android RecyclerView用實體類進行新增和刪除

這次的適配器用的是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="姓名&#8195;&#8195;" />


        <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芟恰趾浅!

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子议薪,更是在濱河造成了極大的恐慌疙咸,老刑警劉巖县匠,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異撒轮,居然都是意外死亡乞旦,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門题山,熙熙樓的掌柜王于貴愁眉苦臉地迎上來兰粉,“玉大人,你說我怎么就攤上這事顶瞳∏阻耄” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵浊仆,是天一觀的道長客峭。 經(jīng)常有香客問我,道長抡柿,這世上最難降的妖魔是什么舔琅? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮洲劣,結果婚禮上备蚓,老公的妹妹穿的比我還像新娘课蔬。我一直安慰自己,他們只是感情好郊尝,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布二跋。 她就那樣靜靜地躺著,像睡著了一般流昏。 火紅的嫁衣襯著肌膚如雪扎即。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天况凉,我揣著相機與錄音谚鄙,去河邊找鬼。 笑死刁绒,一個胖子當著我的面吹牛闷营,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播知市,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼傻盟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了嫂丙?” 一聲冷哼從身側響起莫杈,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎奢入,沒想到半個月后筝闹,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡腥光,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年关顷,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片武福。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡议双,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出捉片,到底是詐尸還是另有隱情平痰,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布伍纫,位于F島的核電站宗雇,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏莹规。R本人自食惡果不足惜赔蒲,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舞虱,春花似錦欢际、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至椅寺,卻和暖如春浑槽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背配并。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工括荡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留高镐,地道東北人溉旋。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像嫉髓,于是被迫代替她去往敵國和親观腊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內(nèi)容