RecyclerView 在v7包下 compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' 需要导入的依赖 布局 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/Swipe" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/Recycler" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </android.support.v4.widget.SwipeRefreshLayout> 这样可以系统自带下拉刷新 http://blog.csdn.net/u012124438/article/details/53495951 适配器继承这个类 RecyclerView.Adapter 代码里 线性布局和相对布局 LinearLayoutManager linear = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); GridLayoutManager grid = new GridLayoutManager(this, 3); 布局管理器 recycler.setLayoutManager(grid); 设置进度条颜色 swipe.setColorSchemeColors(Color.RED, Color.BLUE, Color.YELLOW); 添加分隔线 recycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL)); 自定义View下划线的写法 onDraw方法 onDrawOver方法——是在数据上面 自定义下划线的类自定义的写法 public class MyDivider extends RecyclerView.ItemDecoration { private Context pContext; private final int widthPixels; public MyDivider(Context context) { this.pContext = context; //获取手机屏幕的整个宽度 DisplayMetrics bm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(bm); widthPixels = bm.widthPixels; } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDraw(c, parent, state); //画笔 Paint paint = new Paint(); paint.setColor(Color.BLACK); // c.drawCircle(300, 400, 100, paint); // c.clipRect(0,100,1000,110); // c.drawRect(rectF,paint); //找到总的Item的条目数 int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { //获取每一个item控件视图 View childAt = parent.getChildAt(i); //获取控件的左位置和下位置 int right = childAt.getRight(); int top = childAt.getTop(); int left = childAt.getLeft(); int bottom = childAt.getBottom(); //矩形图片 RectF rectF = new RectF(left, bottom, widthPixels, bottom + 6); c.drawRect(rectF, paint); } } //在上面 @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { super.onDrawOver(c, parent, state); } } --------------------------------------------------- LayoutManager: 管理RecyclerView的结构. Adapter: 处理每个Item的显示. ItemDecoration: 添加每个Item的装饰. ItemAnimator: 负责添加\移除\重排序时的动画效果. class MyViewHolder extends RecyclerView.ViewHolder { private final ImageView image; private final TextView textview; public MyViewHolder(View itemView) { super(itemView); image = (ImageView) itemView.findViewById(R.id.item_image); textview = (TextView) itemView.findViewById(R.id.item_text); } public RecycleAdapter(Context context) { this.mcontext = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = View.inflate(mcontext, R.layout.item, null); MyViewHolder holder = new MyViewHolder(inflate); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyViewHolder vh = (MyViewHolder) holder; vh.textview.setText(list.get(position).getTitle()); mhomePresent.getImage(vh.image,list.get(position).getThumbnail_pic_s()); } @Override public int getItemCount() { return list.size(); } public void setDatas(HomeBean homeBean) { if (homeBean != null) { this.list.addAll(homeBean.getResult().getData()); } } } } package com.baway.myrecycleview.view.activity; import android.graphics.Color; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import com.baway.myrecycleview.R; import com.baway.myrecycleview.mondel.HomeBean; import com.baway.myrecycleview.present.HomePresent; import com.baway.myrecycleview.present.MyDivider; import com.baway.myrecycleview.view.adapter.RecycleAdapter; import com.baway.myrecycleview.view.iview.HomeView; public class MainActivity extends AppCompatActivity implements HomeView<HomeBean>, SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout swipeRefreshLayout; private RecyclerView recyclerView; private HomePresent homePresent; private RecycleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); } private void initData() { swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh_layout); recyclerView = (RecyclerView) findViewById(R.id.recycler_view); homePresent = new HomePresent(); homePresent.attachView(this); // LinearLayoutManager manager = new LinearLayoutManager(this); GridLayoutManager manager = new GridLayoutManager(this, 3); // StaggeredGridLayoutManager manager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(manager); swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN); adapter = new RecycleAdapter(MainActivity.this); adapter.setpresent(homePresent); recyclerView.setAdapter(adapter); recyclerView.addItemDecoration(new MyDivider(this)); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int itemCount = layoutManager.getItemCount(); int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition(); if (lastVisibleItemPosition + 1 == itemCount) { swipeRefreshLayout.setRefreshing(true); } } }); //添加分割线 recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); homePresent.setData(HomeBean.class); } @Override public void callBack(HomeBean homeBean) { adapter.setDatas(homeBean); adapter.notifyDataSetChanged(); } @Override public void onRefresh() { swipeRefreshLayout.setRefreshing(false); } }
Recycler
最新推荐文章于 2021-05-31 13:21:26 发布