Skip to content

Commit 57d6bf6

Browse files
author
hongyangAndroid
committed
增加recyclerview的sample
1 parent 05a5f2d commit 57d6bf6

File tree

7 files changed

+298
-4
lines changed

7 files changed

+298
-4
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ public class AutoCardView extends CardView
206206

207207
### ListView、RecyclerView类的Item的适配
208208

209+
**sample中包含ListView、RecyclerView例子,具体查看sample**
210+
211+
* 对于ListView
212+
209213
对于ListView这类控件的item,默认根局部写“px”进行适配是无效的,因为外层非AutoXXXLayout,而是ListView。但是,不用怕,一行代码就可以支持了:
210214

211215
```java
@@ -232,6 +236,28 @@ public View getView(int position, View convertView, ViewGroup parent)
232236
注意` AutoUtils.autoSize(convertView);`这行代码的位置即可。demo中也有相关实例。
233237

234238

239+
* 对于RecyclerView
240+
241+
```java
242+
public ViewHolder(View itemView)
243+
{
244+
super(itemView);
245+
AutoUtils.autoSize(itemView);
246+
}
247+
248+
//...
249+
@Override
250+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
251+
{
252+
View convertView = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item, parent, false);
253+
return new ViewHolder(convertView);
254+
}
255+
256+
```
257+
258+
一定要记得`LayoutInflater.from(mContext).inflate`使用三个参数的方法!
259+
260+
235261
### 指定设置的值参考宽度或者高度
236262

237263
由于该库的特点,布局文件中宽高上的1px是不相等的,于是如果需要宽高保持一致的情况,布局中使用属性:

build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ buildscript {
77
dependencies {
88
classpath 'com.android.tools.build:gradle:1.2.3'
99
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
10-
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
11-
12-
// NOTE: Do not place your application dependencies here; they belong
13-
// in the individual module build.gradle files
10+
classpath 'com.github.dcendents:android-maven-plugin:1.2'
1411
}
1512
}
1613

sample/src/main/java/com/zhy/sample/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.zhy.autolayout.AutoLayoutActivity;
1414
import com.zhy.sample.fragment.ListFragment;
1515
import com.zhy.sample.fragment.PayFragment;
16+
import com.zhy.sample.fragment.RecyclerViewFragment;
1617
import com.zhy.sample.fragment.RegisterFragment;
1718

1819
import java.util.ArrayList;
@@ -48,6 +49,7 @@ private void initDatas() {
4849
mList.add(new ListFragment());
4950
mList.add(new RegisterFragment());
5051
mList.add(new PayFragment());
52+
mList.add(new RecyclerViewFragment());
5153
mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), mList));
5254
}
5355

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.zhy.sample.fragment;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.support.annotation.Nullable;
6+
import android.support.v4.app.Fragment;
7+
import android.support.v7.widget.LinearLayoutManager;
8+
import android.support.v7.widget.RecyclerView;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
13+
import com.zhy.autolayout.utils.AutoUtils;
14+
import com.zhy.sample.R;
15+
import com.zhy.sample.view.DividerItemDecoration;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
public class RecyclerViewFragment extends Fragment
21+
{
22+
private View mView;
23+
private RecyclerView mRecyclerView;
24+
private List<String> mList;
25+
private Context mContext;
26+
27+
@Override
28+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
29+
{
30+
mView = inflater.inflate(R.layout.fragment_recyclerview, container, false);
31+
initView();
32+
return mView;
33+
}
34+
35+
private void initView()
36+
{
37+
mContext = getActivity();
38+
mRecyclerView = (RecyclerView) mView.findViewById(R.id.id_recyclerview);
39+
mList = new ArrayList<String>();
40+
for (int i = 0; i < 50; i++)
41+
{
42+
mList.add(i + "");
43+
}
44+
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
45+
mRecyclerView.setAdapter(new MyAdapter());
46+
47+
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
48+
DividerItemDecoration.VERTICAL_LIST));
49+
}
50+
51+
class MyAdapter extends RecyclerView.Adapter<ViewHolder>
52+
{
53+
@Override
54+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
55+
{
56+
View convertView = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item, parent, false);
57+
return new ViewHolder(convertView);
58+
}
59+
60+
@Override
61+
public void onBindViewHolder(ViewHolder holder, int position)
62+
{
63+
64+
}
65+
66+
@Override
67+
public long getItemId(int position)
68+
{
69+
return position;
70+
}
71+
72+
@Override
73+
public int getItemCount()
74+
{
75+
return mList.size();
76+
}
77+
78+
79+
}
80+
81+
static class ViewHolder extends RecyclerView.ViewHolder
82+
{
83+
84+
public ViewHolder(View itemView)
85+
{
86+
super(itemView);
87+
//对于listview,注意添加这一行,即可在item上使用高度
88+
AutoUtils.autoSize(itemView);
89+
}
90+
}
91+
92+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.zhy.sample.view;/*
2+
* Copyright (C) 2014 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* limitations under the License.
6+
*/
7+
8+
import android.content.Context;
9+
import android.content.res.TypedArray;
10+
import android.graphics.Canvas;
11+
import android.graphics.Rect;
12+
import android.graphics.drawable.Drawable;
13+
import android.support.v7.widget.LinearLayoutManager;
14+
import android.support.v7.widget.RecyclerView;
15+
import android.util.Log;
16+
import android.view.View;
17+
18+
19+
/**
20+
* This class is from the v7 samples of the Android SDK. It's not by me!
21+
* <p/>
22+
* See the license above for details.
23+
*/
24+
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
25+
26+
private static final int[] ATTRS = new int[]{
27+
android.R.attr.listDivider
28+
};
29+
30+
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
31+
32+
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
33+
34+
private Drawable mDivider;
35+
36+
private int mOrientation;
37+
38+
public DividerItemDecoration(Context context, int orientation) {
39+
final TypedArray a = context.obtainStyledAttributes(ATTRS);
40+
mDivider = a.getDrawable(0);
41+
a.recycle();
42+
setOrientation(orientation);
43+
}
44+
45+
public void setOrientation(int orientation) {
46+
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
47+
throw new IllegalArgumentException("invalid orientation");
48+
}
49+
mOrientation = orientation;
50+
}
51+
52+
@Override
53+
public void onDraw(Canvas c, RecyclerView parent) {
54+
Log.v("recyclerview - itemdecoration", "onDraw()");
55+
56+
if (mOrientation == VERTICAL_LIST) {
57+
drawVertical(c, parent);
58+
} else {
59+
drawHorizontal(c, parent);
60+
}
61+
62+
}
63+
64+
65+
public void drawVertical(Canvas c, RecyclerView parent) {
66+
final int left = parent.getPaddingLeft();
67+
final int right = parent.getWidth() - parent.getPaddingRight();
68+
69+
final int childCount = parent.getChildCount();
70+
for (int i = 0; i < childCount; i++) {
71+
final View child = parent.getChildAt(i);
72+
RecyclerView v = new RecyclerView(parent.getContext());
73+
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
74+
.getLayoutParams();
75+
final int top = child.getBottom() + params.bottomMargin;
76+
final int bottom = top + mDivider.getIntrinsicHeight();
77+
mDivider.setBounds(left, top, right, bottom);
78+
mDivider.draw(c);
79+
}
80+
}
81+
82+
public void drawHorizontal(Canvas c, RecyclerView parent) {
83+
final int top = parent.getPaddingTop();
84+
final int bottom = parent.getHeight() - parent.getPaddingBottom();
85+
86+
final int childCount = parent.getChildCount();
87+
for (int i = 0; i < childCount; i++) {
88+
final View child = parent.getChildAt(i);
89+
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
90+
.getLayoutParams();
91+
final int left = child.getRight() + params.rightMargin;
92+
final int right = left + mDivider.getIntrinsicHeight();
93+
mDivider.setBounds(left, top, right, bottom);
94+
mDivider.draw(c);
95+
}
96+
}
97+
98+
@Override
99+
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
100+
if (mOrientation == VERTICAL_LIST) {
101+
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
102+
} else {
103+
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
104+
}
105+
}
106+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
>
6+
7+
<android.support.v7.widget.RecyclerView
8+
android:id="@+id/id_recyclerview"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
>
12+
</android.support.v7.widget.RecyclerView>
13+
14+
</FrameLayout>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- 消客的列表 -->
3+
<RelativeLayout
4+
xmlns:android="http://schemas.android.com/apk/res/android"
5+
xmlns:app="http://schemas.android.com/apk/res-auto"
6+
android:layout_width="match_parent"
7+
android:layout_height="244px"
8+
android:orientation="vertical" >
9+
10+
<ImageView
11+
android:layout_width="169px"
12+
android:layout_height="169px"
13+
app:layout_auto_baseheight="width"
14+
android:layout_centerVertical="true"
15+
android:layout_marginLeft="40px"
16+
android:src="@drawable/tuijian_touxiang6" />
17+
18+
<TextView
19+
android:id="@+id/xiaoke_name"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_marginLeft="262px"
23+
android:layout_marginTop="60px"
24+
android:text="刘小新"
25+
android:textSize="48px" />
26+
27+
<TextView
28+
android:id="@+id/xiaoke_people_nums"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_marginLeft="262px"
32+
android:layout_marginTop="136px"
33+
android:text="消客:566人"
34+
android:textColor="#656565"
35+
android:textSize="40px" />
36+
37+
<TextView
38+
android:id="@+id/xiaoke_phone"
39+
android:layout_width="wrap_content"
40+
android:layout_height="wrap_content"
41+
android:layout_marginLeft="580px"
42+
android:layout_marginTop="69px"
43+
android:text="手机:15511223564"
44+
android:textColor="#656565"
45+
android:textSize="38px" />
46+
47+
<TextView
48+
android:id="@+id/xiaoke_saleprice"
49+
android:layout_width="wrap_content"
50+
android:layout_height="wrap_content"
51+
android:layout_marginLeft="580px"
52+
android:layout_marginTop="136px"
53+
android:text="销售:¥8,653.00"
54+
android:textColor="#656565"
55+
android:textSize="38px" />
56+
57+
</RelativeLayout>

0 commit comments

Comments
 (0)