Skip to content

Commit

Permalink
add recyclerview gridlayoutmanager demo
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Mar 4, 2016
1 parent 715026a commit 640d6a2
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.zhy.autolayout.widget;

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;

import com.zhy.autolayout.AutoLayoutInfo;
import com.zhy.autolayout.utils.AutoLayoutHelper;

/**
* Created by hupei on 2015/12/28 20:33.
*/
public class AutoToolbar extends Toolbar {

private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);

public AutoToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public AutoToolbar(Context context, AttributeSet attrs) {
super(context, attrs);
}

public AutoToolbar(Context context) {
super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!this.isInEditMode()) {
this.mHelper.adjustChildren();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(this.getContext(), attrs);
}

public static class LayoutParams extends Toolbar.LayoutParams implements AutoLayoutHelper.AutoLayoutParams {
private AutoLayoutInfo mDimenLayoutInfo;

public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
this.mDimenLayoutInfo = AutoLayoutHelper.getAutoLayoutInfo(c, attrs);
}

@Override
public AutoLayoutInfo getAutoLayoutInfo() {
return this.mDimenLayoutInfo;
}

public LayoutParams(int width, int height) {
super(width, height);
}

public LayoutParams(android.view.ViewGroup.LayoutParams source) {
super(source);
}

public LayoutParams(MarginLayoutParams source) {
super(source);
}
}
}
2 changes: 1 addition & 1 deletion autolayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.3.8"
version = "1.3.9"

android {
compileSdkVersion 23
Expand Down
50 changes: 44 additions & 6 deletions autolayout/src/main/java/com/zhy/autolayout/utils/AutoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,8 @@ public static boolean autoed(View view)
public static void autoSize(View view)
{
ViewGroup.LayoutParams lp = view.getLayoutParams();

if (lp == null) return;

Object tag = view.getTag(R.id.id_tag_autolayout_size);
if (tag != null) return;

view.setTag(R.id.id_tag_autolayout_size, "Just Identify");
if (autoed(view)) return;

if (lp.width > 0)
{
Expand All @@ -96,6 +91,49 @@ public static void autoSize(View view)
}
}

/**
* @param view
* @param widthBaseHeight true则layout_width的值以高度为标准;false则layout_height以宽度为标准
*/
public static void autoSize(View view, boolean widthBaseHeight)
{
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp == null) return;
if (autoed(view)) return;


if (lp.width > 0)
{
if (widthBaseHeight)
{
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
lp.width = (int) (lp.width * 1.0f / designHeight * screenHeight);
} else
{
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
lp.width = (int) (lp.width * 1.0f / designWidth * screenWidth);
}
}

if (lp.height > 0)
{
if (!widthBaseHeight)
{
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
lp.height = (int) (lp.height * 1.0f / designWidth * screenWidth);
} else
{
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
lp.height = (int) (lp.height * 1.0f / designHeight * screenHeight);
}

}
}

public static int getPercentWidthSize(int val)
{
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
Expand Down
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ dependencies {
compile project(':autolayout')
compile 'com.android.support:design:23.1.0'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.android.support:recyclerview-v7:23.2.0'
}
2 changes: 2 additions & 0 deletions sample/src/main/java/com/zhy/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.zhy.sample.fragment.ListFragment;
import com.zhy.sample.fragment.PayFragment;
import com.zhy.sample.fragment.RecyclerViewFragment;
import com.zhy.sample.fragment.RecyclerViewGridFragment;
import com.zhy.sample.fragment.RegisterFragment;

import java.util.ArrayList;
Expand Down Expand Up @@ -50,6 +51,7 @@ private void initDatas() {
mList.add(new RegisterFragment());
mList.add(new PayFragment());
mList.add(new RecyclerViewFragment());
mList.add(new RecyclerViewGridFragment());
mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), mList));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.zhy.sample.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhy.autolayout.utils.AutoUtils;
import com.zhy.sample.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecyclerViewGridFragment extends Fragment
{
private View mView;
private RecyclerView mRecyclerView;
private List<String> mList;
private Context mContext;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
mView = inflater.inflate(R.layout.fragment_recyclerview_grid, container, false);
initView();
return mView;
}

private void initView()
{
mContext = getActivity();
mRecyclerView = (RecyclerView) mView.findViewById(R.id.id_recyclerview);
mList = new ArrayList<String>();
for (int i = 0; i < 50; i++)
{
mList.add(i + "");
}
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false));
mRecyclerView.setAdapter(new MyAdapter());

}

class MyAdapter extends RecyclerView.Adapter<ViewHolder>
{
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View convertView = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item_grid, parent, false);
return new ViewHolder(convertView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{

}

@Override
public long getItemId(int position)
{
return position;
}

@Override
public int getItemCount()
{
return mList.size();
}


}

static class ViewHolder extends RecyclerView.ViewHolder
{

public ViewHolder(View itemView)
{
super(itemView);
Random random = new Random();
itemView.setBackgroundColor(Color.argb(200, random.nextInt(255), random.nextInt(255), random.nextInt(255)));
//对于listview,注意添加这一行,即可在item上使用高度
AutoUtils.autoSize(itemView, true);

// Log.e("", itemView.getLayoutParams().width + " , " + itemView.getLayoutParams().height);
}
}

}
14 changes: 14 additions & 0 deletions sample/src/main/res/layout/fragment_recyclerview_grid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</android.support.v7.widget.RecyclerView>

</FrameLayout>
12 changes: 12 additions & 0 deletions sample/src/main/res/layout/recyclerview_item_grid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 消客的列表 -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="244px"
android:layout_height="244px"
>



</RelativeLayout>

0 comments on commit 640d6a2

Please sign in to comment.