Android高级UI开发 RecyclerView高级进阶(五)飞虎潜行极战之切换
【摘要】 RecyclerView控件还有一个非常方便的功能,就是可以动态改变RecyclerView控件的展示风格,即在列表与网格Grid布局之间自由切换,先上效果图《飞虎队剧照》:
一、核心代码如下:
1. MainActivity.java
package com.anyikang.volunteer.sos.recyclerview; import and...
RecyclerView控件还有一个非常方便的功能,就是可以动态改变RecyclerView控件的展示风格,即在列表与网格Grid布局之间自由切换,先上效果图《飞虎队剧照》:
一、核心代码如下:
1. MainActivity.java
-
package com.anyikang.volunteer.sos.recyclerview;
-
-
import android.app.Activity;
-
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.view.View;
-
import android.widget.Button;
-
import android.widget.LinearLayout;
-
import android.widget.Toast;
-
-
public class MainActivity extends Activity {
-
-
private RecyclerView recylerview; //RecyclerView控件实例对象
-
//RecyclerView要显示的 列表数据,在此为一组字符串。
-
private MyRecyclerAdapter adapter; //同ListView一样,需要一个适配器来 将list数据 装载到 RecyclerView列表控件。
-
//private MyStaggedRecyclerAdapter adapter; //列表显示风格 为瀑布流 界面样式 的 适配器。
-
boolean isGrid = true;
-
Button btConvert;
-
DividerGridViewItemDecoration mGridViewItemDecoration;
-
MyListItemDecoration myListItemDecoration;
-
private RecyclerView.ItemDecoration myItemDecoration;
-
-
private int[] list= new int[]{
-
R.drawable.fone,
-
R.drawable.ftwo,
-
R.drawable.fthree,
-
R.drawable.ffour,
-
R.drawable.ffive,
-
R.drawable.fsix,
-
R.drawable.fseven,
-
R.drawable.feight,
-
R.drawable.fnine,
-
R.drawable.ften,
-
R.drawable.feleven,
-
R.drawable.ftwelve,
-
R.drawable.thirteen
-
};
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
initView();
-
// initData();
-
}
-
-
/**
-
* 初始化视图
-
*/
-
public void initView()
-
{
-
-
mGridViewItemDecoration = new DividerGridViewItemDecoration(MainActivity.this);
-
myListItemDecoration = new MyListItemDecoration(MainActivity.this,LinearLayout.VERTICAL);
-
-
btConvert = findViewById(R.id.btConvert);
-
-
//实例化布局中的recylerview控件
-
recylerview = (RecyclerView)findViewById(R.id.recylerview);
-
-
-
//普通列表的适配器
-
adapter = new MyRecyclerAdapter(list);
-
-
//recylerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));//列表显示风格为 水平方向的列表
-
//列表显示风格为 垂直方向的列表
-
//recylerview.setLayoutManager(new LinearLayoutManager(this));
-
-
//显示风格为网格,2列
-
recylerview.setLayoutManager(new GridLayoutManager(this, 2));
-
//装载显示列表数据
-
-
myItemDecoration = new DividerGridViewItemDecoration(this);
-
//recylerview.addItemDecoration(new MyListItemDecoration(this, LinearLayout.VERTICAL));
-
recylerview.addItemDecoration(myItemDecoration);
-
-
recylerview.setItemAnimator(new DefaultItemAnimator());
-
-
adapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener()
-
{
-
-
@Override
-
public void onItemClick(View view, int position) {
-
-
Toast.makeText(MainActivity.this, "点了"+position, Toast.LENGTH_SHORT).show();
-
-
}
-
});
-
recylerview.setAdapter(adapter);
-
btConvert.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View view) {
-
-
-
if(myItemDecoration!=null)
-
recylerview.removeItemDecoration(myItemDecoration);
-
if(!isGrid){
-
recylerview.setLayoutManager(new GridLayoutManager(MainActivity.this, 2));
-
//recylerview.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));//默认垂直
-
myItemDecoration = new DividerGridViewItemDecoration(MainActivity.this);
-
recylerview.addItemDecoration(myItemDecoration);
-
}else{
-
recylerview.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayout.VERTICAL,false));//默认垂直
-
myItemDecoration = new MyListItemDecoration(MainActivity.this, LinearLayoutManager.VERTICAL);
-
recylerview.addItemDecoration(myItemDecoration);
-
}
-
isGrid = !isGrid;
-
}
-
});
-
return;
-
}
-
-
}
其中红色的代码,就是列表与网格布局切换的核心代码,这里值得注意的是以下代码:
-
if(myItemDecoration!=null)
-
recylerview.removeItemDecoration(myItemDecoration); 如果事先不移除的话,会出现分割线重复绘制
越来越粗
2. adapter适配器代码
MyRecyclerAdapter.java
-
package com.anyikang.volunteer.sos.recyclerview;
-
-
import android.support.v7.widget.RecyclerView;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.widget.ImageView;
-
-
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
-
-
private int[] list;
-
private OnItemClickListener mOnItemClickListener;
-
-
public MyRecyclerAdapter(int[] list) {
-
this.list = list;
-
}
-
-
class MyViewHolder extends RecyclerView.ViewHolder{
-
ImageView imv;
-
-
public MyViewHolder(View view) {
-
super(view);
-
imv = (ImageView)view.findViewById(R.id.imv);
-
}
-
-
}
-
@Override
-
public int getItemCount() {
-
return list.length;
-
}
-
-
@Override
-
public void onBindViewHolder(MyViewHolder holder, final int position) {
-
holder.imv.setImageResource(list[position]);
-
-
if(mOnItemClickListener!=null){
-
holder.itemView.setOnClickListener(new View.OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
mOnItemClickListener.onItemClick(v, position);
-
}
-
});
-
}
-
}
-
-
-
@Override
-
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {
-
MyViewHolder holder = new MyViewHolder(View.inflate(viewGroup.getContext(), R.layout.list_item2, null));
-
return holder;
-
}
-
-
-
/*public void addData(int position){
-
list.add(position,"additem"+position);
-
notifyItemInserted(position);
-
}
-
public void removeData(int position){
-
list.remove(position);
-
notifyItemRemoved(position);
-
}*/
-
-
public interface OnItemClickListener{
-
void onItemClick(View view, int position);
-
}
-
-
public void setOnItemClickListener(OnItemClickListener listener){
-
this.mOnItemClickListener = listener;
-
}
-
}
二、源码下载地址:
https://download.csdn.net/download/gaoxiaoweiandy/10409965
文章来源: blog.csdn.net,作者:冉航--小虾米,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/gaoxiaoweiandy/article/details/80291429
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)