博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android RecyclerView批量更新notifyItemRangeChanged
阅读量:6590 次
发布时间:2019-06-24

本文共 3894 字,大约阅读时间需要 12 分钟。

Android RecyclerView批量更新notifyItemRangeChanged
附录1的文章,介绍RecyclerView的定点更新,现在介绍RecyclerView的批量更新,RecyclerView的批量更新通过notifyItemRangeChanged实现,notifyItemRangeChanged官方文档:
/**         * Notify any registered observers that the itemCount items starting at         * position positionStart have changed.         * Equivalent to calling notifyItemRangeChanged(position, itemCount, null);.         *         * 

This is an item change event, not a structural change event. It indicates that * any reflection of the data in the given position range is out of date and should * be updated. The items in the given range retain the same identity.

* * @param positionStart Position of the first item that has changed * @param itemCount Number of items that have changed * * @see #notifyItemChanged(int) */ public final void notifyItemRangeChanged(int positionStart, int itemCount) { mObservable.notifyItemRangeChanged(positionStart, itemCount); }
示例代码:
package zhangphil.demo;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.TextView;import java.util.ArrayList;import java.util.Random;public class MainActivity extends Activity {    private ArrayList data = new ArrayList();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        for (int i = 0; i < 5; i++) {            data.add(System.currentTimeMillis());        }        RecyclerView mRecyclerView = findViewById(R.id.recycler_view);        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);        mLayoutManager.setOrientation(LinearLayout.VERTICAL);        mRecyclerView.setLayoutManager(mLayoutManager);        final RecyclerView.Adapter mAdapter = new MyAdapter();        mRecyclerView.setAdapter(mAdapter);        final Random random = new Random();        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                int count = random.nextInt(data.size());                for (int i = 0; i < count; i++) {                    data.set(i, System.currentTimeMillis());                }                mAdapter.notifyItemRangeChanged(0, count);            }        });    }    private class MyAdapter extends RecyclerView.Adapter
{ public MyAdapter() { super(); } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_2, null); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { viewHolder.text1.setText(i + ""); viewHolder.text2.setText(data.get(i) + ""); } @Override public int getItemCount() { return data.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public TextView text1; public TextView text2; public ViewHolder(View itemView) { super(itemView); text1 = itemView.findViewById(android.R.id.text1); text1.setTextColor(Color.RED); text2 = itemView.findViewById(android.R.id.text2); text2.setTextColor(Color.BLUE); } } }}
附录:
1,《Android RecyclerView更新子项目notifyItemChanged》链接:http://blog.csdn.net/zhangphil/article/details/78565738 
你可能感兴趣的文章
第18天:京东网页头部制作
查看>>
好消息:Dubbo & Spring Boot要来了
查看>>
面向对象封装的web服务器
查看>>
南开大学提出新物体分割评价指标,相比经典指标错误率降低 69.23%
查看>>
初创公司MindMaze研发情绪反应VR,让VR关怀你的喜怒哀乐
查看>>
绕开“陷阱“,阿里专家带你深入理解C++对象模型的特殊之处
查看>>
ElasticSearch
查看>>
9-51单片机ESP8266学习-AT指令(测试TCP服务器--51单片机程序配置8266,C#TCP客户端发信息给单片机控制小灯的亮灭)...
查看>>
香港设计师带来仿生机器人,其身体 70% 构造均由3D打印完成
查看>>
不规则物体形状匹配综述
查看>>
自动化设计-框架介绍 TestCase
查看>>
CJ看showgirl已经out!VR体验才是王道
查看>>
postgresql 数组类型
查看>>
Vue+Webpack常见问题(持续更新)
查看>>
栈与递归的实现
查看>>
Manually Summarizing EIGRP Routes
查看>>
spring boot 1.5.4 整合webService(十五)
查看>>
modsecurity(尚不完善)
查看>>
获取.propertys文件获取文件内容
查看>>
Redis3.0.5配置文件详解
查看>>