概述
1丶安卓5.0以下,需要导入android-support-v7-recyclerview.jar (缺陷eclipes,以前用的时候As还没有多普及,用的eclipse开发,需要导入这个jar文件包)
2丶分割线需要自己加(缺陷)
3丶实现了ListView横向滑动 瀑布流 瀑布流推荐用框架(优点)
4丶解决了ListView条目复用图片错位的问题(优点)
5丶点击事件需要自己写(缺陷)
分割线可以说事recycleview的一大缺点,在这里我就给大家推荐了一个我认为不错的解决分割线的问题,在这里不做解释了,具体实现代码里有详细介绍:
MainActivity
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
private RecyclerView mRecy;
private List<String> list;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
initView();
}
private void initList() {
list=new ArrayList<String>();
for(int i=0;i<50;i++){
list.add("item"+i+" ");
}
}
private void initView() {
mRecy=(RecyclerView) findViewById(R.id.mRecy);
//创建线性布局管理者对象
LinearLayoutManager manager=new LinearLayoutManager(this);
//通过manager对象设置显示方向
manager.setOrientation(RecyclerView.HORIZONTAL);
//将管理者对象给recyclerView
mRecy.setLayoutManager(manager);
mRecy.setAdapter(new MyAdapter(this, list));
mRecy.addItemDecoration(new MyDecor(this, RecyclerView.HORIZONTAL));
}
}
MyAdapter
import java.util.List;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.VH>{
private Context ctx;
private List<String> list;
public MyAdapter(Context ctx,List<String> list){
this.ctx=ctx;
this.list=list;
}
class VH extends ViewHolder{
private TextView mTv;
public VH(View itemView) {
super(itemView);
mTv=(TextView) itemView.findViewById(R.id.mTv);
}
}
@Override
public int getItemCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public void onBindViewHolder(VH arg0, int arg1) {
arg0.mTv.setText(list.get(arg1));
}
@Override
public VH onCreateViewHolder(ViewGroup arg0, int arg1) {
View v=View.inflate(ctx, R.layout.item, null);
return new VH(v);
}
}
MyDecor
import android.R.drawable;
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.RecyclerView;
import android.support.v7.widget.RecyclerView.ItemDecoration;
import android.support.v7.widget.RecyclerView.LayoutParams;
import android.support.v7.widget.RecyclerView.State;
import android.view.View;
public 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 ctx, int orization) {
TypedArray array=ctx.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){
drawHor(c,parent);
}
if(oritation==Horization){
drawVer(c,parent);
}
}
//水平方向回一条线
private void drawHor(Canvas c, RecyclerView parent) {
int left=parent.getPaddingLeft();
int right=parent.getWidth()-parent.getPaddingRight();
int itemCount=parent.getChildCount();
for(int i=0;i<itemCount;i++){
View child=parent.getChildAt(i);
//获得item上view视图的参数对象
RecyclerView.LayoutParams params=(LayoutParams) child.getLayoutParams();
//根据参数对象获取参数值
int top=child.getBottom()+params.bottomMargin;
//设置bottom坐标
int bottom=top+drawable.getIntrinsicHeight();
drawable.setBounds(left, top, right, bottom);
//花在画板上
drawable.draw(c);
}
}
//垂直方向画一条线
private void drawVer(Canvas c, RecyclerView parent) {
int top=parent.getPaddingTop();
int bottom=parent.getHeight()-parent.getPaddingBottom();
int itemCount=parent.getChildCount();
for(int i=0;i<itemCount;i++){
View child=parent.getChildAt(i);
//获得item上view视图的参数对象
RecyclerView.LayoutParams params=(LayoutParams) child.getLayoutParams();
//根据参数对象获取参数值
int left=child.getRight()+params.rightMargin;
//设置bottom坐标
int right=left+drawable.getIntrinsicWidth();
drawable.setBounds(left, top, right, bottom);
//花在画板上
drawable.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
// TODO Auto-generated method stub
super.getItemOffsets(outRect, itemPosition, parent);
}
}
//以下是xml文件:
activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="cn.wdf.recyclerveiw.MainActivity" >
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecy"
android:layout_width="match_parent"
android:layout_height="30dp"/>
</RelativeLayout>
item
<?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" >
<TextView
android:id="@+id/mTv"
android:layout_width="match_parent"
android:layout_height="30dp"/>
</LinearLayout>
最后
以上就是震动黑米为你收集整理的Android RecycleView分割线详解、完美解决Recyclerview没有分割线的问题的全部内容,希望文章能够帮你解决Android RecycleView分割线详解、完美解决Recyclerview没有分割线的问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复