Recycleview完全可以替代Listview及Gridview加载数据
首先用Recycleview来实现Listview的效果:
//找到控件
recycle =(RecyclerView)findViewById(R.id.recycle);
//加载布局
recycle.setLayoutManager(new LinearLayoutManager(ShuipingActivity.this));
//加载数据
public void local(){
list_view =new ArrayList<String>();
for(int i=0;i<88;i++){
list_view.add(""+i);
}
}
//加载适配器
recycle.setAdapter(adapter =new MyAdapter());
//赋值
class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
MyViewHolder myViewHolder=new MyViewHolder(LayoutInflater.from(ShuipingActivity.this).inflate(R.layout.list_view,viewGroup,false));
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int i) {
myViewHolder.tv_listview.setText(list_view.get(i));
}
@Override
public int getItemCount() {
return list_view.size();
}
}
//找到控件
class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView tv_listview;
public MyViewHolder(View itemView) {
super(itemView);
tv_listview=(TextView)itemView.findViewById(R.id.tv_listview);
}
}
2.实现GridLayout效果:只需把加载布局那行代码改为` recycle_grid.setLayoutManager(new GridLayoutManager(ChuizhiActivity.this,4));
3.实现加下划线 提供一个DividerItemDecoration类继承RecyclerView.ItemDecoration
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
Log.v("recyclerview - itemdecoration", "onDraw()");
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
在Activity中实现`recycle_grid.addItemDecoration(new DividerItemDecoration(ChuizhiActivity.this,DividerItemDecoration.VERTICAL_LIST ));
recycle_grid.addItemDecoration(new DividerItemDecoration(ChuizhiActivity.this,DividerItemDecoration.HORIZONTAL_LIST));
4.实现瀑布流
将加载布局文件改为:
recyclr_pubu.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL));即可
5.实现瀑布流加载图片
//提供一个集合来存储随机数
mhegit=new ArrayList<Integer>();
for(int i=0;i<xianshi_list.length;i++){
mhegit.add( (int) (100 + Math.random() * 300));
}
在适配器的onBindViewHolder方法中实现
ViewGroup.LayoutParams pp = staggerViewHolder.pubu_img.getLayoutParams();
pp.height = mhegit.get(i);
staggerViewHolder.pubu_img.setLayoutParams(pp);
Glide.with(pubuxianshiActivity).load(xianshi_list[i]).into(staggerViewHolder.pubu_img);
以上就是简单的Recycleview的使用
“`