Android RecyclerView详解及简单实例
作者:lqh 时间:2023-03-06 21:31:29
Android RecyclerView
小白今天第一次接触RecyclerView,前辈大神告诉我这是一个很神奇的控件,一看就是一整天。
RecyclerView中有规定好的方法去显示列表,图片甚至视频。还带有删除新建某一列表的方法。相对于ListView这个
RecyclerView控件就更加牛了。
明白的大神看一眼就懂,小白可以自己照源码敲一遍看看效果。这段程序是把A-Z按列排列下来:
package com.example.osserver.recycler;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.*;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
private List<String> mDatas;
private HomeAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
mRecyclerView = (RecyclerView)findViewById(R.id.id_recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter = new HomeAdapter());
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL_LIST));
}
private void initData() {
mDatas = new ArrayList<String>();
for (int i ='A';i<='Z';i++){
mDatas.add(""+(char)i);
}
}
class HomeAdapter extends RecyclerView.Adapter<MyViewHolder>
{
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item,parent,false));
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv.setText(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas.size();
}
}
class MyViewHolder extends RecyclerView.ViewHolder
{
TextView tv;
public MyViewHolder(View view) {
super(view);
tv = (TextView)view.findViewById(R.id.id_num);
}
}
}
接下来就是列表的润色部分:
今天我也只是进行到了这一步,踩了好多坑。
package com.zhy.sample.demo_recyclerview;
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* limitations under the License.
*/
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.State;
import android.util.Log;
import android.view.View;
/**
* This class is from the v7 samples of the Android SDK. It's not by me!
* <p/>
* See the license above for details.
*/
public class DividerItemDecoration extends 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);
}
}
}
不能添加图片,那就写这些好了。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:Android,RecyclerView
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot 多数据源处理事务的思路详解
2022-04-21 18:21:47
java selenium使用浏览器调试工具实现方法
2023-07-27 04:04:22
![](https://img.aspxhome.com/file/2023/7/57897_0s.gif)
Java查找并高亮PDF文本过程解析
2022-02-17 07:20:32
![](https://img.aspxhome.com/file/2023/8/75508_0s.png)
Java集合TreeSet用法详解
2023-11-10 22:53:34
![](https://img.aspxhome.com/file/2023/4/59354_0s.jpg)
C#将布尔类型转换成字节数组的方法
2023-06-21 15:30:16
Spring Boot中@ConditionalOnProperty的使用方法
2021-11-27 09:07:33
springcloud LogBack日志使用详解
2021-12-23 15:17:05
![](https://img.aspxhome.com/file/2023/4/63764_0s.png)
Android使用shape绘制阴影图层阴影效果示例
2021-11-25 01:19:39
![](https://img.aspxhome.com/file/2023/1/106061_0s.jpg)
Android支付宝支付开发实例
2023-01-01 09:15:18
![](https://img.aspxhome.com/file/2023/9/137679_0s.png)
Android键盘输入语言设置默认打开myanmar缅甸语的步骤
2021-07-01 17:15:44
Java实现上传文件图片到指定服务器目录
2023-06-28 00:23:32
Java编程之文件读写实例详解
2022-01-27 11:13:40
基于<aop:aspect>与<aop:advisor>的区别
2023-01-22 21:53:35
详解在Spring Boot框架下使用WebSocket实现消息推送
2023-03-08 01:32:05
![](https://img.aspxhome.com/file/2023/3/77993_0s.jpg)
springboot读取配置文件中的参数具体步骤
2023-11-29 05:46:14
![](https://img.aspxhome.com/file/2023/2/59582_0s.png)
java泛型基本知识和通用方法
2023-09-19 12:59:12
C# 进行图片压缩的示例代码(对jpg压缩效果最好)
2023-05-11 16:34:19
详解Java数据结构之平衡二叉树
2022-05-06 08:54:13
![](https://img.aspxhome.com/file/2023/1/78061_0s.png)
浅谈C#中的值类型和引用类型
2023-10-30 21:41:41
![](https://img.aspxhome.com/file/2023/4/98284_0s.jpg)
Java中ArrayList和SubList的坑面试题
2023-09-21 06:56:21
![](https://img.aspxhome.com/file/2023/5/76625_0s.png)