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.
- Loading branch information
hongyangAndroid
committed
Feb 22, 2016
1 parent
05a5f2d
commit 57d6bf6
Showing
7 changed files
with
298 additions
and
4 deletions.
There are no files selected for viewing
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
92 changes: 92 additions & 0 deletions
92
sample/src/main/java/com/zhy/sample/fragment/RecyclerViewFragment.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,92 @@ | ||
package com.zhy.sample.fragment; | ||
|
||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
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 com.zhy.sample.view.DividerItemDecoration; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RecyclerViewFragment 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, 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 LinearLayoutManager(getActivity())); | ||
mRecyclerView.setAdapter(new MyAdapter()); | ||
|
||
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), | ||
DividerItemDecoration.VERTICAL_LIST)); | ||
} | ||
|
||
class MyAdapter extends RecyclerView.Adapter<ViewHolder> | ||
{ | ||
@Override | ||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) | ||
{ | ||
View convertView = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item, 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); | ||
//对于listview,注意添加这一行,即可在item上使用高度 | ||
AutoUtils.autoSize(itemView); | ||
} | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
sample/src/main/java/com/zhy/sample/view/DividerItemDecoration.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,106 @@ | ||
package com.zhy.sample.view;/* | ||
* 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.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); | ||
RecyclerView v = new 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); | ||
} | ||
} | ||
} |
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="match_parent" | ||
> | ||
</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,57 @@ | ||
<?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="match_parent" | ||
android:layout_height="244px" | ||
android:orientation="vertical" > | ||
|
||
<ImageView | ||
android:layout_width="169px" | ||
android:layout_height="169px" | ||
app:layout_auto_baseheight="width" | ||
android:layout_centerVertical="true" | ||
android:layout_marginLeft="40px" | ||
android:src="@drawable/tuijian_touxiang6" /> | ||
|
||
<TextView | ||
android:id="@+id/xiaoke_name" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="262px" | ||
android:layout_marginTop="60px" | ||
android:text="刘小新" | ||
android:textSize="48px" /> | ||
|
||
<TextView | ||
android:id="@+id/xiaoke_people_nums" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="262px" | ||
android:layout_marginTop="136px" | ||
android:text="消客:566人" | ||
android:textColor="#656565" | ||
android:textSize="40px" /> | ||
|
||
<TextView | ||
android:id="@+id/xiaoke_phone" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="580px" | ||
android:layout_marginTop="69px" | ||
android:text="手机:15511223564" | ||
android:textColor="#656565" | ||
android:textSize="38px" /> | ||
|
||
<TextView | ||
android:id="@+id/xiaoke_saleprice" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="580px" | ||
android:layout_marginTop="136px" | ||
android:text="销售:¥8,653.00" | ||
android:textColor="#656565" | ||
android:textSize="38px" /> | ||
|
||
</RelativeLayout> |