Android高级UI开发 RecyclerView高级进阶(五)飞虎潜行极战之切换

举报
yd_57386892 发表于 2020/12/28 23:22:09 2020/12/28
【摘要】 RecyclerView控件还有一个非常方便的功能,就是可以动态改变RecyclerView控件的展示风格,即在列表与网格Grid布局之间自由切换,先上效果图《飞虎队剧照》:   一、核心代码如下: 1. MainActivity.java package com.anyikang.volunteer.sos.recyclerview; import and...

RecyclerView控件还有一个非常方便的功能,就是可以动态改变RecyclerView控件的展示风格,即在列表与网格Grid布局之间自由切换,先上效果图《飞虎队剧照》:

 

一、核心代码如下:

1. MainActivity.java



 

  
  1. package com.anyikang.volunteer.sos.recyclerview;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.support.v7.widget.DefaultItemAnimator;
  5. import android.support.v7.widget.GridLayoutManager;
  6. import android.support.v7.widget.LinearLayoutManager;
  7. import android.support.v7.widget.RecyclerView;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.LinearLayout;
  11. import android.widget.Toast;
  12. public class MainActivity extends Activity {
  13. private RecyclerView recylerview; //RecyclerView控件实例对象
  14. //RecyclerView要显示的 列表数据,在此为一组字符串。
  15. private MyRecyclerAdapter adapter; //同ListView一样,需要一个适配器来 将list数据 装载到 RecyclerView列表控件。
  16. //private MyStaggedRecyclerAdapter adapter; //列表显示风格 为瀑布流 界面样式 的 适配器。
  17. boolean isGrid = true;
  18. Button btConvert;
  19. DividerGridViewItemDecoration mGridViewItemDecoration;
  20. MyListItemDecoration myListItemDecoration;
  21. private RecyclerView.ItemDecoration myItemDecoration;
  22. private int[] list= new int[]{
  23. R.drawable.fone,
  24. R.drawable.ftwo,
  25. R.drawable.fthree,
  26. R.drawable.ffour,
  27. R.drawable.ffive,
  28. R.drawable.fsix,
  29. R.drawable.fseven,
  30. R.drawable.feight,
  31. R.drawable.fnine,
  32. R.drawable.ften,
  33. R.drawable.feleven,
  34. R.drawable.ftwelve,
  35. R.drawable.thirteen
  36. };
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_main);
  41. initView();
  42. // initData();
  43. }
  44. /**
  45. * 初始化视图
  46. */
  47. public void initView()
  48. {
  49. mGridViewItemDecoration = new DividerGridViewItemDecoration(MainActivity.this);
  50. myListItemDecoration = new MyListItemDecoration(MainActivity.this,LinearLayout.VERTICAL);
  51. btConvert = findViewById(R.id.btConvert);
  52. //实例化布局中的recylerview控件
  53. recylerview = (RecyclerView)findViewById(R.id.recylerview);
  54. //普通列表的适配器
  55. adapter = new MyRecyclerAdapter(list);
  56. //recylerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));//列表显示风格为 水平方向的列表
  57. //列表显示风格为 垂直方向的列表
  58. //recylerview.setLayoutManager(new LinearLayoutManager(this));
  59. //显示风格为网格,2列
  60. recylerview.setLayoutManager(new GridLayoutManager(this, 2));
  61. //装载显示列表数据
  62. myItemDecoration = new DividerGridViewItemDecoration(this);
  63. //recylerview.addItemDecoration(new MyListItemDecoration(this, LinearLayout.VERTICAL));
  64. recylerview.addItemDecoration(myItemDecoration);
  65. recylerview.setItemAnimator(new DefaultItemAnimator());
  66. adapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener()
  67. {
  68. @Override
  69. public void onItemClick(View view, int position) {
  70. Toast.makeText(MainActivity.this, "点了"+position, Toast.LENGTH_SHORT).show();
  71. }
  72. });
  73. recylerview.setAdapter(adapter);
  74. btConvert.setOnClickListener(new View.OnClickListener() {
  75. @Override
  76. public void onClick(View view) {
  77. if(myItemDecoration!=null)
  78. recylerview.removeItemDecoration(myItemDecoration);
  79. if(!isGrid){
  80. recylerview.setLayoutManager(new GridLayoutManager(MainActivity.this, 2));
  81. //recylerview.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));//默认垂直
  82. myItemDecoration = new DividerGridViewItemDecoration(MainActivity.this);
  83. recylerview.addItemDecoration(myItemDecoration);
  84. }else{
  85. recylerview.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayout.VERTICAL,false));//默认垂直
  86. myItemDecoration = new MyListItemDecoration(MainActivity.this, LinearLayoutManager.VERTICAL);
  87. recylerview.addItemDecoration(myItemDecoration);
  88. }
  89. isGrid = !isGrid;
  90. }
  91. });
  92. return;
  93. }
  94. }
其中红色的代码,就是列表与网格布局切换的核心代码,这里值得注意的是以下代码:
 

  
  1. if(myItemDecoration!=null)
  2. recylerview.removeItemDecoration(myItemDecoration); 如果事先不移除的话,会出现分割线重复绘制
越来越粗
 

2. adapter适配器代码

MyRecyclerAdapter.java
 

  
  1. package com.anyikang.volunteer.sos.recyclerview;
  2. import android.support.v7.widget.RecyclerView;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.ImageView;
  6. public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
  7. private int[] list;
  8. private OnItemClickListener mOnItemClickListener;
  9. public MyRecyclerAdapter(int[] list) {
  10. this.list = list;
  11. }
  12. class MyViewHolder extends RecyclerView.ViewHolder{
  13. ImageView imv;
  14. public MyViewHolder(View view) {
  15. super(view);
  16. imv = (ImageView)view.findViewById(R.id.imv);
  17. }
  18. }
  19. @Override
  20. public int getItemCount() {
  21. return list.length;
  22. }
  23. @Override
  24. public void onBindViewHolder(MyViewHolder holder, final int position) {
  25. holder.imv.setImageResource(list[position]);
  26. if(mOnItemClickListener!=null){
  27. holder.itemView.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. mOnItemClickListener.onItemClick(v, position);
  31. }
  32. });
  33. }
  34. }
  35. @Override
  36. public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
  37. MyViewHolder holder = new MyViewHolder(View.inflate(viewGroup.getContext(), R.layout.list_item2, null));
  38. return holder;
  39. }
  40. /*public void addData(int position){
  41. list.add(position,"additem"+position);
  42. notifyItemInserted(position);
  43. }
  44. public void removeData(int position){
  45. list.remove(position);
  46. notifyItemRemoved(position);
  47. }*/
  48. public interface OnItemClickListener{
  49. void onItemClick(View view, int position);
  50. }
  51. public void setOnItemClickListener(OnItemClickListener listener){
  52. this.mOnItemClickListener = listener;
  53. }
  54. }

 

二、源码下载地址:

 

https://download.csdn.net/download/gaoxiaoweiandy/10409965

 

文章来源: blog.csdn.net,作者:冉航--小虾米,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/gaoxiaoweiandy/article/details/80291429

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。