Skip to content

Commit a937a6d

Browse files
author
zhangxutong
committed
旋转画廊
1 parent 98130e0 commit a937a6d

File tree

4 files changed

+108
-38
lines changed

4 files changed

+108
-38
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/mcxtzhang/flowlayoutmanager/gallary/GalleryActivity.java

+17-37
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,46 @@
66
import android.support.v7.widget.RecyclerView;
77
import android.support.v7.widget.SnapHelper;
88
import android.util.Log;
9-
import android.view.View;
9+
import android.widget.ImageView;
1010

1111
import com.mcxtzhang.commonadapter.rv.CommonAdapter;
1212
import com.mcxtzhang.commonadapter.rv.ViewHolder;
1313
import com.mcxtzhang.flowlayoutmanager.R;
14-
import com.mcxtzhang.flowlayoutmanager.TestBean;
15-
import com.mcxtzhang.flowlayoutmanager.other.ImgAdapter;
14+
import com.mcxtzhang.flowlayoutmanager.swipecard.SwipeCardBean;
15+
import com.squareup.picasso.Picasso;
1616

17-
import java.util.ArrayList;
1817
import java.util.List;
1918

19+
import static com.mcxtzhang.flowlayoutmanager.swipecard.SwipeCardBean.initDatas;
20+
2021
public class GalleryActivity extends AppCompatActivity {
2122
private RecyclerView mRv;
22-
private CommonAdapter<TestBean> mAdapter;
23-
private List<TestBean> mDatas;
23+
private CommonAdapter<SwipeCardBean> mAdapter;
24+
private List<SwipeCardBean> mDatas;
2425

2526
@Override
2627
protected void onCreate(Bundle savedInstanceState) {
2728
super.onCreate(savedInstanceState);
2829
setContentView(R.layout.activity_gallery);
2930

30-
initDatas();
3131
mRv = (RecyclerView) findViewById(R.id.rv);
3232

33-
mAdapter = new CommonAdapter<TestBean>(this, mDatas, R.layout.item_flow) {
33+
mRv.setAdapter(mAdapter = new CommonAdapter<SwipeCardBean>(this, mDatas = initDatas(), R.layout.item_gallery) {
34+
public static final String TAG = "zxt/Adapter";
35+
3436
@Override
35-
public void convert(ViewHolder holder, TestBean testBean) {
36-
Log.d("zxt", "convert() called with: holder = [" + holder + "], testBean = [" + testBean + "]");
37-
holder.setText(R.id.tv, testBean.getName() + testBean.getUrl());
38-
holder.setOnClickListener(R.id.tv, new View.OnClickListener() {
39-
@Override
40-
public void onClick(View v) {
41-
Log.e("TAG1", "onClick() called with: v = [" + v + "]");
42-
}
43-
});
37+
public void convert(ViewHolder viewHolder, SwipeCardBean swipeCardBean) {
38+
Log.d(TAG, "convert() called with: viewHolder = [" + viewHolder + "], swipeCardBean = [" + swipeCardBean + "]");
39+
viewHolder.setText(R.id.tvName, swipeCardBean.getName());
40+
viewHolder.setText(R.id.tvPrecent, swipeCardBean.getPostition() + " /" + mDatas.size());
41+
Picasso.with(GalleryActivity.this).load(swipeCardBean.getUrl()).into((ImageView) viewHolder.getView(R.id.iv));
4442
}
45-
};
43+
});
4644

47-
//图片的Adapter
48-
final ImgAdapter imgAdapter = new ImgAdapter(this);
49-
mRv.setAdapter(mAdapter);
5045
mRv.setLayoutManager(new GalleryLayoutManager());
5146
SnapHelper snapHelper = new LinearSnapHelper();
52-
snapHelper.attachToRecyclerView(mRv);
47+
//snapHelper.attachToRecyclerView(mRv);
5348
}
5449

55-
private int i = 0;
5650

57-
public List<TestBean> initDatas() {
58-
mDatas = new ArrayList<>();
59-
for (int j = 0; j < 1; j++) {
60-
mDatas.add(new TestBean((i++) + " ", "张旭童"));
61-
mDatas.add(new TestBean((i++) + " ", "旭童"));
62-
mDatas.add(new TestBean((i++) + " ", "多种type"));
63-
mDatas.add(new TestBean((i++) + " ", "遍"));
64-
mDatas.add(new TestBean((i++) + " ", "多种type"));
65-
mDatas.add(new TestBean((i++) + " ", "多种type"));
66-
mDatas.add(new TestBean((i++) + " ", "多种type"));
67-
mDatas.add(new TestBean((i++) + " ", "多种type"));
68-
}
69-
return mDatas;
70-
}
7151
}

app/src/main/java/com/mcxtzhang/flowlayoutmanager/gallary/GalleryLayoutManager.java

+45
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public int fill(RecyclerView.Recycler recycler, RecyclerView.State state, int dx
8383
//step 1 :回收越界子View
8484
recycleHideViews(recycler, state, dx);
8585

86+
//为了能给每个childView做动画,所以这里要暂时全部把他们干掉
87+
//detachAndScrapAttachedViews(recycler);
88+
89+
//为了能给每个childView做动画
90+
for (int i = 0; i < getChildCount(); i++) {
91+
View child = getChildAt(i);
92+
changeViewUIProperty(dx, child);
93+
}
94+
95+
8696
//Step2. layout right views
8797
int itemCount = getItemCount();
8898
if (dx >= 0) {
@@ -99,6 +109,7 @@ public int fill(RecyclerView.Recycler recycler, RecyclerView.State state, int dx
99109
left = getNextViewLeft(child);
100110
}
101111

112+
102113
for (int i = startPos; i < itemCount; i++) {
103114
//如果左边界已经大于屏幕可见
104115
if (left > getWidth() - getPaddingRight()) {
@@ -107,8 +118,13 @@ public int fill(RecyclerView.Recycler recycler, RecyclerView.State state, int dx
107118

108119
child = recycler.getViewForPosition(i);
109120
addView(child);
121+
110122
//measure 还是需要的
111123
measureChildWithMargins(child, 0, 0);
124+
125+
changeViewUIProperty(dx, child);
126+
127+
112128
/*
113129
int width = getDecoratedMeasuredWidth(child);
114130
int height = getDecoratedMeasuredHeight(child);*/
@@ -133,7 +149,11 @@ public int fill(RecyclerView.Recycler recycler, RecyclerView.State state, int dx
133149
leftChild = recycler.getViewForPosition(pos);
134150
//这里是重点重点重点!!!作者每次在这里都踩坑,
135151
addView(leftChild, 0);
152+
136153
measureChildWithMargins(leftChild, 0, 0);
154+
155+
changeViewUIProperty(dx, leftChild);
156+
137157
layoutDecoratedWithMargins(leftChild, right - mChildWidth, top
138158
, right, top + mChildHeight);
139159
right -= mChildWidth;
@@ -146,6 +166,31 @@ public int fill(RecyclerView.Recycler recycler, RecyclerView.State state, int dx
146166
return dx;
147167
}
148168

169+
private float mFraction;
170+
171+
/**
172+
* 根据滑动值改变View的UI状态
173+
*
174+
* @param dx
175+
* @param child
176+
*/
177+
private void changeViewUIProperty(int dx, View child) {
178+
/* float fraction = dx * 1.0f / 45;
179+
if (fraction > 1) {
180+
fraction = 1;
181+
}
182+
mFraction = Math.max(mFraction, fraction);
183+
child.animate().rotationY(mFraction * 15).setDuration(100).start();*/
184+
int parentMiddle = getPaddingLeft() + getWidth() / 2;
185+
int childMiddle = (int) (child.getX() + child.getWidth() / 2);
186+
int distance = parentMiddle - childMiddle;
187+
float fraction = distance * 1.0f / getWidth() / 2;
188+
189+
child.setRotationY(45 * fraction);
190+
child.setAlpha(0.5f * (1-fraction)+0.5f);
191+
192+
}
193+
149194
/**
150195
* 根据dx 回收界面不可见的View
151196
*
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="270dp"
7+
android:layout_height="330dp"
8+
android:layout_margin="10dp"
9+
android:background="#393F4E">
10+
11+
<ImageView
12+
android:id="@+id/iv"
13+
android:layout_width="170dp"
14+
android:layout_height="200dp"
15+
android:layout_gravity="center_horizontal"
16+
android:layout_marginTop="10dp"/>
17+
18+
<TextView
19+
android:id="@+id/tvName"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_gravity="center_horizontal"
23+
android:layout_marginTop="210dp"
24+
android:paddingTop="10dp"
25+
android:textColor="@android:color/white"
26+
tools:text="怪气无语 第一季"/>
27+
28+
<Button
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_gravity="bottom|center_horizontal"
32+
android:layout_marginBottom="20dp"
33+
android:background="#F05079"
34+
android:text="订阅"/>
35+
36+
<TextView
37+
android:id="@+id/tvPrecent"
38+
android:layout_width="wrap_content"
39+
android:layout_height="wrap_content"
40+
android:layout_gravity="right|bottom"
41+
android:layout_margin="3dp"
42+
android:textColor="@android:color/white"
43+
tools:text="1/13"/>
44+
45+
</FrameLayout>

0 commit comments

Comments
 (0)