forked from hongyangAndroid/AndroidAutoLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add recyclerview gridlayoutmanager demo
- Loading branch information
hongyangAndroid
committed
Mar 4, 2016
1 parent
715026a
commit 640d6a2
Showing
8 changed files
with
241 additions
and
7 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
autolayout-widget/src/main/java/com/zhy/autolayout/widget/AutoToolbar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
sample/src/main/java/com/zhy/sample/fragment/RecyclerViewGridFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |