RecycleView中UI更新
来源:互联网 发布:mac上用word办公软件 编辑:程序博客网 时间:2024/06/08 14:18
原创文章,转载请注明 ( 来自:http://blog.csdn.net/leejizhou/article/details/51179233 李济洲的博客 )
如果你还不了解RecyclerView如何去使用,可以参考我的前面几篇博文,再看此篇博文:)
RecyclerView的使用(1)之HelloWorld
RecyclerView的使用(2)之多Item布局的加载
RecyclerView的使用(3)之添加Header和Footer
RecyclerView的使用(4)之下拉刷新和上拉加载
以前在使用ListView当中,如果我们的列表数据发生了改变,我们会去调用Adapter.notifyDataSetChanged()去更新UI界面上的列表数据,当然这个方法在RecyclerView也同样适用,那么既然说RecyclerView是ListView最好的替代者,那么RecyclerView有没有什么高级的方法呢?答案是有的。RecyclerView的Adapter支持更多种类的更新UI数据的方法,并且RecyclerView为添加删除移动都做了默认的动画效果。
看一下RecyclerView的Adapter主要增加了哪些方法:
- notifyItemChanged(int position) 更新列表position位置上的数据可以调用
- notifyItemInserted(int position) 列表position位置添加一条数据时可以调用,伴有动画效果
- notifyItemRemoved(int position) 列表position位置移除一条数据时调用,伴有动画效果
- notifyItemMoved(int fromPosition, int toPosition) 列表fromPosition位置的数据移到toPosition位置时调用,伴有动画效果
- notifyItemRangeChanged(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项进行数据刷新
- notifyItemRangeInserted(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项批量添加数据时调用,伴有动画效果
- notifyItemRangeRemoved(int positionStart, int itemCount) 列表从positionStart位置到itemCount数量的列表项批量删除数据时调用,伴有动画效果
看下本篇博文要实现的DEMO效果
可以看到RecyclerView的增删移动操作都自动带有了动画效果,这点靠Adapter.notifyDataSetChanged更新UI数据是做不到的,需要靠RecyclerView的Adapter新增的方法去实现。
下面详细的介绍下实现过程
配置Recyclerview,build.gradle
compile 'com.android.support:recyclerview-v7:23.1.1'
- 1
- 1
首先是主界面的布局,很简单上面一个Toolbar下面是RecyclerView,res/layout/activity_main.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.leejz.recyclerviewanim.MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:background="@color/colorPrimary" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" > </android.support.v7.widget.Toolbar> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv_list" /></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
RecyclerView列表项布局 ,res/layout/item_layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="8dp" android:background="@color/colorAccent" > <TextView android:id="@+id/tv_text" android:padding="20dp" android:textColor="#ffffff" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
然后是Toolbar需要用到的菜单XML,分别对应的是demo效果图toolbar上面4个选项,res/menu/menu_main.xml
<?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" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/menu_add" android:title="Add" android:orderInCategory="80" app:showAsAction="always" /> <item android:id="@+id/menu_del" android:title="Del" android:orderInCategory="80" app:showAsAction="always" /> <item android:id="@+id/menu_addmore" android:title="AddMore" android:orderInCategory="80" app:showAsAction="always" /> <item android:id="@+id/menu_move" android:title="Move" android:orderInCategory="80" app:showAsAction="always" /></menu>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
然后去建立RecyclerView的Adapter,很常规的一个Adapter如果看不懂,证明你还不会使用RecyclerView建议点击文章开头的几个链接去学习一下:)
RecyclerViewAdapter.Java
/** * Created by LeeJizhou on 2016/4/18. * Blog:www.lijizhou.com * QQ:3107777777 */public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> { private LayoutInflater mLayoutInflater; private Context mContext; private ArrayList<String> mTitle=new ArrayList<>(); public RecyclerViewAdapter(Context context,ArrayList<String>title){ mContext=context; mTitle=title; mLayoutInflater=LayoutInflater.from(context); } public static class MyViewHolder extends RecyclerView.ViewHolder{ TextView mTextView; public MyViewHolder(View itemView) { super(itemView); mTextView=(TextView)itemView.findViewById(R.id.tv_text); } } public void remove(int position) { mTitle.remove(position); notifyItemRemoved(position); } public void add(String text, int position) { mTitle.add(position, text); notifyItemInserted(position); } @Override public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new MyViewHolder(mLayoutInflater.inflate(R.layout.item_layout,parent,false)); } @Override public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, int position) { holder.mTextView.setText(mTitle.get(position)); } @Override public int getItemCount() { return mTitle==null ? 0 : mTitle.size(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
最后是重要的MainActivity类,其中涉及到了对Toolbar的使用,不熟悉的朋友也可以顺便复习下,Toolbar上面4个菜单分别对应了新增列表,删除列表,新增批量列表,列表项移动的操作,具体可以参考下面代码注释,MainActivity.java
/** * Created by LeeJizhou on 2016/4/18. * Blog:www.lijizhou.com * QQ:3107777777 */public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; //item 显示所需 private String[] title = {"JAVA","C","C++","C#","PYTHON","PHP" ,".NET","JAVASCRIPT","RUBY","PERL","VB","OC","SWIFT" }; private ArrayList<String> mTitle=new ArrayList<>(); private RecyclerViewAdapter mRecyclerViewAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Toolbar Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //去除Toolbar标题 getSupportActionBar().setDisplayShowTitleEnabled(false); //设置菜单点击监听 toolbar.setOnMenuItemClickListener(onMenuItemClick); mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView.setLayoutManager(layoutManager); Collections.addAll(mTitle,title); //为RecyclerView添加默认动画效果,测试不写也可以 mRecyclerView.setItemAnimator(new DefaultItemAnimator()); mRecyclerView.setAdapter(mRecyclerViewAdapter=new RecyclerViewAdapter(this, mTitle)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return super.onCreateOptionsMenu(menu); } private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.menu_add: //添加模拟数据到第一项 mTitle.add(0, "www.lijizhou.com"); //RecyclerView列表进行UI数据更新 mRecyclerViewAdapter.notifyItemInserted(0); //如果在第一项添加模拟数据需要调用 scrollToPosition(0)把列表移动到顶端(可选) mRecyclerView.scrollToPosition(0); break; case R.id.menu_del: //删除模拟数据第一项 mTitle.remove(0); //RecyclerView 列表进行UI数据更新 mRecyclerViewAdapter.notifyItemRemoved(0); break; case R.id.menu_move: //列表中第二项移到第三项 进行UI数据更新 mRecyclerViewAdapter.notifyItemMoved(1,2); break; case R.id.menu_addmore: //模拟数据批量添加4条数据 mTitle.add(0,"test"); mTitle.add(0,"test1"); mTitle.add(0,"test2"); mTitle.add(0,"test3"); //RecyclerView列表进行批量UI数据更新 mRecyclerViewAdapter.notifyItemRangeInserted(0,4); // scrollToPosition(0)作用是把列表移动到顶端 mRecyclerView.scrollToPosition(0); break; } return true; } };}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
OK,这样RecyclerView的Adapter更新UI数据的几个高级用法就介绍完了,具体的还要靠自己去实践摸索,去实践摸索,也欢迎大家在下方留言。
[本篇源码下载]
(http://download.csdn.net/detail/leejizhou/9495125)
- RecycleView中UI更新
- 线程中更新ui
- 线程中更新UI
- Android 非UI线程中更新UI
- Android 非UI线程中更新UI
- c# 线程中更新UI
- Android 线程中更新UI
- 工作线程中更新UI
- 子线程中更新UI
- Android中handler更新UI
- 在service 中更新UI
- .NET中 后台子线程 更新UI
- Avd2.3 - 线程中更新UI
- android中如何实现循环更新UI
- 有关Android中Service实现UI更新
- Android中使用定时器更新UI
- android中更新UI线程方法
- viewDidAppear:方法中UI不更新
- Tomcat 7 配置MySQL/Oracle JDBC
- zookeeper 工具类以及利用InterProcessLock 创建分布式锁
- Hibernate5 的ServiceRegistory的用法
- 直接在低版本IE6/7/8浏览器中使用HTML5的audio和video标签播放视频音频的办法
- C++实验4——输出星号图
- RecycleView中UI更新
- 6.0申请权限封装修改版
- c++測試網站
- SpringMVC实现文件下载
- spark.mllib源码阅读-分类算法5-GradientBoostedTrees
- 文章标题
- iis7 下配置MVC问题
- PHP交换变量值
- Android开发配置opencv环境超详细教程