Skip to content

Commit

Permalink
DEMO、README
Browse files Browse the repository at this point in the history
  • Loading branch information
BCsl committed Mar 24, 2017
1 parent fd66ce5 commit 9f6cbd6
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 47 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@

[中文](./README_CN.md)

A custom LayoutManager to build a Gallery or a ViewPager like RecyclerView and support both HORIZONTAL and VERTICAL scroll.And View Recycle Machine is also supported.
A custom LayoutManager to build a Gallery or a ViewPager like RecyclerView that shows items in a center-locked and support both HORIZONTAL and VERTICAL scroll.And View Recycle Machine is also supported.

## Screenshots

![ViewPager](./screenshots/ViewPager.gif)

![Demo](./screenshots/demo.gif)

## Usage

### 1、Build

#### Gradle

```java
compile 'github.hellocsl:GalleryLayoutManager:1.0.2'
compile 'github.hellocsl:GalleryLayoutManager:1.0.3'
```

> Be care :if you have used `RecyclerView` in your project , maybe your should use this library as below and your recyclerview-v7 requires API level 24.2.0 or higher
```java
compile ('github.hellocsl:GalleryLayoutManager:1.0.1'){
compile ('github.hellocsl:GalleryLayoutManager:1.0.3'){
exclude group: 'com.android.support', module:'recyclerview-v7'
}
```
Expand Down
8 changes: 6 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@

## 效果

![ViewPager](./screenshots/ViewPager.gif)

![Demo](./screenshots/demo.gif)

## 使用方法

### 1、添加依赖

#### Gradle

```java
compile 'github.hellocsl:GalleryLayoutManager:1.0.2'
compile 'github.hellocsl:GalleryLayoutManager:1.0.3'
```

> 注意:如果你的项目已经引用了 `RecyclerView` ,那么应该这样引用,而且你的 `RecyclerView` 的引用版本必须大于 24.2.0
```java
compile ('github.hellocsl:GalleryLayoutManager:1.0.1'){
compile ('github.hellocsl:GalleryLayoutManager:1.0.3'){
exclude group: 'com.android.support', module:'recyclerview-v7'
}
```
Expand Down
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ dependencies {

compile deps.butterKnife
annotationProcessor deps.butterKnifeProcessor
compile ('github.hellocsl:GalleryLayoutManager:1.0.3'){
exclude group: 'com.android.support', module:'recyclerview-v7'
}

// compile project(path: ':library')

compile project(path: ':library')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import butterknife.ButterKnife;
Expand Down Expand Up @@ -51,7 +50,6 @@ private void showFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction()
.add(R.id.activity_main, fragment)
.hide(mMainFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(fragment.getClass().getSimpleName())
.commitAllowingStateLoss();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package github.hellocsl.gallerylayoutmanager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import github.hellocsl.gallerylayoutmanager.adapter.DemoAdapter;
import github.hellocsl.gallerylayoutmanager.layout.impl.ScaleTransformer;
import github.hellocsl.layoutmanager.gallery.GalleryLayoutManager;

/**
* Created by chensuilun on 2017/3/24.
*/

public class TestActivity extends AppCompatActivity {
private static final String TAG = "TestActivity";
@BindView(R.id.main_recycle1)
RecyclerView mMainRecycle1;
@BindView(R.id.main_tv_recycle_info_1)
TextView mMainTvRecycleInfo1;
@BindView(R.id.main_recycle2)
RecyclerView mMainRecycle2;
@BindView(R.id.main_tv_recycle_info_2)
TextView mMainTvRecycleInfo2;
@BindView(R.id.main_tv_recycle_info_3)
TextView mMainTvRecycleInfo3;
@BindView(R.id.main_btn_random)
Button mMainBtnRandom;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_test);
ButterKnife.bind(this);
initView();
}

private void initView() {
final List<String> title = new ArrayList<String>();
int size = 50;
for (int i = 0; i < size; i++) {
title.add("Hello" + i);
}
GalleryLayoutManager layoutManager1 = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
layoutManager1.attach(mMainRecycle1, 30);
layoutManager1.setItemTransformer(new ScaleTransformer());
DemoAdapter demoAdapter1 = new DemoAdapter(title) {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mMainTvRecycleInfo1.append("Create VH type:+" + viewType + "\n");
return super.onCreateViewHolder(parent, viewType);
}
};
demoAdapter1.setOnItemClickListener(new DemoAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
mMainRecycle1.smoothScrollToPosition(position);
}
});
mMainRecycle1.setAdapter(demoAdapter1);

