Skip to content

Commit 640d6a2

Browse files
author
hongyangAndroid
committed
add recyclerview gridlayoutmanager demo
1 parent 715026a commit 640d6a2

File tree

8 files changed

+241
-7
lines changed

8 files changed

+241
-7
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.zhy.autolayout.widget;
2+
3+
import android.content.Context;
4+
import android.support.v7.widget.Toolbar;
5+
import android.util.AttributeSet;
6+
7+
import com.zhy.autolayout.AutoLayoutInfo;
8+
import com.zhy.autolayout.utils.AutoLayoutHelper;
9+
10+
/**
11+
* Created by hupei on 2015/12/28 20:33.
12+
*/
13+
public class AutoToolbar extends Toolbar {
14+
15+
private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);
16+
17+
public AutoToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
18+
super(context, attrs, defStyleAttr);
19+
}
20+
21+
public AutoToolbar(Context context, AttributeSet attrs) {
22+
super(context, attrs);
23+
}
24+
25+
public AutoToolbar(Context context) {
26+
super(context);
27+
}
28+
29+
@Override
30+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
31+
if (!this.isInEditMode()) {
32+
this.mHelper.adjustChildren();
33+
}
34+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
35+
}
36+
37+
@Override
38+
protected void onLayout(boolean changed, int l, int t, int r, int b) {
39+
super.onLayout(changed, l, t, r, b);
40+
}
41+
42+
@Override
43+
public LayoutParams generateLayoutParams(AttributeSet attrs) {
44+
return new LayoutParams(this.getContext(), attrs);
45+
}
46+
47+
public static class LayoutParams extends Toolbar.LayoutParams implements AutoLayoutHelper.AutoLayoutParams {
48+
private AutoLayoutInfo mDimenLayoutInfo;
49+
50+
public LayoutParams(Context c, AttributeSet attrs) {
51+
super(c, attrs);
52+
this.mDimenLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
53+
}
54+
55+
@Override
56+
public AutoLayoutInfo getAutoLayoutInfo() {
57+
return this.mDimenLayoutInfo;
58+
}
59+
60+
public LayoutParams(int width, int height) {
61+
super(width, height);
62+
}
63+
64+
public LayoutParams(android.view.ViewGroup.LayoutParams source) {
65+
super(source);
66+
}
67+
68+
public LayoutParams(MarginLayoutParams source) {
69+
super(source);
70+
}
71+
}
72+
}

autolayout/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.github.dcendents.android-maven'
33
apply plugin: 'com.jfrog.bintray'
44

5-
version = "1.3.8"
5+
version = "1.3.9"
66

