RecyclerView使用,RecyclerView使用,增加一个item,删除一个item,更新数据,item点击事件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/baozilichao/article/details/52698407

build.gradle

 compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:recyclerview-v7:24.2.0'

main_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=".MainActivity">

    <Button
        android:id="@+id/bt111"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="流布局" />

    <Button
        android:id="@+id/bt112"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="线性布局" />

    <Button
        android:id="@+id/bt113"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加一个Item"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/bt114"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="删除一个Item"
        android:textAllCaps="false" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/srl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv111"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

item.xml

<?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="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是but"
        android:layout_margin="5dp"
        android:id="@+id/but3"/>

</LinearLayout>

MainAcitvity

package com.baozilichao.recyclerviewuse;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    RecyclerView rv1;
    List<String> list1, list2;
    Button bt1, bt2, bt3, bt4;
    SwipeRefreshLayout srl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initDate();
        initView();
    }

    private void initView() {
        srl = (SwipeRefreshLayout) findViewById(R.id.srl);
        rv1 = (RecyclerView) findViewById(R.id.rv111);
        bt1 = (Button) findViewById(R.id.bt111);
        bt2 = (Button) findViewById(R.id.bt112);
        bt3 = (Button) findViewById(R.id.bt113);
        bt4 = (Button) findViewById(R.id.bt114);
        final MyAdpater adapter = new MyAdpater(list1);
        //瀑布流
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rv1.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
            }
        });
        //线性布局
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rv1.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            }
        });
//增加一个item
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list1.size() > 0) {
                    list1.add("我是加的");
                    adapter.notifyItemInserted(list1.size());//更新adapter
                }
            }
        });
//删除一个item
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list1.size() > 1) {
                    list1.remove(0);
                    adapter.notifyItemRemoved(list1.size());//移除更新
                }
            }
        });
//        刷新
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (list1.size() > 0) {
                    list1.add("我是刷新的");
                    list1.addAll(list2);
                    adapter.notifyItemInserted(list1.size());
                    srl.setRefreshing(false);//停止刷新
                }
            }
        });
//item点击事件
        rv1.setAdapter(adapter);
        adapter.setOnclickible(new MainActivity.Onclickible() {
            @Override
            public void ItemClick(int pos) {
                adapter.list1.add(list1.size() + "");
                adapter.notifyItemInserted(list1.size());
//                adapter.addData(list1.size()+"", list1.size());
                Toast.makeText(MainActivity.this, "" + pos, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initDate() {
        list1 = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            list1.add("item" + i);
        }
        list2 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list2.add("item" + i);
        }
    }

    class MyAdpater extends RecyclerView.Adapter<MyAdpater.MyViewHolder> {

        private List<String> list1;
        private MainActivity.Onclickible onclickible;

        public MyAdpater(List<String> list1) {
            this.list1 = list1;
        }

        public void setOnclickible(MainActivity.Onclickible onclickible) {
            this.onclickible = onclickible;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(MainActivity.this, R.layout.item, null);
            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }

        @Override
        public int getItemCount() {
            return list1.size();
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {
            MyViewHolder mholder = holder;
            mholder.tv3.setText(list1.get(position));
            mholder.tv3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onclickible.ItemClick(position);
                }
            });
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            Button tv3;

            public MyViewHolder(View itemView) {
                super(itemView);
                tv3 = (Button) itemView.findViewById(R.id.but3);
            }
        }
    }

    public interface Onclickible {
        void ItemClick(int pos);
    }

}

这里写图片描述

传送门

展开阅读全文

没有更多推荐了,返回首页