本文主要是介绍android中RecyclerView的简单使用(二),三面头条+四面阿里+五面腾讯拿offer分享面经总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在ListView中,Google为我们提供了SetDivider(Drawable divider)这样的方法来设置分隔线,那么在RecyclerView中,Google又为我们提供了什么样的方法去添加分隔线呢?通过查看官方文档,它,提供了:addItemDecoration(RecyclerView.ItemDecoration decor)这个方法了设置分隔线,那问题又来了,RecyclerView.ItemDecoration是什么东西呢?(文章结尾部分做出了解释)
那么接下来就将分割线的有关代码贴出来,并将我自己的理解描述一下。
首先先贴代码:
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7
.widget.RecyclerView.ItemDecoration;
import android.support.v7.widget.RecyclerView.LayoutParams;
import android.support.v7.widget.RecyclerView.State;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
// 创建集合,存放数据
private ArrayList list = new ArrayList();
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 查找控件
recyclerView = (RecyclerView) findViewById(R.id.rview);
// 初始化list集合
for (int i = 0; i < 100; i++) {
list.add("数据 " + i);
}
MyAdapter adapter = new MyAdapter(MainActivity.this, list);
//将适配器放到recyclerView上
//创建线性布局管理者对象
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
//StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2,
// RecyclerView.VERTICAL);
//通过manager对象设置显示方向
manager.setOrientation(RecyclerView.VERTICAL);
//将管理者对象给recyclerView
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new MyDecor(MainActivity.this, RecyclerView.VERTICAL));
}
}
class MyAdapter extends RecyclerView.Adapter {
// 声明两个属性,接收传递过来的数据
private Context context;
private ArrayList list;
private MyViewHolder viewHolder;
// 创建一个构造方法,将数据和activity的上下文传递过来
public MyAdapter(Context context, ArrayList list) {
this.context = context;
this.list = list;
}
// 返回显示的条目个数
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public void onBindViewHolder(ViewHolder arg0, int arg1) {
// 将父类的对象强转为子类对象
// MyViewHolder myViewHolder = (MyViewHolder) arg0;
if (arg0 instanceof MyViewHolder) {
viewHolder = (MyViewHolder) arg0;
}
String ss = list.get(arg1);
viewHolder.textView.setText(ss);
}
// 创建viewholder对象,并将对象返回
@Override
public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
View view = LayoutInflater.from(context).inflate(R.layout.re, null);
ViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
class MyViewHolder extends ViewHolder {
TextView textView;
public MyViewHolder(View view) {
super(view);
textView = (TextView) view.findViewById(R.id.tview);
}
}
}
class MyDecor extends ItemDecoration {
//定义一个水平和竖直常量
private final int Horization = RecyclerView.HORIZONTAL;
private final int Vertical = RecyclerView.VERTICAL;
//创建一个int型的数组,在数组中存放android中自带的分割线
private int [] attrs = {android.R.attr.listDivider};
//声明一个图片对象
private Drawable drawable;
//声明一个RecyclerView的方向
private int oritation;
public MyDecor (Context context,int orization){
TypedArray array = context.obtainStyledAttributes(attrs);
drawable = array.getDrawable(0);
//让这个属性重复使用
array.recycle();
//判断方向
if (orization != Horization && orization != Vertical) {
//如果既不是水平又不是竖直的,抛出异常
throw new IllegalArgumentException(“没有该方向”);
}else {
oritation = orization;
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, State state) {
// TODO Auto-generated method stub
super.onDraw(c, parent, state);
if (oritation == Vertical) {
new IllegalArgumentException(“没有该方向”);
}else {
oritation = orization;
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, State state) {
// TODO Auto-generated method stub
super.onDraw(c, parent, state);
if (oritation == Vertical) {
这篇关于android中RecyclerView的简单使用(二),三面头条+四面阿里+五面腾讯拿offer分享面经总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!