77
android {
88
compileSdkVersion 23

autolayout/src/main/java/com/zhy/autolayout/utils/AutoUtils.java

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,8 @@ public static boolean autoed(View view)
7373
public static void autoSize(View view)
7474
{
7575
ViewGroup.LayoutParams lp = view.getLayoutParams();
76-
7776
if (lp == null) return;
78-
79-
Object tag = view.getTag(R.id.id_tag_autolayout_size);
80-
if (tag != null) return;
81-
82-
view.setTag(R.id.id_tag_autolayout_size, "Just Identify");
77+
if (autoed(view)) return;
8378

8479
if (lp.width > 0)
8580
{
@@ -96,6 +91,49 @@ public static void autoSize(View view)
9691
}
9792
}
9893

94+
/**
95+
* @param view
96+
* @param widthBaseHeight true则layout_width的值以高度为标准;false则layout_height以宽度为标准
97+
*/
98+
public static void autoSize(View view, boolean widthBaseHeight)
99+
{
100+
ViewGroup.LayoutParams lp = view.getLayoutParams();
101+
if (lp == null) return;
102+
if (autoed(view)) return;
103+
104+
105+
if (lp.width > 0)
106+
{
107+
if (widthBaseHeight)
108+
{
109+
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
110+
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
111+
lp.width = (int) (lp.width * 1.0f / designHeight * screenHeight);
112+
} else
113+
{
114+
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
115+
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
116+
lp.width = (int) (lp.width * 1.0f / designWidth * screenWidth);
117+
}
118+
}
119+
120+
if (lp.height > 0)
121+
{
122+
if (!widthBaseHeight)
123+
{
124+
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
125+
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
126+
lp.height = (int) (lp.height * 1.0f / designWidth * screenWidth);
127+
} else
128+
{
129+
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
130+
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
131+
lp.height = (int) (lp.height * 1.0f / designHeight * screenHeight);
132+
}
133+
134+
}
135+
}
136+
99137
public static int getPercentWidthSize(int val)
100138
{
101139
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();

sample/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ dependencies {
2525
compile project(':autolayout')
2626
compile 'com.android.support:design:23.1.0'
2727
compile 'com.android.support:cardview-v7:23.1.0'
28+
compile 'com.android.support:recyclerview-v7:23.2.0'
2829
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.zhy.sample.fragment.ListFragment;
1515
import com.zhy.sample.fragment.PayFragment;
1616
import com.zhy.sample.fragment.RecyclerViewFragment;
17+
import com.zhy.sample.fragment.RecyclerViewGridFragment;
1718
import com.zhy.sample.fragment.RegisterFragment;
1819

1920
import java.util.ArrayList;
@@ -50,6 +51,7 @@ private void initDatas() {
5051
mList.add(new RegisterFragment());
5152
mList.add(new PayFragment());
5253
mList.add(new RecyclerViewFragment());
54+
mList.add(new RecyclerViewGridFragment());
5355
mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), mList));
5456
}
5557

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.zhy.sample.fragment;
2+
3+
import android.content.Context;
4+
import android.graphics.Color;
5+
import android.os.Bundle;
6+
import android.support.annotation.Nullable;
7+
import android.support.v4.app.Fragment;
8+
import android.support.v7.widget.GridLayoutManager;
9+
import android.support.v7.widget.RecyclerView;
10+
import android.view.LayoutInflater;
11+
import android.view.View;
12+
import android.view.ViewGroup;
13+
14+
import com.zhy.autolayout.utils.AutoUtils;
15+
import com.zhy.sample.R;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
import java.util.Random;
20+
21+
public class RecyclerViewGridFragment extends Fragment
22+
{
23+
private View mView;
24+
private RecyclerView mRecyclerView;
25+
private List<String> mList;
26+
private Context mContext;
27+
28+
@Override
29+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
30+
{
31+
mView = inflater.inflate(R.layout.fragment_recyclerview_grid, container, false);
32+
initView();
33+
return mView;
34+
}
35+
36+
private void initView()
37+
{
38+
mContext = getActivity();
39+
mRecyclerView = (RecyclerView) mView.findViewById(R.id.id_recyclerview);
40+
mList = new ArrayList<String>();
41+
for (int i = 0; i < 50; i++)
42+
{
43+
mList.add(i + "");
44+
}
45+
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false));
46+
mRecyclerView.setAdapter(new MyAdapter());
47+
48+
}
49+
50+
class MyAdapter extends RecyclerView.Adapter<ViewHolder>
51+
{
52+
@Override
53+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
54+
{
55+
View convertView = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item_grid, parent, false);
56+
return new ViewHolder(convertView);
57+
}
58+
59+
@Override
60+
public void onBindViewHolder(ViewHolder holder, int position)
61+
{
62+
63+
}
64+
65+
@Override
66+
public long getItemId(int position)
67+
{
68+
return position;
69+
}
70+
71+
@Override
72+
public int getItemCount()
73+
{
74+
return mList.size();
75+
}
76+
77+
78+
}
79+
80+
static class ViewHolder extends RecyclerView.ViewHolder
81+
{
82+
83+
public ViewHolder(View itemView)
84+
{
85+
super(itemView);
86+
Random random = new Random();
87+
itemView.setBackgroundColor(Color.argb(200, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
88+
//对于listview,注意添加这一行,即可在item上使用高度
89+
AutoUtils.autoSize(itemView, true);
90+
91+
// Log.e("", itemView.getLayoutParams().width + " , " + itemView.getLayoutParams().height);
92+
}
93+
}
94+
95+
}
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="wrap_content"
11+
>
12+
</android.support.v7.widget.RecyclerView>
13+
14+
</FrameLayout>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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="244px"
7+
android:layout_height="244px"
8+
>
9+
10+
11+
12+
</RelativeLayout>

0 commit comments

Comments
 (0)