final GalleryLayoutManager layoutManager2 = new GalleryLayoutManager(GalleryLayoutManager.VERTICAL);
layoutManager2.attach(mMainRecycle2, 20);
layoutManager2.setCallbackInFling(true);
layoutManager2.setOnItemSelectedListener(new GalleryLayoutManager.OnItemSelectedListener() {
@Override
public void onItemSelected(RecyclerView recyclerView, View item, int position) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onItemSelected: " + item.isSelected());
}
mMainTvRecycleInfo2.setText("selected:" + position + "\n");
}
});
DemoAdapter demoAdapter2 = new DemoAdapter(title, DemoAdapter.VIEW_TYPE_TEXT);
demoAdapter2.setOnItemClickListener(new DemoAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
mMainRecycle2.smoothScrollToPosition(position);
}
});
mMainRecycle2.setAdapter(demoAdapter2);
}

private final Random mRandom = new Random();

@OnClick(R.id.main_btn_random)
public void onClick() {
int selectPosition = mRandom.nextInt(50);
mMainRecycle1.smoothScrollToPosition(selectPosition);
mMainRecycle2.smoothScrollToPosition(selectPosition);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class TestFragment extends BaseRestoreFragment {
RecyclerView mMainRecycle2;
@BindView(R.id.main_tv_recycle_info_2)
TextView mMainTvRecycleInfo2;
@BindView(R.id.main_tv_recycle_info_3)
TextView mMainTvRecycleInfo3;
@BindView(R.id.main_btn_random)
Button mMainBtnRandom;

Expand Down Expand Up @@ -64,13 +66,13 @@ protected View onCreateContentView(LayoutInflater inflater, ViewGroup container,
@Override
protected void initView(View root, Bundle savedInstanceState) {
ButterKnife.bind(this, root);
List<String> title = new ArrayList<String>();
final List<String> title = new ArrayList<String>();
int size = 50;
for (int i = 0; i < size; i++) {
title.add("Hello" + i);
}
GalleryLayoutManager layoutManager1 = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
layoutManager1.attach(mMainRecycle1, 30);
layoutManager1.attach(mMainRecycle1, 0);
layoutManager1.setItemTransformer(new ScaleTransformer());
DemoAdapter demoAdapter1 = new DemoAdapter(title) {
@Override
Expand All @@ -91,11 +93,11 @@ public void onItemClick(View view, int position) {

final GalleryLayoutManager layoutManager2 = new GalleryLayoutManager(GalleryLayoutManager.VERTICAL);
layoutManager2.attach(mMainRecycle2, 20);
layoutManager2.setCallbackInFling(false);
layoutManager2.setCallbackInFling(true);
layoutManager2.setOnItemSelectedListener(new GalleryLayoutManager.OnItemSelectedListener() {
@Override
public void onItemSelected(RecyclerView recyclerView, View item, int position) {
mMainTvRecycleInfo2.append("selected:" + position + "\n");
mMainTvRecycleInfo2.setText("selected:" + position + "\n");
}
});
DemoAdapter demoAdapter2 = new DemoAdapter(title, DemoAdapter.VIEW_TYPE_TEXT);
Expand All @@ -116,11 +118,20 @@ protected void initData(Bundle savedInstanceState) {

private final Random mRandom = new Random();

@OnClick(R.id.main_btn_random)
public void onClick() {
int selectPosition = mRandom.nextInt(50);
mMainRecycle1.smoothScrollToPosition(selectPosition);
mMainRecycle2.smoothScrollToPosition(selectPosition);
@OnClick({R.id.main_btn_random, R.id.main_btn_data_change})
public void onClick(View view) {
if (view.getId() == R.id.main_btn_random) {
int selectPosition = mRandom.nextInt(50);
mMainRecycle1.smoothScrollToPosition(selectPosition);
mMainRecycle2.smoothScrollToPosition(selectPosition);
} else {
if (mMainRecycle1.getAdapter() instanceof DemoAdapter) {
((DemoAdapter) mMainRecycle1.getAdapter()).addData();
}
if (mMainRecycle2.getAdapter() instanceof DemoAdapter) {
((DemoAdapter) mMainRecycle2.getAdapter()).addData();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static ViewPagerFragment newInstance() {
* Use FragmentManager to find this Fragment's instance by tag
*/
public static ViewPagerFragment findFragment(FragmentManager manager) {
return (ViewPagerFragment)manager.findFragmentByTag(ViewPagerFragment.class.getSimpleName());
return (ViewPagerFragment) manager.findFragmentByTag(ViewPagerFragment.class.getSimpleName());
}

List<ImageCardAdapter.CardItem> mCardItems;
Expand Down Expand Up @@ -79,6 +79,7 @@ protected void initData(Bundle savedInstanceState) {
mPagerRecycleView.setFlingAble(false);
GalleryLayoutManager layoutManager = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
layoutManager.attach(mPagerRecycleView, 30);
// layoutManager.attach(mPagerRecycleView);
layoutManager.setOnItemSelectedListener(new GalleryLayoutManager.OnItemSelectedListener() {
@Override
public void onItemSelected(RecyclerView recyclerView, View item, int position) {
Expand All @@ -88,15 +89,15 @@ public void onItemSelected(RecyclerView recyclerView, View item, int position) {
});
layoutManager.setItemTransformer(new CurveTransformer());
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
ImageCardAdapter demoAdapter1 = new ImageCardAdapter(mCardItems, (int) (displayMetrics.widthPixels * 0.7f), (int) (displayMetrics.heightPixels * 0.8f));
demoAdapter1.setOnItemClickListener(new ImageCardAdapter.OnItemClickListener() {
ImageCardAdapter imageAdapter = new ImageCardAdapter(mCardItems, (int) (displayMetrics.widthPixels * 0.7f), (int) (displayMetrics.heightPixels * 0.8f));
imageAdapter.setOnItemClickListener(new ImageCardAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getContext(), "click" + mCardItems.get(position).mName, Toast.LENGTH_SHORT).show();
mPagerRecycleView.smoothScrollToPosition(position);
}
});
mPagerRecycleView.setAdapter(demoAdapter1);
mPagerRecycleView.setAdapter(imageAdapter);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.ViewHolder> im
public static final int VIEW_TYPE_IMAGE = 0;
public static final int VIEW_TYPE_TEXT = 1;
private static final String TAG = "DemoAdapter";
private List<String> items;
private List<String> mItems;
private int mType = VIEW_TYPE_IMAGE;
private OnItemClickListener mOnItemClickListener;

Expand All @@ -30,10 +30,19 @@ public DemoAdapter(List<String> items) {
}

public DemoAdapter(List<String> items, int type) {
this.items = items;
this.mItems = items;
mType = type;
}

public void addData() {
if (mItems != null) {
for (int i = 0; i < 10; i++) {
mItems.add("Extra:" + i);
}
notifyDataSetChanged();
}
}

public DemoAdapter setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
return this;
Expand Down Expand Up @@ -64,7 +73,7 @@ public void onBindViewHolder(ViewHolder holder, int position) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onBindViewHolder: position:" + position);
}
String item = items.get(position);
String item = mItems.get(position);
if (mType == VIEW_TYPE_TEXT) {
holder.text.setText(item);
} else {
Expand All @@ -75,7 +84,7 @@ public void onBindViewHolder(ViewHolder holder, int position) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package github.hellocsl.gallerylayoutmanager.layout.impl;

import android.util.Log;
import android.view.View;

import github.hellocsl.gallerylayoutmanager.BuildConfig;
import github.hellocsl.layoutmanager.gallery.GalleryLayoutManager;

/**
* Created by chensuilun on 2016/12/16.
*/
public class CurveTransformer implements GalleryLayoutManager.ItemTransformer {

public static final int ROTATE_ANGEL = 7;
private static final String TAG = "CurveTransformer";


@Override
public void transformItem(GalleryLayoutManager layoutManager, View item, float fraction) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "transformItem() called with: fraction = [" + fraction + "]");
}
if (layoutManager.getOrientation() == GalleryLayoutManager.VERTICAL) {
return;
}
item.setPivotX(item.getWidth() / 2.f);
item.setPivotY(item.getHeight());
int width = item.getWidth();
int height = item.getHeight();
item.setPivotX(width / 2.f);
item.setPivotY(height);
float scale = 1 - 0.1f * Math.abs(fraction);
item.setScaleX(scale);
item.setScaleY(scale);
item.setRotation(10 * fraction);
item.setTranslationY(30 * Math.abs(fraction));
item.setTranslationX(50 * -fraction);
item.setRotation(ROTATE_ANGEL * fraction);
item.setTranslationY((float) (Math.sin(2 * Math.PI * ROTATE_ANGEL * Math.abs(fraction) / 360.f) * width / 2.0f));
item.setTranslationX((float) ((1 - scale) * width / 2.0f / Math.cos(2 * Math.PI * ROTATE_ANGEL * fraction / 360.f)));
if (fraction > 0) {
item.setTranslationX(-item.getTranslationX());
}
item.setAlpha(1 - 0.2f * Math.abs(fraction));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ScaleTransformer implements GalleryLayoutManager.ItemTransformer {
public void transformItem(GalleryLayoutManager layoutManager, View item, float fraction) {
item.setPivotX(item.getWidth() / 2.f);
item.setPivotY(item.getHeight()/2.0f);
float scale = 1 - 0.3f * Math.abs(fraction);
float scale = 1 - 0.2f * Math.abs(fraction);
item.setScaleX(scale);
item.setScaleY(scale);
}
Expand Down
Loading

0 comments on commit 9f6cbd6

Please sign in to comment.