├── .gitignore
├── .idea
├── caches
│ └── build_file_checksums.ser
├── codeStyles
│ └── Project.xml
├── dictionaries
│ └── gavin.xml
├── gradle.xml
├── misc.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── view
│ │ └── gavin
│ │ └── com
│ │ └── flexibleview
│ │ ├── CoordinatorLayoutActivity.java
│ │ ├── MainActivity.java
│ │ ├── RecyclerViewActivity.java
│ │ ├── ScrollViewActivity.java
│ │ └── adapter
│ │ ├── AbRecyclerViewAdapter.java
│ │ ├── BaseRecyclerViewAdapter.java
│ │ ├── MoreItemHelper.java
│ │ └── SimpleListAdapter.java
│ └── res
│ ├── drawable-v24
│ └── ic_launcher_foreground.xml
│ ├── drawable
│ ├── header_background.jpg
│ └── ic_launcher_background.xml
│ ├── layout
│ ├── activity_coordinator_layout.xml
│ ├── activity_main.xml
│ ├── activity_recycler_view.xml
│ ├── activity_scroll_view.xml
│ ├── header.xml
│ ├── item_recycler_view.xml
│ └── refresh_layout.xml
│ ├── mipmap-anydpi-v26
│ ├── ic_launcher.xml
│ └── ic_launcher_round.xml
│ ├── mipmap-xxxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── dependencies.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── gavin
│ │ └── view
│ │ └── flexible
│ │ ├── FlexibleLayout.java
│ │ ├── IFlexible.java
│ │ ├── callback
│ │ ├── OnPullListener.java
│ │ ├── OnReadyPullListener.java
│ │ └── OnRefreshListener.java
│ │ └── util
│ │ └── PullAnimatorUtil.java
│ └── res
│ ├── drawable
│ └── flexible_loading.png
│ └── values
│ └── strings.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/FlexibleLayout/2185b81f1d72f6940377711b5aa816a5642f2231/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/.idea/dictionaries/gavin.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 可以下拉放大的Layout
2 |
3 | 
4 |
5 | ## 支持
6 | - 下拉刷新
7 | - ScrollView
8 | - RecyclerView
9 | - CoordinatorLayout
10 | (其他Layout需要处理改Layout的onTouchEvent事件,否则可能无法使用)
11 |
12 | ## 使用
13 |
14 | **依赖**
15 |
16 | ```gradle
17 | compile 'com.gavin.view.flexible:library:1.0.2'
18 | ```
19 |
20 | **xml**(ScrollView)
21 |
22 | ```xml
23 |
25 |
27 |
30 |
33 |
35 |
36 |
37 |
38 | ```
39 |
40 | **Activity**
41 |
42 | 下拉放大
43 |
44 | ```java
45 | private ImageView mHeader;
46 | private ScrollView mScrollView;
47 | private FlexibleLayout mFlexibleLayout;
48 | ...
49 | mFlexibleLayout.setHeader(mHeader)
50 | .setReadyListener(new OnReadyPullListener() {
51 | @Override
52 | public boolean isReady() {
53 | //下拉放大的条件
54 | return mScrollView.getScrollY() == 0;
55 | }
56 | });
57 | ```
58 |
59 | 下拉放大 + 刷新
60 |
61 | ```java
62 | mFlexibleLayout.setHeader(mHeader)
63 | .setReadyListener(new OnReadyPullListener() {
64 | @Override
65 | public boolean isReady() {
66 | return mScrollView.getScrollY() == 0;
67 | }
68 | })
69 | .setRefreshable(true)
70 | .setDefaultRefreshView(new OnRefreshListener() {
71 | @Override
72 | public void onRefreshing() {
73 | //刷新操作
74 | ...
75 | //刷新完成后需要调用onRefreshComplete()通知FlexibleLayout
76 | mFlexibleLayout.onRefreshComplete();
77 | }
78 | });
79 | ```
80 |
81 | ## 支持的方法
82 |
83 | **配置**
84 |
85 | |方法 | 功能 | 默认 |
86 | | - | - | - |
87 | | setEnable(boolean isEnable) | 允许下拉放大 | true |
88 | | setHeader(View header) | 设置Header | null |
89 | | setMaxPullHeight(int height) | Header最大下拉高度 | header 高度 + 1/3屏幕宽度 |
90 | | setRefreshable(boolean isEnable) | 是否允许下拉刷新 | false |
91 | | setMaxRefreshPullHeight(int height) | 刷新View最大下拉高度 | 1/3屏幕宽度 |
92 | | setRefreshSize(int size) | 刷新View的尺寸(正方形)| 1/15屏幕宽度 |
93 | | isRefreshing() | 是否正在刷新| |
94 |
95 |
96 | **监听**
97 |
98 | |方法 | 功能 |
99 | | - | - |
100 | | setReadyListener(OnReadyPullListener listener) | 设置准备监听 |
101 | | setOnPullListener(OnPullListener onPullListener) | 下拉监听 |
102 | | setRefreshView(View refreshView, OnRefreshListener listener) | 设置下拉刷新View 以及监听 |
103 | | setDefaultRefreshView(OnRefreshListener listener) | 使用默认的下拉刷新View |
104 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.compileSdkVersion
5 | buildToolsVersion rootProject.ext.buildToolsVersion
6 |
7 | defaultConfig {
8 | minSdkVersion rootProject.ext.minSdkVersion
9 | targetSdkVersion rootProject.ext.targetSdkVersion
10 | versionCode rootProject.ext.versionCode
11 | versionName rootProject.ext.versionName
12 |
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile rootProject.ext.dependencies['appcompat-v7']
24 | compile project(':library')
25 | compile rootProject.ext.dependencies['recyclerview-v7']
26 | compile rootProject.ext.dependencies['design']
27 | }
28 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/view/gavin/com/flexibleview/CoordinatorLayoutActivity.java:
--------------------------------------------------------------------------------
1 | package view.gavin.com.flexibleview;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.support.v7.widget.LinearLayoutManager;
7 | import android.support.v7.widget.RecyclerView;
8 | import android.view.View;
9 |
10 | import com.gavin.view.flexible.FlexibleLayout;
11 | import com.gavin.view.flexible.callback.OnReadyPullListener;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | import view.gavin.com.flexibleview.adapter.SimpleListAdapter;
17 |
18 | /**
19 | * Created by gavin
20 | * date 2018/6/18
21 | */
22 | public class CoordinatorLayoutActivity extends AppCompatActivity {
23 | @Override
24 | protected void onCreate(@Nullable Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_coordinator_layout);
27 |
28 | View header = findViewById(R.id.iv);
29 | RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
30 |
31 | List list = new ArrayList<>();
32 | for (int i = 0; i < 20; i++) {
33 | list.add("Item : " + i);
34 | }
35 | SimpleListAdapter adapter = new SimpleListAdapter(this, list);
36 | final LinearLayoutManager manager = new LinearLayoutManager(this);
37 | recyclerView.setLayoutManager(manager);
38 | recyclerView.setAdapter(adapter);
39 |
40 |
41 | FlexibleLayout flexibleLayout = (FlexibleLayout) findViewById(R.id.fv);
42 | flexibleLayout.setReadyListener(new OnReadyPullListener() {
43 | @Override
44 | public boolean isReady() {
45 | return manager.findFirstCompletelyVisibleItemPosition() == 0;
46 | }
47 | });
48 | flexibleLayout.setHeader(header);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/view/gavin/com/flexibleview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package view.gavin.com.flexibleview;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.view.View;
8 |
9 | /**
10 | * Created by gavin
11 | * date 2018/6/12
12 | */
13 | public class MainActivity extends AppCompatActivity {
14 | @Override
15 | public void onCreate(@Nullable Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_main);
18 |
19 | }
20 |
21 | public void toScrollView(View view) {
22 | Intent intent = new Intent(this, ScrollViewActivity.class);
23 | startActivity(intent);
24 | }
25 |
26 | public void toRecyclerView(View view) {
27 | Intent intent = new Intent(this, RecyclerViewActivity.class);
28 | startActivity(intent);
29 | }
30 |
31 | public void toCoordinatorLayout(View view) {
32 | Intent intent = new Intent(this, CoordinatorLayoutActivity.class);
33 | startActivity(intent);
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/app/src/main/java/view/gavin/com/flexibleview/RecyclerViewActivity.java:
--------------------------------------------------------------------------------
1 | package view.gavin.com.flexibleview;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.support.v7.widget.LinearLayoutManager;
7 | import android.support.v7.widget.RecyclerView;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 |
11 | import com.gavin.view.flexible.FlexibleLayout;
12 | import com.gavin.view.flexible.callback.OnReadyPullListener;
13 |
14 | import java.util.ArrayList;
15 | import java.util.List;
16 |
17 | import view.gavin.com.flexibleview.adapter.SimpleListAdapter;
18 |
19 | /**
20 | * Created by gavin
21 | * date 2018/6/18
22 | */
23 | public class RecyclerViewActivity extends AppCompatActivity {
24 |
25 | @Override
26 | protected void onCreate(@Nullable Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_recycler_view);
29 | RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
30 | View header = LayoutInflater.from(this).inflate(R.layout.header, null);
31 | View headerImage = header.findViewById(R.id.iv);
32 |
33 | List list = new ArrayList<>();
34 | for (int i = 0; i < 20; i++) {
35 | list.add("Item : " + i);
36 | }
37 | SimpleListAdapter adapter = new SimpleListAdapter(this, list);
38 | adapter.addHeaderView(header);
39 | final LinearLayoutManager manager = new LinearLayoutManager(this);
40 | recyclerView.setLayoutManager(manager);
41 | recyclerView.setAdapter(adapter);
42 |
43 |
44 | FlexibleLayout flexibleLayout = (FlexibleLayout) findViewById(R.id.fv);
45 | flexibleLayout.setReadyListener(new OnReadyPullListener() {
46 | @Override
47 | public boolean isReady() {
48 | return manager.findFirstCompletelyVisibleItemPosition() == 0;
49 | }
50 | });
51 | flexibleLayout.setHeader(headerImage);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/app/src/main/java/view/gavin/com/flexibleview/ScrollViewActivity.java:
--------------------------------------------------------------------------------
1 | package view.gavin.com.flexibleview;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.widget.ScrollView;
9 |
10 | import com.gavin.view.flexible.FlexibleLayout;
11 | import com.gavin.view.flexible.callback.OnReadyPullListener;
12 | import com.gavin.view.flexible.callback.OnRefreshListener;
13 |
14 | /**
15 | * Created by gavin
16 | * date 2018/6/18
17 | */
18 | public class ScrollViewActivity extends AppCompatActivity {
19 |
20 | private View mHeader;
21 | private ScrollView mScrollView;
22 | private FlexibleLayout mFlexibleLayout;
23 | private View mRefreshView;
24 |
25 | @Override
26 | public void onCreate(@Nullable Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_scroll_view);
29 | initView();
30 | mFlexibleLayout.setHeader(mHeader)
31 | .setReadyListener(new OnReadyPullListener() {
32 | @Override
33 | public boolean isReady() {
34 | return mScrollView.getScrollY() == 0;
35 | }
36 | })
37 | .setRefreshable(true)
38 | .setDefaultRefreshView(new OnRefreshListener() {
39 | @Override
40 | public void onRefreshing() {
41 | new Thread(new Runnable() {
42 | @Override
43 | public void run() {
44 | try {
45 | Thread.sleep(2000);
46 | } catch (InterruptedException e) {
47 | e.printStackTrace();
48 | }
49 | //刷新完成后需要调用onRefreshComplete()通知FlexibleLayout
50 | runOnUiThread(new Runnable() {
51 | @Override
52 | public void run() {
53 | mFlexibleLayout.onRefreshComplete();
54 | }
55 | });
56 | }
57 | }).start();
58 | }
59 | });
60 | }
61 |
62 | private void initView() {
63 | mFlexibleLayout = (FlexibleLayout) findViewById(R.id.ffv);
64 | mHeader = findViewById(R.id.header);
65 | mScrollView = (ScrollView) findViewById(R.id.sv);
66 | mRefreshView = LayoutInflater.from(this).inflate(R.layout.refresh_layout, null);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/app/src/main/java/view/gavin/com/flexibleview/adapter/AbRecyclerViewAdapter.java:
--------------------------------------------------------------------------------
1 | package view.gavin.com.flexibleview.adapter;
2 |
3 | import android.support.v4.widget.SwipeRefreshLayout;
4 | import android.support.v7.widget.LinearLayoutManager;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.support.v7.widget.StaggeredGridLayoutManager;
7 |
8 | /**
9 | * Created by dazhuanjia_rx on 16/9/8.
10 | */
11 | public abstract class AbRecyclerViewAdapter extends RecyclerView.Adapter {
12 |
13 | protected boolean isLoading = false; //是否在加载更多
14 | protected boolean mLoadMoreEnable = false; //加载更多功能是否打开
15 | private int lastVisibleItem;
16 |
17 | private void attachRecyclerView(RecyclerView recyclerView) {
18 | if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
19 | final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
20 | recyclerView.addOnScrollListener(
21 | new RecyclerView.OnScrollListener() {
22 | @Override
23 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
24 | super.onScrolled(recyclerView, dx, dy);
25 | lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
26 | }
27 |
28 | @Override
29 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
30 | super.onScrollStateChanged(recyclerView, newState);
31 | if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) { //正在刷新时不加载更多
32 | return;
33 | }
34 | if (newState == RecyclerView.SCROLL_STATE_IDLE
35 | && lastVisibleItem + 1 == getItemCount() && !isLoading && mLoadMoreEnable && canLoadMore()) {
36 | isLoading = true;
37 | }
38 | }
39 | }
40 | );
41 | } else if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
42 | final StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
43 | recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
44 | @Override
45 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
46 | super.onScrolled(recyclerView, dx, dy);
47 | int[] into = new int[staggeredGridLayoutManager.getSpanCount()];
48 | (staggeredGridLayoutManager).findLastVisibleItemPositions(into);
49 | lastVisibleItem = findMax(into);
50 |
51 | }
52 |
53 | @Override
54 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
55 | super.onScrollStateChanged(recyclerView, newState);
56 | if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) { //正在刷新时不加载更多
57 | return;
58 | }
59 | if (newState == RecyclerView.SCROLL_STATE_IDLE
60 | && lastVisibleItem + 1 == getItemCount() && !isLoading && mLoadMoreEnable && canLoadMore()) {
61 | isLoading = true;
62 | }
63 | }
64 | });
65 | }
66 | }
67 | /**
68 | * 获取数组中的最大值
69 | *
70 | * @param lastPositions 需要找到最大值的数组
71 | * @return 数组中的最大值
72 | */
73 | private int findMax(int[] lastPositions) {
74 | int max = lastPositions[0];
75 | for (int value : lastPositions) {
76 | if (value > max) {
77 | max = value;
78 | }
79 | }
80 | return max;
81 | }
82 |
83 | protected boolean canLoadMore() {
84 | return true;
85 | }
86 |
87 | /**
88 | * 是否正在加载更多
89 | *
90 | * @return
91 | */
92 | public boolean isLoadMoring() {
93 | return isLoading;
94 | }
95 |
96 | /**
97 | * @param mLoadMoreEnable 是否可以加载更多
98 | */
99 | public void enableLoadMore(boolean mLoadMoreEnable) {
100 | this.mLoadMoreEnable = mLoadMoreEnable;
101 | }
102 |
103 | protected SwipeRefreshLayout swipeRefreshLayout;
104 |
105 | public void attachSwipeRefreshLayout(SwipeRefreshLayout swipeRefreshLayout) {
106 | this.swipeRefreshLayout = swipeRefreshLayout;
107 | }
108 |
109 | /**
110 | * 是否在刷新或加载更多
111 | *
112 | * @return
113 | */
114 | public boolean isRefreshOrLoadMoring() {
115 | return isLoadMoring() || isRefreshing();
116 | }
117 |
118 | /**
119 | * 是否在刷新
120 | *
121 | * @return
122 | */
123 | public boolean isRefreshing() {
124 | return swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing();
125 | }
126 |
127 | /**
128 | * 刷新完成
129 | */
130 | public void refreshComplete() {
131 | if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
132 | swipeRefreshLayout.setRefreshing(false);
133 | }
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/app/src/main/java/view/gavin/com/flexibleview/adapter/BaseRecyclerViewAdapter.java:
--------------------------------------------------------------------------------
1 | package view.gavin.com.flexibleview.adapter;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.LayoutRes;
5 | import android.support.annotation.NonNull;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.widget.LinearLayout;
11 | import android.widget.ProgressBar;
12 | import android.widget.TextView;
13 |
14 | import java.util.HashMap;
15 | import java.util.List;
16 | import java.util.Set;
17 |
18 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
19 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
20 |
21 | /**
22 | * Created by dazhuanjia_rx on 16/8/31.
23 | */
24 | public abstract class BaseRecyclerViewAdapter extends AbRecyclerViewAdapter {
25 | public Context context;
26 | public List list;
27 |
28 | private static final int HEADER_VIEW = 0x00000111;
29 | private static final int LOADING_VIEW = 0x00000222;
30 | private static final int FOOTER_VIEW = 0x00000333;
31 | private static final int ITEM_VIEW = 0x00000444;
32 |
33 | // 缓存对应 viewType 的 item
34 | private HashMap moreItemHelperSparseArray = new HashMap<>();
35 |
36 | public BaseRecyclerViewAdapter(Context context, @NonNull List list) {
37 | this.context = context;
38 | this.list = list;
39 | }
40 |
41 | /**
42 | * 在构造方法里调用
43 | *
44 | * @param moreItemHelper
45 | */
46 | protected void addMoreItemHelper(MoreItemHelper moreItemHelper) {
47 | moreItemHelperSparseArray.put(moreItemHelper.getItemType(), moreItemHelper);
48 | }
49 |
50 | @Override
51 | final public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
52 | View view;
53 | switch (viewType) {
54 | case HEADER_VIEW:
55 | return new HeadViewHolder(mHeaderLayout);
56 | case FOOTER_VIEW:
57 | return new FootViewHolder(mFooterLayout);
58 | default:
59 | if (moreItemHelperSparseArray.size() != 0) {
60 | MoreItemHelper moreItemHelper = moreItemHelperSparseArray.get(viewType);
61 | return moreItemHelper.createViewHolder(context, parent);
62 | } else {
63 | view = LayoutInflater.from(context).inflate(getLayoutId(), parent, false);
64 | return getViewHolder(view);
65 | }
66 | }
67 | }
68 |
69 | @LayoutRes
70 | protected abstract int getLayoutId();
71 |
72 | @NonNull
73 | protected abstract RecyclerView.ViewHolder getViewHolder(View view);
74 |
75 | @Override
76 | final public int getItemCount() {
77 | int listSize = getItemSize();
78 | return listSize + getHeaderLayoutCount() + getFooterLayoutCount() + getLoadMoreViewCount();
79 | }
80 |
81 | /**
82 | * 返回item个数 除了head footer loadMoreView
83 | * 子类可重写
84 | *
85 | * @return
86 | */
87 | public int getItemSize() {
88 | if (moreItemHelperSparseArray.size() != 0) {
89 | int count = 0;
90 | Set integers = moreItemHelperSparseArray.keySet();
91 | for (Integer viewType : integers) {
92 | MoreItemHelper moreItemHelper = moreItemHelperSparseArray.get(viewType);
93 | count = count + moreItemHelper.getItemCount();
94 | }
95 | return count;
96 | }
97 | return list.size();
98 | }
99 |
100 | @Override
101 | public int getItemViewType(int position) {
102 | int numHeaders = getHeaderLayoutCount();
103 | if (position < numHeaders) {
104 | return HEADER_VIEW;
105 | } else {
106 | int adjPosition = position - numHeaders;
107 | int adapterCount = getItemSize();
108 | if (adjPosition < adapterCount) {
109 | if (moreItemHelperSparseArray.size() != 0) {
110 | return getMoreItemHelperViewType(adjPosition);
111 | } else {
112 | return ITEM_VIEW;
113 | }
114 | } else {
115 | adjPosition = adjPosition - adapterCount;
116 | int numFooters = getFooterLayoutCount();
117 | if (adjPosition < numFooters) {
118 | return FOOTER_VIEW;
119 | } else {
120 | return LOADING_VIEW;
121 | }
122 | }
123 | }
124 | }
125 |
126 | /**
127 | * 返回对应 position 的viewType
128 | *
129 | * @param position
130 | * @return
131 | */
132 | protected int getMoreItemHelperViewType(int position) {
133 | return 0;
134 | }
135 |
136 | @Override
137 | final public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
138 | int viewType = holder.getItemViewType();
139 | switch (viewType) {
140 | case 0:
141 | onBindView(holder, holder.getLayoutPosition() - getHeaderLayoutCount());
142 | break;
143 | case LOADING_VIEW:
144 | setLoadMoreView((LoadMoreViewHolder) holder);
145 | break;
146 | case HEADER_VIEW:
147 | break;
148 | case FOOTER_VIEW:
149 | break;
150 | default:
151 | if (moreItemHelperSparseArray.size() != 0) {
152 | moreItemHelperSparseArray.get(viewType).onBindView(holder, getRealPosition(viewType, holder.getLayoutPosition() - getHeaderLayoutCount()));
153 | } else {
154 | onBindView(holder, holder.getLayoutPosition() - getHeaderLayoutCount());
155 | }
156 | break;
157 | }
158 | }
159 |
160 | /**
161 | * 多 item 布局需要重写该方法拿到对应的 position
162 | *
163 | * @param viewType
164 | * @param position
165 | * @return
166 | */
167 | protected int getRealPosition(int viewType, int position) {
168 | return position;
169 | }
170 |
171 | protected abstract void onBindView(RecyclerView.ViewHolder holder, int position);
172 |
173 | /**
174 | * @param offset
175 | * @param limit
176 | * @param newList
177 | * @return true 代表有数据 false代表无数据
178 | */
179 | public boolean updateList(int offset, int limit, List newList) {
180 | if (offset == 0) {
181 | list.clear();
182 | notifyDataSetChanged();
183 | resetLoadMoreStatus();
184 | }
185 | if (newList != null && newList.size() != 0) {
186 | int size = list.size();
187 | list.addAll(newList);
188 | notifyItemRangeInserted(size + getHeaderLayoutCount(), newList.size());
189 | notifyItemRangeChanged(size + getHeaderLayoutCount(), newList.size());
190 | if (newList.size() < limit) {
191 | loadMoreEnd();
192 | } else {
193 | loadMoreComplete();
194 | }
195 | return true;
196 | } else { //没有新数据关闭加载更多
197 | loadMoreEnd();
198 | return list.size() != 0;
199 | }
200 | }
201 |
202 | /**
203 | * 移除item
204 | *
205 | * @param position
206 | * @return true: list empty ; false: list size more than 0
207 | */
208 | public boolean removeItem(int position) {
209 | if (list.size() > position) {
210 | list.remove(position);
211 | if (list.size() == 0) {
212 | notifyDataSetChanged();
213 | return true;
214 | } else {
215 | notifyItemRemoved(position);
216 | if (position != list.size()) {
217 | notifyItemRangeChanged(position, list.size() - position);
218 | }
219 | }
220 | }
221 | return false;
222 | }
223 |
224 | //header footer
225 | private LinearLayout mHeaderLayout;
226 | private LinearLayout mFooterLayout;
227 |
228 | /**
229 | * Return root layout of header
230 | */
231 | public LinearLayout getHeaderLayout() {
232 | return mHeaderLayout;
233 | }
234 |
235 | /**
236 | * Return root layout of footer
237 | */
238 | public LinearLayout getFooterLayout() {
239 | return mFooterLayout;
240 | }
241 |
242 | /**
243 | * Append header to the rear of the mHeaderLayout.
244 | *
245 | * @param header
246 | */
247 | public int addHeaderView(View header) {
248 | return addHeaderView(header, -1);
249 | }
250 |
251 | /**
252 | * Add header view to mHeaderLayout and set header view position in mHeaderLayout.
253 | * When index = -1 or index >= child count in mHeaderLayout,
254 | * the effect of this method is the same as that of {@link #addHeaderView(View)}.
255 | *
256 | * @param header
257 | * @param index the position in mHeaderLayout of this header.
258 | * When index = -1 or index >= child count in mHeaderLayout,
259 | * the effect of this method is the same as that of {@link #addHeaderView(View)}.
260 | */
261 | public int addHeaderView(View header, int index) {
262 | return addHeaderView(header, index, LinearLayout.VERTICAL);
263 | }
264 |
265 | /**
266 | * @param header
267 | * @param index
268 | * @param orientation
269 | */
270 | public int addHeaderView(View header, int index, int orientation) {
271 | if (mHeaderLayout == null) {
272 | mHeaderLayout = new LinearLayout(header.getContext());
273 | if (orientation == LinearLayout.VERTICAL) {
274 | mHeaderLayout.setOrientation(LinearLayout.VERTICAL);
275 | mHeaderLayout.setLayoutParams(new RecyclerView.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
276 | } else {
277 | mHeaderLayout.setOrientation(LinearLayout.HORIZONTAL);
278 | mHeaderLayout.setLayoutParams(new RecyclerView.LayoutParams(WRAP_CONTENT, MATCH_PARENT));
279 | }
280 | }
281 | final int childCount = mHeaderLayout.getChildCount();
282 | if (index < 0 || index > childCount) {
283 | index = childCount;
284 | }
285 | mHeaderLayout.addView(header, index);
286 | if (mHeaderLayout.getChildCount() == 1) {
287 | notifyItemInserted(0);
288 | }
289 | return index;
290 | }
291 |
292 | public void removeHeaderView(View header) {
293 | if (getHeaderLayoutCount() == 0) {
294 | return;
295 | }
296 | mHeaderLayout.removeView(header);
297 | if (mHeaderLayout.getChildCount() == 0) {
298 | notifyItemRemoved(0);
299 | }
300 | }
301 |
302 | /**
303 | * Append footer to the rear of the mFooterLayout.
304 | *
305 | * @param footer
306 | */
307 | public int addFooterView(View footer) {
308 | return addFooterView(footer, -1, LinearLayout.VERTICAL);
309 | }
310 |
311 | public int addFooterView(View footer, int index) {
312 | return addFooterView(footer, index, LinearLayout.VERTICAL);
313 | }
314 |
315 | /**
316 | * Add footer view to mFooterLayout and set footer view position in mFooterLayout.
317 | * When index = -1 or index >= child count in mFooterLayout,
318 | * the effect of this method is the same as that of {@link #addFooterView(View)}.
319 | *
320 | * @param footer
321 | * @param index the position in mFooterLayout of this footer.
322 | * When index = -1 or index >= child count in mFooterLayout,
323 | * the effect of this method is the same as that of {@link #addFooterView(View)}.
324 | */
325 | public int addFooterView(View footer, int index, int orientation) {
326 | if (mFooterLayout == null) {
327 | mFooterLayout = new LinearLayout(footer.getContext());
328 | if (orientation == LinearLayout.VERTICAL) {
329 | mFooterLayout.setOrientation(LinearLayout.VERTICAL);
330 | mFooterLayout.setLayoutParams(new RecyclerView.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
331 | } else {
332 | mFooterLayout.setOrientation(LinearLayout.HORIZONTAL);
333 | mFooterLayout.setLayoutParams(new RecyclerView.LayoutParams(WRAP_CONTENT, MATCH_PARENT));
334 | }
335 | }
336 | final int childCount = mFooterLayout.getChildCount();
337 | if (index < 0 || index > childCount) {
338 | index = childCount;
339 | }
340 | mFooterLayout.addView(footer, index);
341 | if (mFooterLayout.getChildCount() == 1) {
342 | int position = getHeaderLayoutCount() + getItemSize();
343 | if (position != -1) {
344 | notifyItemInserted(position);
345 | }
346 | }
347 | return index;
348 | }
349 |
350 | public void removeFooterView(View footer) {
351 | if (getFooterLayoutCount() == 0) return;
352 |
353 | mFooterLayout.removeView(footer);
354 | if (mFooterLayout.getChildCount() == 0) {
355 | int position = getHeaderLayoutCount() + getItemSize();
356 | if (position != -1) {
357 | notifyItemRemoved(position);
358 | }
359 | }
360 | }
361 |
362 | /**
363 | * if addHeaderView will be return 1, if not will be return 0
364 | */
365 | public int getHeaderLayoutCount() {
366 | if (mHeaderLayout == null || mHeaderLayout.getChildCount() == 0) {
367 | return 0;
368 | }
369 | return 1;
370 | }
371 |
372 | /**
373 | * if addFooterView will be return 1, if not will be return 0
374 | */
375 | public int getFooterLayoutCount() {
376 | if (mFooterLayout == null || mFooterLayout.getChildCount() == 0) {
377 | return 0;
378 | }
379 | return 1;
380 | }
381 |
382 | /**
383 | * Load more view count
384 | *
385 | * @return 0 or 1
386 | */
387 | public int getLoadMoreViewCount() {
388 | if (!mLoadMoreEnable) {
389 | return 0;
390 | }
391 | if (loadMoreStatus == LOAD_MORE_NO_MORE) {
392 | return 0;
393 | }
394 | if (getItemSize() == 0) {
395 | return 0;
396 | }
397 | return 1;
398 | }
399 |
400 | public boolean isHeadLayout(int position) {
401 | return getItemViewType(position) == HEADER_VIEW;
402 | }
403 |
404 | public boolean isLoadMoreLayout(int position) {
405 | return getItemViewType(position) == LOADING_VIEW;
406 | }
407 |
408 | public boolean isFooterLayout(int position) {
409 | return getItemViewType(position) == FOOTER_VIEW;
410 | }
411 |
412 | /**
413 | * 加载更多完成
414 | */
415 | public void loadMoreComplete() {
416 | setLoadMoreStatus(LOAD_MORE_NORMAL);
417 | }
418 |
419 | /**
420 | * 加载更多失败
421 | */
422 | public void loadMoreFail() {
423 | setLoadMoreStatus(LOAD_MORE_FAIL);
424 | }
425 |
426 | /**
427 | * 加载到底部了
428 | */
429 | public void loadMoreEnd() {
430 | setLoadMoreStatus(LOAD_MORE_NO_MORE);
431 | }
432 |
433 | public void setLoadMoreStatus(int status) {
434 | if (getLoadMoreViewCount() == 0) {
435 | return;
436 | }
437 | isLoading = false;
438 | loadMoreStatus = status;
439 | notifyItemChanged(getHeaderLayoutCount() + getItemSize() + getFooterLayoutCount());
440 | }
441 |
442 | public void resetLoadMoreStatus() {
443 | loadMoreStatus = LOAD_MORE_NORMAL;
444 | }
445 |
446 | @Override
447 | protected boolean canLoadMore() {
448 | return loadMoreStatus == LOAD_MORE_NORMAL;
449 | }
450 |
451 | public static final int LOAD_MORE_NORMAL = 0; //正常情况
452 | public static final int LOAD_MORE_FAIL = 1; //失败
453 | public static final int LOAD_MORE_NO_MORE = 2; //没有更多
454 |
455 | private int loadMoreStatus = LOAD_MORE_NORMAL;
456 |
457 | public int getLoadMoreStatus() {
458 | return loadMoreStatus;
459 | }
460 |
461 | private void setLoadMoreView(LoadMoreViewHolder viewHolder) {
462 | switch (loadMoreStatus) {
463 | case LOAD_MORE_NORMAL:
464 | viewHolder.progressBar.setVisibility(View.VISIBLE);
465 | viewHolder.tv_status.setText("加载更多中...");
466 | viewHolder.itemView.setVisibility(View.VISIBLE);
467 | break;
468 | case LOAD_MORE_FAIL:
469 | viewHolder.progressBar.setVisibility(View.GONE);
470 | viewHolder.tv_status.setText("加载更多失败,点击重试");
471 |
472 | viewHolder.itemView.setVisibility(View.VISIBLE);
473 | break;
474 | case LOAD_MORE_NO_MORE:
475 | viewHolder.progressBar.setVisibility(View.GONE);
476 | viewHolder.tv_status.setText("已经到底部了");
477 | viewHolder.itemView.setVisibility(View.VISIBLE);
478 | break;
479 | }
480 | }
481 |
482 | private static class LoadMoreViewHolder extends RecyclerView.ViewHolder {
483 | private ProgressBar progressBar;
484 | private TextView tv_status;
485 |
486 | public LoadMoreViewHolder(View itemView) {
487 | super(itemView);
488 | }
489 | }
490 |
491 | private static class HeadViewHolder extends RecyclerView.ViewHolder {
492 |
493 | public HeadViewHolder(View itemView) {
494 | super(itemView);
495 | }
496 | }
497 |
498 | private static class FootViewHolder extends RecyclerView.ViewHolder {
499 |
500 | public FootViewHolder(View itemView) {
501 | super(itemView);
502 | }
503 | }
504 | }
505 |
--------------------------------------------------------------------------------
/app/src/main/java/view/gavin/com/flexibleview/adapter/MoreItemHelper.java:
--------------------------------------------------------------------------------
1 | package view.gavin.com.flexibleview.adapter;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 |
9 | import java.util.List;
10 |
11 | /**
12 | * Created by think on 17/8/3.
13 | */
14 |
15 | public abstract class MoreItemHelper {
16 | protected List list;
17 | protected Context context;
18 |
19 | public MoreItemHelper(List list) {
20 | this.list = list;
21 | }
22 |
23 | /**
24 | * @return item 类型
25 | */
26 | public abstract int getItemType();
27 |
28 | public RecyclerView.ViewHolder createViewHolder(Context context, ViewGroup parent) {
29 | if (this.context == null) {
30 | this.context = context;
31 | }
32 | View view = LayoutInflater.from(context).inflate(getLayoutId(), parent, false);
33 | return getViewHolder(view);
34 | }
35 |
36 | /**
37 | * @return item 布局
38 | */
39 | public abstract int getLayoutId();
40 |
41 | public abstract RecyclerView.ViewHolder getViewHolder(View view);
42 |
43 | /**
44 | * @return item 个数
45 | */
46 | public int getItemCount() {
47 | return list != null ? list.size() : 0;
48 | }
49 |
50 | /**
51 | * 绑定视图
52 | */
53 | public abstract void onBindView(RecyclerView.ViewHolder holder, int position);
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/app/src/main/java/view/gavin/com/flexibleview/adapter/SimpleListAdapter.java:
--------------------------------------------------------------------------------
1 | package view.gavin.com.flexibleview.adapter;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.NonNull;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.View;
7 | import android.widget.TextView;
8 |
9 | import java.util.List;
10 |
11 | import view.gavin.com.flexibleview.R;
12 |
13 | /**
14 | * Created by gavin
15 | * date 2018/6/18
16 | */
17 | public class SimpleListAdapter extends BaseRecyclerViewAdapter {
18 | public SimpleListAdapter(Context context, @NonNull List list) {
19 | super(context, list);
20 | }
21 |
22 | @Override
23 | protected int getLayoutId() {
24 | return R.layout.item_recycler_view;
25 | }
26 |
27 | @NonNull
28 | @Override
29 | protected RecyclerView.ViewHolder getViewHolder(View view) {
30 | return new Holder(view);
31 | }
32 |
33 | @Override
34 | protected void onBindView(RecyclerView.ViewHolder viewHolder, int position) {
35 | Holder holder = (Holder) viewHolder;
36 | ((TextView)holder.itemView).setText(list.get(position));
37 | }
38 |
39 | class Holder extends RecyclerView.ViewHolder {
40 |
41 | public Holder(View itemView) {
42 | super(itemView);
43 | }
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/header_background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/FlexibleLayout/2185b81f1d72f6940377711b5aa816a5642f2231/app/src/main/res/drawable/header_background.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_coordinator_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
18 |
19 |
22 |
23 |
29 |
30 |
31 |
32 |
33 |
34 |
39 |
40 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
9 |
10 |
16 |
17 |
23 |
24 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_recycler_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_scroll_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
12 |
13 |
18 |
19 |
23 |
24 |
29 |
30 |
34 |
35 |
36 |
43 |
44 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/header.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_recycler_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/refresh_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/FlexibleLayout/2185b81f1d72f6940377711b5aa816a5642f2231/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/FlexibleLayout/2185b81f1d72f6940377711b5aa816a5642f2231/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | FlexibleView
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | apply from: "dependencies.gradle"
3 |
4 | buildscript {
5 |
6 | repositories {
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:2.3.1'
11 |
12 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
13 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
14 |
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 | }
18 | }
19 |
20 | allprojects {
21 | repositories {
22 | jcenter()
23 | }
24 | }
25 |
26 | task clean(type: Delete) {
27 | delete rootProject.buildDir
28 | }
29 |
--------------------------------------------------------------------------------
/dependencies.gradle:
--------------------------------------------------------------------------------
1 | ext {
2 | compileSdkVersion = 25
3 | buildToolsVersion = '25.0.2'
4 | minSdkVersion = 14
5 | targetSdkVersion = 25
6 | versionCode = 1
7 | versionName = "1.0"
8 |
9 | dependencies = ["appcompat-v7" : "com.android.support:appcompat-v7:22.2.1",
10 | "recyclerview-v7": "com.android.support:recyclerview-v7:23.0.0",
11 | "design" : "com.android.support:design:23.0.0",
12 | "support-v4" : "com.android.support:support-v4:25.1.0"]
13 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/FlexibleLayout/2185b81f1d72f6940377711b5aa816a5642f2231/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Jun 12 11:37:00 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 |
5 | android {
6 | compileSdkVersion rootProject.ext.compileSdkVersion
7 | buildToolsVersion rootProject.ext.buildToolsVersion
8 |
9 | defaultConfig {
10 | minSdkVersion rootProject.ext.minSdkVersion
11 | targetSdkVersion rootProject.ext.targetSdkVersion
12 | versionCode rootProject.ext.versionCode
13 | versionName rootProject.ext.versionName
14 |
15 | }
16 |
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 |
24 | }
25 |
26 | dependencies {
27 | compile fileTree(dir: 'libs', include: ['*.jar'])
28 |
29 | compile rootProject.ext.dependencies['appcompat-v7']
30 | }
31 |
32 | //gradle install
33 | //gradle bintrayUpload
34 |
35 | version = "1.0.2" //这个是版本号,必须填写
36 | def siteUrl = 'https://github.com/Gavin-ZYX/FlexibleLayout' // 项目的主页
37 | def gitUrl = 'https://github.com/Gavin-ZYX/FlexibleLayout' // Git仓库的
38 | group = "com.gavin.view.flexible" // 这里是groupId ,必须填写 一般填你唯一的包名
39 | install {
40 | repositories.mavenInstaller {
41 | // This generates POM.xml with proper parameters
42 | pom {
43 | project {
44 | packaging 'aar' // 项目描述,复制我的话,这里需要修改。
45 | name 'A Sticky Decoration Libriary' //项目描述
46 | url siteUrl // 软件开源协议,现在一般都是Apache License2.0吧,复制我的,这里不需要修改。
47 | licenses {
48 | license {
49 | name 'The Apache Software License, Version 2.0'
50 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
51 | }
52 | }
53 | //填写开发者基本信息,复制我的,这里需要修改。
54 | developers {
55 | developer {
56 | id 'Gavin_ZYX' //你公司的id
57 | name 'Gavin_ZYX' //你的用户名
58 | email 'zhouyouxi93@gmail.com' // 你的邮箱
59 | }
60 | }
61 | // SCM,复制我的,这里不需要修改。
62 | scm {
63 | connection gitUrl
64 | developerConnection gitUrl
65 | url siteUrl
66 | }
67 | }
68 | }
69 | }
70 | }
71 | // 生成jar包的task,不需要修改。
72 | task sourcesJar(type: Jar) {
73 | from android.sourceSets.main.java.srcDirs
74 | classifier = 'sources'
75 | }
76 | // 生成javaDoc的jar,不需要修改
77 | task javadoc(type: Javadoc) {
78 | options.encoding = "UTF-8"
79 | source = android.sourceSets.main.java.srcDirs
80 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
81 | }
82 | task javadocJar(type: Jar, dependsOn: javadoc) {
83 | classifier = 'javadoc'
84 | from javadoc.destinationDir
85 | }
86 | //下面设置编码格式,重点注意,如果不设置可能会在gradlew install的时候出现GBK编码映射错误
87 | javadoc {
88 | options {
89 | encoding "UTF-8"
90 | charSet 'UTF-8'
91 | author true
92 | version true
93 | links "http://docs.oracle.com/javase/7/docs/api"
94 | title 'A CalendarView Support Lunar Calendar For Android' // 文档标题
95 | }
96 | }
97 |
98 | artifacts {//
99 | archives javadocJar
100 | archives sourcesJar
101 | }
102 | // 生成jar包
103 | task releaseJar(type: Copy) {
104 | from('build/intermediates/bundles/release')
105 | into('../jar')
106 | include('classes.jar')
107 | rename('classes.jar', 'okgo-' + version + '.jar')
108 | }
109 | // 这里是读取Bintray相关的信息,我们上传项目到github上的时候会把gradle文件传上去,
110 | // 所以不要把帐号密码的信息直接写在这里,写在local.properties中,这里动态读取。
111 | Properties properties = new Properties()
112 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
113 | bintray {
114 | //读取 local.properties 文件里面的 bintray.user
115 | user = properties.getProperty("bintray.user")
116 | //读取 local.properties 文件里面的 bintray.apikey
117 | key = properties.getProperty("bintray.apikey")
118 | configurations = ['archives']
119 | pkg {
120 | userOrg = "gavin-zyx" //发布到JCenter的组织,注意新版本的bintray是需要手动创建的
121 | repo = "maven"
122 | //发布到JCenter上的仓库名称,注意新版本的bintray是需要手动创建的 // 发布到Bintray上的项目名字
123 | name = "FlexibleView"
124 | websiteUrl = siteUrl
125 | vcsUrl = gitUrl
126 | licenses = ["Apache-2.0"]
127 | publish = true // 是否是公开项目
128 | }
129 | }
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gavin/view/flexible/FlexibleLayout.java:
--------------------------------------------------------------------------------
1 | package com.gavin.view.flexible;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorListenerAdapter;
5 | import android.content.Context;
6 | import android.support.annotation.NonNull;
7 | import android.support.annotation.Nullable;
8 | import android.util.AttributeSet;
9 | import android.util.DisplayMetrics;
10 | import android.view.Gravity;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.view.WindowManager;
14 | import android.widget.FrameLayout;
15 | import android.widget.ImageView;
16 |
17 | import com.gavin.view.flexible.callback.OnPullListener;
18 | import com.gavin.view.flexible.callback.OnReadyPullListener;
19 | import com.gavin.view.flexible.callback.OnRefreshListener;
20 | import com.gavin.view.flexible.util.PullAnimatorUtil;
21 |
22 | /**
23 | * Created by gavin
24 | * date 2018/6/12
25 | * 带有下拉放大效果的FrameLayout
26 | */
27 | public class FlexibleLayout extends FrameLayout implements IFlexible {
28 |
29 | /**
30 | * 是否允许下拉放大
31 | */
32 | private boolean isEnable = true;
33 |
34 | /**
35 | * 是否允许下拉刷新
36 | */
37 | private boolean isRefreshable = false;
38 |
39 | /**
40 | * 头部高度
41 | */
42 | private int mHeaderHeight = 0;
43 |
44 | /**
45 | * 头部宽度
46 | */
47 | private int mHeaderWidth = 0;
48 |
49 | /**
50 | * 头部size ready
51 | */
52 | private boolean mHeaderSizeReady;
53 |
54 | /**
55 | * 头部
56 | */
57 | private View mHeaderView;
58 |
59 | /**
60 | * 刷新
61 | */
62 | private View mRefreshView;
63 |
64 | /**
65 | * 刷新View的宽高
66 | */
67 | private int mRefreshSize = getScreenWidth() / 15;
68 |
69 | /**
70 | * 最大头部下拉高度
71 | */
72 | private int mMaxPullHeight = getScreenWidth() / 3;
73 |
74 | /**
75 | * 最大 刷新 下拉高度
76 | */
77 | private int mMaxRefreshPullHeight = getScreenWidth() / 3;
78 |
79 | /**
80 | * true 开始下拽
81 | */
82 | private boolean mIsBeingDragged;
83 |
84 | /**
85 | * 标志:正在刷新
86 | */
87 | private boolean mIsRefreshing;
88 |
89 | /**
90 | * 准备下拉监听
91 | */
92 | private OnReadyPullListener mListener;
93 |
94 | /**
95 | * 刷新监听
96 | */
97 | private OnRefreshListener mRefreshListener;
98 |
99 | /**
100 | * 初始坐标
101 | */
102 | private float mInitialY, mInitialX;
103 |
104 | /**
105 | * 下拉监听
106 | */
107 | private OnPullListener mOnPullListener;
108 |
109 | /**
110 | * 刷新动画消失监听
111 | */
112 | private RefreshAnimatorListener mRefreshAnimatorListener = new RefreshAnimatorListener();
113 |
114 | public FlexibleLayout(@NonNull Context context) {
115 | this(context, null);
116 | }
117 |
118 | public FlexibleLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
119 | this(context, attrs, 0);
120 | }
121 |
122 | public FlexibleLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
123 | super(context, attrs, defStyleAttr);
124 | init();
125 | }
126 |
127 | private void init() {
128 | mIsRefreshing = false;
129 | mHeaderSizeReady = false;
130 | }
131 |
132 | @Override
133 | public boolean onInterceptTouchEvent(MotionEvent ev) {
134 | log("onInterceptTouchEvent");
135 | if (isEnable && isHeaderReady() && isReady()) {
136 | switch (ev.getAction()) {
137 | case MotionEvent.ACTION_DOWN:
138 | log("onInterceptTouchEvent DOWN");
139 | mInitialX = ev.getX();
140 | mInitialY = ev.getY();
141 | mIsBeingDragged = false;
142 | break;
143 | case MotionEvent.ACTION_MOVE:
144 | log("onInterceptTouchEvent MOVE");
145 | float diffY = ev.getY() - mInitialY;
146 | float diffX = ev.getX() - mInitialX;
147 | if (diffY > 0 && diffY / Math.abs(diffX) > 2) {
148 | mIsBeingDragged = true;
149 | log("onInterceptTouchEvent return true");
150 | return true;
151 | }
152 | break;
153 | case MotionEvent.ACTION_CANCEL:
154 | case MotionEvent.ACTION_UP:
155 | break;
156 | }
157 | }
158 | return super.onInterceptTouchEvent(ev);
159 | }
160 |
161 | @Override
162 | public boolean onTouchEvent(MotionEvent ev) {
163 | log("onTouchEvent");
164 | if (isEnable && isHeaderReady() && isReady()) {
165 | switch (ev.getAction()) {
166 | case MotionEvent.ACTION_MOVE:
167 | if (mIsBeingDragged) {
168 | float diffY = ev.getY() - mInitialY;
169 | changeHeader((int) diffY);
170 | changeRefreshView((int) diffY);
171 | if (mOnPullListener != null) {
172 | mOnPullListener.onPull((int) diffY);
173 | }
174 | log("onTouchEvent return true");
175 | //return true;
176 | }
177 | break;
178 | case MotionEvent.ACTION_CANCEL:
179 | case MotionEvent.ACTION_UP:
180 | if (mIsBeingDragged) {
181 | resetHeader();
182 | if (mOnPullListener != null) {
183 | mOnPullListener.onRelease();
184 | }
185 | //刷新操作
186 | float diffY = ev.getY() - mInitialY;
187 | changeRefreshViewOnActionUp((int) diffY);
188 | return true;
189 | }
190 | break;
191 | }
192 | }
193 | return super.onTouchEvent(ev);
194 | }
195 |
196 | @Override
197 | public boolean isReady() {
198 | return mListener != null && mListener.isReady();
199 | }
200 |
201 | @Override
202 | public boolean isHeaderReady() {
203 | return mHeaderView != null && mHeaderSizeReady;
204 | }
205 |
206 | @Override
207 | public void changeHeader(int offsetY) {
208 | PullAnimatorUtil.pullAnimator(mHeaderView, mHeaderHeight, mHeaderWidth, offsetY, mMaxPullHeight);
209 | }
210 |
211 | @Override
212 | public void resetHeader() {
213 | PullAnimatorUtil.resetAnimator(mHeaderView, mHeaderHeight, mHeaderWidth);
214 | }
215 |
216 | @Override
217 | public void changeRefreshView(int offsetY) {
218 | if (!isRefreshable || mRefreshView == null || isRefreshing()) {
219 | return;
220 | }
221 | PullAnimatorUtil.pullRefreshAnimator(mRefreshView, offsetY, mRefreshSize, mMaxRefreshPullHeight);
222 | }
223 |
224 | @Override
225 | public void changeRefreshViewOnActionUp(int offsetY) {
226 | if (!isRefreshable || mRefreshView == null || isRefreshing()) {
227 | return;
228 | }
229 | mIsRefreshing = true;
230 | if (offsetY > mMaxRefreshPullHeight) {
231 | PullAnimatorUtil.onRefreshing(mRefreshView);
232 | if (mRefreshListener != null) {
233 | mRefreshListener.onRefreshing();
234 | }
235 | } else {
236 | PullAnimatorUtil.resetRefreshView(mRefreshView, mRefreshSize, mRefreshAnimatorListener);
237 | }
238 | }
239 |
240 | @Override
241 | public void onRefreshComplete() {
242 | if (!isRefreshable || mRefreshView == null) {
243 | return;
244 | }
245 | PullAnimatorUtil.resetRefreshView(mRefreshView, mRefreshSize, mRefreshAnimatorListener);
246 | }
247 |
248 |
249 | @Override
250 | public boolean isRefreshing() {
251 | return mIsRefreshing;
252 | }
253 |
254 | /**
255 | * 是否允许下拉放大
256 | *
257 | * @param isEnable
258 | * @return
259 | */
260 | public FlexibleLayout setEnable(boolean isEnable) {
261 | this.isEnable = isEnable;
262 | return this;
263 | }
264 |
265 | /**
266 | * 是否允许下拉刷新
267 | *
268 | * @param isEnable
269 | * @return
270 | */
271 | public FlexibleLayout setRefreshable(boolean isEnable) {
272 | this.isRefreshable = isEnable;
273 | return this;
274 | }
275 |
276 | /**
277 | * 设置头部
278 | *
279 | * @param header
280 | * @return
281 | */
282 | public FlexibleLayout setHeader(View header) {
283 | mHeaderView = header;
284 | mHeaderView.post(new Runnable() {
285 | @Override
286 | public void run() {
287 | mHeaderHeight = mHeaderView.getHeight();
288 | mHeaderWidth = mHeaderView.getWidth();
289 | mHeaderSizeReady = true;
290 | }
291 | });
292 | return this;
293 | }
294 |
295 | /**
296 | * Header最大下拉高度
297 | *
298 | * @param height
299 | * @return
300 | */
301 | public FlexibleLayout setMaxPullHeight(int height) {
302 | mMaxPullHeight = height;
303 | return this;
304 | }
305 |
306 | /**
307 | * 刷新控件 最大下拉高度
308 | *
309 | * @param height
310 | * @return
311 | */
312 | public FlexibleLayout setMaxRefreshPullHeight(int height) {
313 | mMaxRefreshPullHeight = height;
314 | return this;
315 | }
316 |
317 | /**
318 | * 设置刷新View的尺寸(正方形)
319 | *
320 | * @param size
321 | * @return
322 | */
323 | public FlexibleLayout setRefreshSize(int size) {
324 | mRefreshSize = size;
325 | return this;
326 | }
327 |
328 | /**
329 | * 设置刷新View
330 | *
331 | * @param refreshView
332 | * @param listener
333 | * @return
334 | */
335 | public FlexibleLayout setRefreshView(View refreshView, OnRefreshListener listener) {
336 | if (mRefreshView != null) {
337 | removeView(mRefreshView);
338 | }
339 | mRefreshView = refreshView;
340 | mRefreshListener = listener;
341 | FrameLayout.LayoutParams layoutParams = new LayoutParams(mRefreshSize, mRefreshSize);
342 | layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
343 | mRefreshView.setLayoutParams(layoutParams);
344 | mRefreshView.setTranslationY(-mRefreshSize);
345 | addView(mRefreshView);
346 | return this;
347 | }
348 |
349 | /**
350 | * 设置默认的刷新头
351 | *
352 | * @param listener
353 | * @return
354 | */
355 | public FlexibleLayout setDefaultRefreshView(OnRefreshListener listener) {
356 | ImageView refreshView = new ImageView(getContext());
357 | refreshView.setImageResource(R.drawable.flexible_loading);
358 | return setRefreshView(refreshView, listener);
359 | }
360 |
361 | /**
362 | * 监听 是否可以下拉放大
363 | *
364 | * @param listener
365 | * @return
366 | */
367 | public FlexibleLayout setReadyListener(OnReadyPullListener listener) {
368 | mListener = listener;
369 | return this;
370 | }
371 |
372 | /**
373 | * 设置下拉监听
374 | *
375 | * @param onPullListener
376 | * @return
377 | */
378 | public FlexibleLayout setOnPullListener(OnPullListener onPullListener) {
379 | mOnPullListener = onPullListener;
380 | return this;
381 | }
382 |
383 | /**
384 | * 获取屏幕宽度
385 | *
386 | * @return
387 | */
388 | private int getScreenWidth() {
389 | WindowManager mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
390 | DisplayMetrics metrics = new DisplayMetrics();
391 | if (mWindowManager != null) {
392 | mWindowManager.getDefaultDisplay().getMetrics(metrics);
393 | return metrics.widthPixels;
394 | } else {
395 | return 300;
396 | }
397 | }
398 |
399 | private void log(String str) {
400 | //Log.i("FlexibleView", str);
401 | }
402 |
403 | class RefreshAnimatorListener extends AnimatorListenerAdapter {
404 | @Override
405 | public void onAnimationEnd(Animator animation) {
406 | super.onAnimationEnd(animation);
407 | mIsRefreshing = false;
408 | }
409 |
410 | @Override
411 | public void onAnimationCancel(Animator animation) {
412 | super.onAnimationCancel(animation);
413 | mIsRefreshing = false;
414 | }
415 | }
416 | }
417 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gavin/view/flexible/IFlexible.java:
--------------------------------------------------------------------------------
1 | package com.gavin.view.flexible;
2 |
3 | /**
4 | * Created by gavin
5 | * date 2018/6/12
6 | */
7 | public interface IFlexible {
8 |
9 | /**
10 | * 是否准备下拉
11 | *
12 | * @return
13 | */
14 | boolean isReady();
15 |
16 | /**
17 | * 头部ready
18 | *
19 | * @return
20 | */
21 | boolean isHeaderReady();
22 |
23 | /**
24 | * 下拉Header
25 | *
26 | * @param offsetY
27 | */
28 | void changeHeader(int offsetY);
29 |
30 | /**
31 | * 重置Header
32 | */
33 | void resetHeader();
34 |
35 | /**
36 | * 刷新
37 | */
38 | void changeRefreshView(int offsetY);
39 |
40 | /**
41 | * 松开时的刷新控件
42 | * 重置 或 触发刷新
43 | *
44 | * @param offsetY
45 | */
46 | void changeRefreshViewOnActionUp(int offsetY);
47 |
48 | /**
49 | * 刷新完成
50 | */
51 | void onRefreshComplete();
52 |
53 | /**
54 | * 是否正在刷新
55 | */
56 | boolean isRefreshing();
57 | }
58 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gavin/view/flexible/callback/OnPullListener.java:
--------------------------------------------------------------------------------
1 | package com.gavin.view.flexible.callback;
2 |
3 | /**
4 | * Created by gavin
5 | * date 2018/6/12
6 | * 下拉监听
7 | */
8 | public interface OnPullListener {
9 |
10 | /**
11 | * 下拉
12 | * @param offset
13 | */
14 | void onPull(int offset);
15 |
16 | /**
17 | * 松开
18 | */
19 | void onRelease();
20 | }
21 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gavin/view/flexible/callback/OnReadyPullListener.java:
--------------------------------------------------------------------------------
1 | package com.gavin.view.flexible.callback;
2 |
3 | /**
4 | * Created by gavin
5 | * date 2018/6/12
6 | */
7 | public interface OnReadyPullListener {
8 | boolean isReady();
9 | }
10 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gavin/view/flexible/callback/OnRefreshListener.java:
--------------------------------------------------------------------------------
1 | package com.gavin.view.flexible.callback;
2 |
3 | /**
4 | * Created by gavin
5 | * date 2018/6/24
6 | * 下拉刷新监听
7 | */
8 | public interface OnRefreshListener {
9 | void onRefreshing();
10 | }
11 |
--------------------------------------------------------------------------------
/library/src/main/java/com/gavin/view/flexible/util/PullAnimatorUtil.java:
--------------------------------------------------------------------------------
1 | package com.gavin.view.flexible.util;
2 |
3 | import android.animation.Animator;
4 | import android.animation.AnimatorSet;
5 | import android.animation.ObjectAnimator;
6 | import android.animation.ValueAnimator;
7 | import android.support.v4.view.animation.FastOutSlowInInterpolator;
8 | import android.view.View;
9 | import android.view.animation.LinearInterpolator;
10 | import android.widget.RelativeLayout;
11 |
12 | /**
13 | * Created by gavin
14 | * date 2018/6/12
15 | */
16 | public class PullAnimatorUtil {
17 |
18 | /**
19 | * @param headerView
20 | * @param headerHeight
21 | * @param offsetY
22 | */
23 | public static void pullAnimator(View headerView, int headerHeight, int headerWidth, int offsetY, int maxHeight) {
24 | if (headerView == null) {
25 | return;
26 | }
27 | int pullOffset = (int) Math.pow(offsetY, 0.8);
28 | int newHeight = Math.min(maxHeight + headerHeight, pullOffset + headerHeight);
29 | int newWidth = (int) ((((float) newHeight / headerHeight)) * headerWidth);
30 | headerView.getLayoutParams().height = newHeight;
31 | headerView.getLayoutParams().width = newWidth;
32 | int margin = (newWidth - headerWidth) / 2;
33 | if (headerView.getParent() != null
34 | && headerView.getParent() instanceof RelativeLayout) {
35 | // TODO: gavin 2018/6/26 RelativeLayout会有问题,需要查明原因
36 | margin = 0;
37 | }
38 | headerView.setTranslationX(-margin);
39 | headerView.requestLayout();
40 | }
41 |
42 | /**
43 | * 高度重置动画
44 | *
45 | * @param headerView
46 | * @param headerHeight
47 | */
48 | public static void resetAnimator(final View headerView, final int headerHeight, int headerWidth) {
49 | if (headerView == null) {
50 | return;
51 | }
52 | ValueAnimator heightAnimator = ValueAnimator.ofInt(headerView.getLayoutParams().height, headerHeight);
53 | heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
54 | @Override
55 | public void onAnimationUpdate(ValueAnimator animation) {
56 | int height = (int) animation.getAnimatedValue();
57 | headerView.getLayoutParams().height = height;
58 | }
59 | });
60 | ValueAnimator widthAnimator = ValueAnimator.ofInt(headerView.getLayoutParams().width, headerWidth);
61 | widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
62 | @Override
63 | public void onAnimationUpdate(ValueAnimator animation) {
64 | int width = (int) animation.getAnimatedValue();
65 | headerView.getLayoutParams().width = width;
66 | }
67 | });
68 | ValueAnimator translationAnimator = ValueAnimator.ofInt((int) headerView.getTranslationX(), 0);
69 | translationAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
70 | @Override
71 | public void onAnimationUpdate(ValueAnimator animation) {
72 | int translation = (int) animation.getAnimatedValue();
73 | headerView.setTranslationX(translation);
74 | headerView.requestLayout();
75 | }
76 | });
77 | AnimatorSet set = new AnimatorSet();
78 | set.setInterpolator(new FastOutSlowInInterpolator());
79 | set.setDuration(100);
80 | set.play(heightAnimator).with(widthAnimator).with(translationAnimator);
81 | set.start();
82 |
83 | }
84 |
85 | /**
86 | * 下拉时 刷新控件动画
87 | *
88 | * @param refreshView
89 | * @param offsetY
90 | * @param refreshViewHeight
91 | * @param maxRefreshPullHeight
92 | */
93 | public static void pullRefreshAnimator(View refreshView, int offsetY, int refreshViewHeight, int maxRefreshPullHeight) {
94 | if (refreshView == null) {
95 | return;
96 | }
97 | int pullOffset = (int) Math.pow(offsetY, 0.9);
98 | int newHeight = Math.min(maxRefreshPullHeight, pullOffset);
99 | refreshView.setTranslationY(-refreshViewHeight + newHeight);
100 | refreshView.setRotation(pullOffset);
101 | refreshView.requestLayout();
102 | }
103 |
104 | private static ObjectAnimator mRefreshingAnimator;
105 |
106 | /**
107 | * 刷新动画
108 | * 一直转圈圈
109 | *
110 | * @param refreshView
111 | */
112 | public static void onRefreshing(View refreshView) {
113 | float rotation = refreshView.getRotation();
114 | mRefreshingAnimator = ObjectAnimator.ofFloat(refreshView, "rotation", rotation, rotation + 360);
115 | mRefreshingAnimator.setDuration(1000);
116 | mRefreshingAnimator.setInterpolator(new LinearInterpolator());
117 | mRefreshingAnimator.setRepeatMode(ValueAnimator.RESTART);
118 | mRefreshingAnimator.setRepeatCount(-1);
119 | mRefreshingAnimator.start();
120 | }
121 |
122 | /**
123 | * 重置刷新动画
124 | *
125 | * @param refreshView
126 | * @param refreshViewHeight
127 | */
128 | public static void resetRefreshView(View refreshView, int refreshViewHeight, Animator.AnimatorListener animatorListener) {
129 | if (mRefreshingAnimator != null) {
130 | mRefreshingAnimator.cancel();
131 | }
132 | float translation = refreshView.getTranslationY();
133 | ObjectAnimator animator = ObjectAnimator.ofFloat(refreshView, "translationY", translation, -refreshViewHeight);
134 | animator.setDuration(500);
135 | animator.setInterpolator(new LinearInterpolator());
136 | animator.addListener(animatorListener);
137 | animator.start();
138 | }
139 | }
140 |
--------------------------------------------------------------------------------
/library/src/main/res/drawable/flexible_loading.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Gavin-ZYX/FlexibleLayout/2185b81f1d72f6940377711b5aa816a5642f2231/library/src/main/res/drawable/flexible_loading.png
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | library
3 |
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------