RecyclerView notifyItemInserted(position)

该博客介绍了如何在Android应用中使用RecyclerView,并展示了如何通过调用notifyItemInserted(position)方法动态地在列表中插入数据。文章通过创建一个简单的RecyclerView实例,设置不同的布局管理器(LinearLayoutManager、GridLayoutManager、StaggeredGridLayoutManager),并实现数据的添加、删除功能,来演示数据更新的过程。
摘要由CSDN通过智能技术生成
package cgg.com.recyclertwo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<Bean> list = new ArrayList();
    private int[] imgIds = {
            R.drawable.g01, R.drawable.g02, R.drawable.g03, R.drawable.g04, R.drawable.g05,
            R.drawable.g06, R.drawable.g07, R.drawable.g08, R.drawable.g09, R.drawable.g10
    };
    private RecyclerView mRecycler;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setData();
        initView();
        // 实例化适配器
        myAdapter = new MyAdapter(list, this);
        // 设置默认的布局
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        // 设置动画
        mRecycler.setItemAnimator(new DefaultItemAnimator());
        // 设置适配器
        mRecycler.setAdapter(myAdapter);
        myAdapter.setOnRecyclerClickListener(new MyAdapter.RecyclerClickListener() {
            @Override
            public void myClick(int postion) {
                myAdapter.removeList(postion);
            }
        });
    }


    // 获取数据
    public void setData() {
        for (int i = 0; i < 9; i++) {
            Bean bean = new Bean("小阿姨 " + i, imgIds[i]);
            list.add(bean);
        }
    }

    // 初始化控件
    public void initView() {
        mRecycler = findViewById(R.id.mRecycler);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.one:
                mRecycler.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                break;

            case R.id.two:
                mRecycler.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
                break;

            case R.id.three:
                mRecycler.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.item, menu);
        return true;
package cgg.com.recyclertwo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;

/**
 * author: shichenguang
 * date:   2017/12/29 17:28
 * email:  none
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private ArrayList<Bean> list;
    private Context context;
    private RecyclerClickListener recyclerClickListener;
    public MyAdapter(ArrayList<Bean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 解析XML文件
        View view = View.inflate(context, R.layout.itme, null);



        // 返回一个MyViewHolder对象
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        // 获取list数据
        Bean bean = list.get(position);
        // 将数据赋值给item
        holder.getImageView().setImageResource(bean.getImgId());
        holder.getTextView().setText(bean.getName());

        // set MotionEvent
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerClickListener.myClick(position);
            }
        });

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.itemImg);
            this.textView = itemView.findViewById(R.id.itemText);

        }

        public ImageView getImageView() {
            return imageView;
        }

        public TextView getTextView() {
            return textView;
        }
    }

    public interface RecyclerClickListener{
        public void myClick(int postion);
    }

    public void addList(Bean bean,int position){
        list.add(position,bean);
        notifyDataSetChanged();
    }

    public void removeList(int position){
        list.remove(position);
        notifyDataSetChanged();
    }

    public void setOnRecyclerClickListener(RecyclerClickListener recyclerClickListener){
        this.recyclerClickListener = recyclerClickListener;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/one"
        android:title="list"
        app:showAsAction="never">
    </item>

    <item
        android:id="@+id/two"
        android:title="grid"
        app:showAsAction="never">
    </item>

    <item
        android:id="@+id/three"
        android:title="waterfall"
        app:showAsAction="never">
    </item>
</menu>
}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值