├── .gitignore ├── LICENSE ├── README.md ├── app ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── leaf8 │ │ │ └── alicx │ │ │ └── loadmorerecycler │ │ │ ├── ContentListFragment.java │ │ │ ├── LoadMoreRecyclerView.java │ │ │ ├── MainActivity.java │ │ │ ├── MyItemRecyclerViewAdapter.java │ │ │ └── dummy │ │ │ └── DummyContent.java │ └── res │ │ ├── drawable-xxhdpi │ │ ├── allow_down.png │ │ ├── allow_up.png │ │ ├── ic_launcher.png │ │ └── loc_logo.png │ │ ├── drawable │ │ └── shape_rect.xml │ │ ├── layout │ │ ├── activity_header_view.xml │ │ ├── activity_main.xml │ │ ├── fragment_item.xml │ │ ├── fragment_item_list.xml │ │ ├── fragment_item_staggel.xml │ │ └── list_foot_loading.xml │ │ ├── menu │ │ └── menu_main.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── leaf8 │ └── alicx │ └── loadmorerecycler │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | .idea/ 30 | gradle/ 31 | gradlew 32 | gradlew.bat 33 | *.iml 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LoadMoreRecyclerView 2 | 业务发开中的一个demo,RecyclerView实现,支持下拉刷新,上拉自动加载,瀑布流及线性流切换 3 | 4 | 5 | - 列表模式 6 | 7 | ![列表模式](http://images2015.cnblogs.com/blog/263744/201511/263744-20151119112745015-1733937025.png) 8 | 9 | - 瀑布流模式 10 | 11 | ![瀑布流模式](http://images2015.cnblogs.com/blog/263744/201511/263744-20151119113335108-353965405.png) 12 | 13 | - 问题:切换之后滑动到第一屏出现不对齐现象 14 | 15 | ![切换之后滑动到第一屏出现不对齐现象](http://images2015.cnblogs.com/blog/263744/201511/263744-20151119113406327-104210803.png) 16 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.leaf8.alicx.myapplication" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 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 fileTree(include: ['*.jar'], dir: 'libs') 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.1.1' 26 | compile 'com.android.support:design:23.1.1' 27 | compile 'com.android.support:recyclerview-v7:23.1.1' 28 | compile 'com.android.support:support-v4:23.1.1' 29 | compile 'com.android.support:cardview-v7:21.0.0' 30 | } 31 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/alicx/Program/androidSdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/leaf8/alicx/loadmorerecycler/ContentListFragment.java: -------------------------------------------------------------------------------- 1 | package com.leaf8.alicx.loadmorerecycler; 2 | 3 | import android.app.Fragment; 4 | import android.content.Context; 5 | import android.os.Bundle; 6 | import android.support.v4.widget.SwipeRefreshLayout; 7 | import android.support.v7.widget.LinearLayoutManager; 8 | import android.support.v7.widget.StaggeredGridLayoutManager; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.TextView; 13 | 14 | import com.leaf8.alicx.loadmorerecycler.dummy.DummyContent; 15 | 16 | /** 17 | * A fragment representing a list of Items. 18 | *

19 | * interface. 20 | */ 21 | public class ContentListFragment extends Fragment { 22 | 23 | private static final String ARG_COLUMN_COUNT = "column-count"; 24 | private int mColumnCount = 1; 25 | private MyItemRecyclerViewAdapter myItemRecyclerViewAdapter; 26 | private LoadMoreRecyclerView recyclerView; 27 | private SwipeRefreshLayout swipeRefreshLayout; 28 | private int page = 0; 29 | 30 | /** 31 | * Mandatory empty constructor for the fragment manager to instantiate the 32 | * fragment (e.g. upon screen orientation changes). 33 | */ 34 | public ContentListFragment() { 35 | } 36 | 37 | @Override 38 | public void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | 41 | if (getArguments() != null) { 42 | mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); 43 | } 44 | } 45 | 46 | @Override 47 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 48 | Bundle savedInstanceState) { 49 | View view = inflater.inflate(R.layout.fragment_item_list, container, false); 50 | recyclerView = (LoadMoreRecyclerView) view.findViewById(R.id.list); 51 | recyclerView.setHasFixedSize(true); 52 | view.findViewById(R.id.mode_switch_btn).setOnClickListener(new View.OnClickListener() { 53 | 54 | @Override 55 | public void onClick(View v) { 56 | if (1 == mColumnCount) { 57 | mColumnCount = 2; 58 | ((TextView) v).setText(R.string.list_mode_stagger); 59 | myItemRecyclerViewAdapter.switchMode(true); 60 | recyclerView.switchLayoutManager(new StaggeredGridLayoutManager(mColumnCount, StaggeredGridLayoutManager.VERTICAL)); 61 | } else { 62 | mColumnCount = 1; 63 | ((TextView) v).setText(R.string.list_mode_list); 64 | myItemRecyclerViewAdapter.switchMode(false); 65 | recyclerView.switchLayoutManager(new LinearLayoutManager(getActivity())); 66 | } 67 | } 68 | }); 69 | swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout); 70 | swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 71 | @Override 72 | public void onRefresh() { 73 | swipeRefreshLayout.setRefreshing(false); 74 | page = 0; 75 | myItemRecyclerViewAdapter.setData(DummyContent.generyData(page)); 76 | recyclerView.setAutoLoadMoreEnable(DummyContent.hasMore(page)); 77 | myItemRecyclerViewAdapter.notifyDataSetChanged(); 78 | } 79 | }); 80 | if (1 >= mColumnCount) { 81 | recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 82 | } else { 83 | recyclerView.setLayoutManager(new StaggeredGridLayoutManager(mColumnCount, StaggeredGridLayoutManager.VERTICAL)); 84 | } 85 | myItemRecyclerViewAdapter = new MyItemRecyclerViewAdapter(DummyContent.generyData(page)); 86 | recyclerView.setAdapter(myItemRecyclerViewAdapter); 87 | recyclerView.setAutoLoadMoreEnable(true); 88 | recyclerView.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() { 89 | @Override 90 | public void onLoadMore() { 91 | recyclerView.postDelayed(new Runnable() { 92 | @Override 93 | public void run() { 94 | swipeRefreshLayout.setRefreshing(false); 95 | myItemRecyclerViewAdapter.addDatas(DummyContent.generyData(++page)); 96 | recyclerView.notifyMoreFinish(DummyContent.hasMore(page)); 97 | } 98 | }, 1000); 99 | } 100 | }); 101 | myItemRecyclerViewAdapter.notifyDataSetChanged(); 102 | return view; 103 | } 104 | 105 | @Override 106 | public void onAttach(Context context) { 107 | super.onAttach(context); 108 | // if (context instanceof OnListFragmentInteractionListener) { 109 | // mListener = (OnListFragmentInteractionListener) context; 110 | // } else { 111 | // throw new RuntimeException(context.toString() 112 | // + " must implement OnListFragmentInteractionListener"); 113 | // } 114 | } 115 | 116 | @Override 117 | public void onDetach() { 118 | super.onDetach(); 119 | // mListener = null; 120 | } 121 | 122 | /** 123 | * This interface must be implemented by activities that contain this 124 | * fragment to allow an interaction in this fragment to be communicated 125 | * to the activity and potentially other fragments contained in that 126 | * activity. 127 | *

128 | * See the Android Training lesson Communicating with Other Fragments for more information. 131 | */ 132 | // public interface OnListFragmentInteractionListener { 133 | // // TODO: Update argument type and name 134 | // void onListFragmentInteraction(DummyItem item); 135 | // } 136 | } 137 | -------------------------------------------------------------------------------- /app/src/main/java/com/leaf8/alicx/loadmorerecycler/LoadMoreRecyclerView.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Alipay.com Inc. 3 | * Copyright (c) 2004-2015 All Rights Reserved. 4 | */ 5 | package com.leaf8.alicx.loadmorerecycler; 6 | 7 | import android.content.Context; 8 | import android.support.v7.widget.GridLayoutManager; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.support.v7.widget.StaggeredGridLayoutManager; 12 | import android.util.AttributeSet; 13 | import android.view.LayoutInflater; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | 17 | /** 18 | * 支持上拉加载更多的 19 | * 20 | * @author 紫韶 21 | * @version $$Id: LoadMoreRecyclerView.java, v 0.1 11/17/15 10:07 alicx Exp $$ 22 | */ 23 | public class LoadMoreRecyclerView extends RecyclerView { 24 | /** 25 | * item 类型 26 | */ 27 | public final static int TYPE_NORMAL = 0; 28 | public final static int TYPE_HEADER = 1;//头部--支持头部增加一个headerView 29 | public final static int TYPE_FOOTER = 2;//底部--往往是loading_more 30 | public final static int TYPE_LIST = 3;//代表item展示的模式是list模式 31 | public final static int TYPE_STAGGER = 4;//代码item展示模式是网格模式 32 | 33 | private boolean mIsFooterEnable = false;//是否允许加载更多 34 | 35 | /** 36 | * 自定义实现了头部和底部加载更多的adapter 37 | */ 38 | private AutoLoadAdapter mAutoLoadAdapter; 39 | /** 40 | * 标记是否正在加载更多,防止再次调用加载更多接口 41 | */ 42 | private boolean mIsLoadingMore; 43 | /** 44 | * 标记加载更多的position 45 | */ 46 | private int mLoadMorePosition; 47 | /** 48 | * 加载更多的监听-业务需要实现加载数据 49 | */ 50 | private LoadMoreListener mListener; 51 | 52 | public LoadMoreRecyclerView(Context context) { 53 | super(context); 54 | init(); 55 | } 56 | 57 | public LoadMoreRecyclerView(Context context, AttributeSet attrs) { 58 | super(context, attrs); 59 | init(); 60 | } 61 | 62 | public LoadMoreRecyclerView(Context context, AttributeSet attrs, int defStyle) { 63 | super(context, attrs, defStyle); 64 | init(); 65 | } 66 | 67 | /** 68 | * 初始化-添加滚动监听 69 | *

70 | * 回调加载更多方法,前提是 71 | *

 72 |      *    1、有监听并且支持加载更多:null != mListener && mIsFooterEnable
 73 |      *    2、目前没有在加载,正在上拉(dy>0),当前最后一条可见的view是否是当前数据列表的最好一条--及加载更多
 74 |      * 
75 | */ 76 | private void init() { 77 | super.addOnScrollListener(new OnScrollListener() { 78 | 79 | @Override 80 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 81 | super.onScrollStateChanged(recyclerView, newState); 82 | } 83 | 84 | @Override 85 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 86 | super.onScrolled(recyclerView, dx, dy); 87 | if (null != mListener && mIsFooterEnable && !mIsLoadingMore && dy > 0) { 88 | int lastVisiblePosition = getLastVisiblePosition(); 89 | if (lastVisiblePosition + 1 == mAutoLoadAdapter.getItemCount()) { 90 | setLoadingMore(true); 91 | mLoadMorePosition = lastVisiblePosition; 92 | mListener.onLoadMore(); 93 | } 94 | } 95 | } 96 | }); 97 | } 98 | 99 | /** 100 | * 设置加载更多的监听 101 | * 102 | * @param listener 103 | */ 104 | public void setLoadMoreListener(LoadMoreListener listener) { 105 | mListener = listener; 106 | } 107 | 108 | /** 109 | * 设置正在加载更多 110 | * 111 | * @param loadingMore 112 | */ 113 | public void setLoadingMore(boolean loadingMore) { 114 | this.mIsLoadingMore = loadingMore; 115 | } 116 | 117 | /** 118 | * 加载更多监听 119 | */ 120 | public interface LoadMoreListener { 121 | /** 122 | * 加载更多 123 | */ 124 | void onLoadMore(); 125 | } 126 | 127 | /** 128 | * 129 | */ 130 | public class AutoLoadAdapter extends Adapter { 131 | 132 | /** 133 | * 数据adapter 134 | */ 135 | private Adapter mInternalAdapter; 136 | 137 | private boolean mIsHeaderEnable; 138 | private int mHeaderResId; 139 | 140 | public AutoLoadAdapter(Adapter adapter) { 141 | mInternalAdapter = adapter; 142 | mIsHeaderEnable = false; 143 | } 144 | 145 | @Override 146 | public int getItemViewType(int position) { 147 | int headerPosition = 0; 148 | int footerPosition = getItemCount() - 1; 149 | 150 | if (headerPosition == position && mIsHeaderEnable && mHeaderResId > 0) { 151 | return TYPE_HEADER; 152 | } 153 | if (footerPosition == position && mIsFooterEnable) { 154 | return TYPE_FOOTER; 155 | } 156 | /** 157 | * 这么做保证layoutManager切换之后能及时的刷新上对的布局 158 | */ 159 | if (getLayoutManager() instanceof LinearLayoutManager) { 160 | return TYPE_LIST; 161 | } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) { 162 | return TYPE_STAGGER; 163 | } else { 164 | return TYPE_NORMAL; 165 | } 166 | } 167 | 168 | @Override 169 | public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 170 | if (viewType == TYPE_HEADER) { 171 | return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate( 172 | mHeaderResId, parent, false)); 173 | } 174 | if (viewType == TYPE_FOOTER) { 175 | return new FooterViewHolder( 176 | LayoutInflater.from(parent.getContext()).inflate( 177 | R.layout.list_foot_loading, parent, false)); 178 | } else { // type normal 179 | return mInternalAdapter.onCreateViewHolder(parent, viewType); 180 | } 181 | } 182 | 183 | public class FooterViewHolder extends ViewHolder { 184 | 185 | public FooterViewHolder(View itemView) { 186 | super(itemView); 187 | } 188 | } 189 | 190 | public class HeaderViewHolder extends ViewHolder { 191 | public HeaderViewHolder(View itemView) { 192 | super(itemView); 193 | } 194 | } 195 | 196 | @Override 197 | public void onBindViewHolder(ViewHolder holder, int position) { 198 | int type = getItemViewType(position); 199 | if (type != TYPE_FOOTER && type != TYPE_HEADER) { 200 | mInternalAdapter.onBindViewHolder(holder, position); 201 | } 202 | } 203 | 204 | /** 205 | * 需要计算上加载更多和添加的头部俩个 206 | * 207 | * @return 208 | */ 209 | @Override 210 | public int getItemCount() { 211 | int count = mInternalAdapter.getItemCount(); 212 | if (mIsFooterEnable) count++; 213 | if (mIsHeaderEnable) count++; 214 | 215 | return count; 216 | } 217 | 218 | public void setHeaderEnable(boolean enable) { 219 | mIsHeaderEnable = enable; 220 | } 221 | 222 | public void addHeaderView(int resId) { 223 | mHeaderResId = resId; 224 | } 225 | } 226 | 227 | @Override 228 | public void setAdapter(Adapter adapter) { 229 | if (adapter != null) { 230 | mAutoLoadAdapter = new AutoLoadAdapter(adapter); 231 | } 232 | super.swapAdapter(mAutoLoadAdapter, true); 233 | } 234 | 235 | /** 236 | * 切换layoutManager 237 | * 238 | * 为了保证切换之后页面上还是停留在当前展示的位置,记录下切换之前的第一条展示位置,切换完成之后滚动到该位置 239 | * 另外切换之后必须要重新刷新下当前已经缓存的itemView,否则会出现布局错乱(俩种模式下的item布局不同), 240 | * RecyclerView提供了swapAdapter来进行切换adapter并清理老的itemView cache 241 | * 242 | * @param layoutManager 243 | */ 244 | public void switchLayoutManager(LayoutManager layoutManager) { 245 | int firstVisiblePosition = getFirstVisiblePosition(); 246 | // getLayoutManager().removeAllViews(); 247 | setLayoutManager(layoutManager); 248 | // super.swapAdapter(mAutoLoadAdapter, true); 249 | getLayoutManager().scrollToPosition(firstVisiblePosition); 250 | } 251 | 252 | /** 253 | * 获取第一条展示的位置 254 | * 255 | * @return 256 | */ 257 | private int getFirstVisiblePosition() { 258 | int position; 259 | if (getLayoutManager() instanceof LinearLayoutManager) { 260 | position = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); 261 | } else if (getLayoutManager() instanceof GridLayoutManager) { 262 | position = ((GridLayoutManager) getLayoutManager()).findFirstVisibleItemPosition(); 263 | } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) { 264 | StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager(); 265 | int[] lastPositions = layoutManager.findFirstVisibleItemPositions(new int[layoutManager.getSpanCount()]); 266 | position = getMinPositions(lastPositions); 267 | } else { 268 | position = 0; 269 | } 270 | return position; 271 | } 272 | 273 | /** 274 | * 获得当前展示最小的position 275 | * 276 | * @param positions 277 | * @return 278 | */ 279 | private int getMinPositions(int[] positions) { 280 | int size = positions.length; 281 | int minPosition = Integer.MAX_VALUE; 282 | for (int i = 0; i < size; i++) { 283 | minPosition = Math.min(minPosition, positions[i]); 284 | } 285 | return minPosition; 286 | } 287 | 288 | /** 289 | * 获取最后一条展示的位置 290 | * 291 | * @return 292 | */ 293 | private int getLastVisiblePosition() { 294 | int position; 295 | if (getLayoutManager() instanceof LinearLayoutManager) { 296 | position = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition(); 297 | } else if (getLayoutManager() instanceof GridLayoutManager) { 298 | position = ((GridLayoutManager) getLayoutManager()).findLastVisibleItemPosition(); 299 | } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) { 300 | StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager(); 301 | int[] lastPositions = layoutManager.findLastVisibleItemPositions(new int[layoutManager.getSpanCount()]); 302 | position = getMaxPosition(lastPositions); 303 | } else { 304 | position = getLayoutManager().getItemCount() - 1; 305 | } 306 | return position; 307 | } 308 | 309 | /** 310 | * 获得最大的位置 311 | * 312 | * @param positions 313 | * @return 314 | */ 315 | private int getMaxPosition(int[] positions) { 316 | int size = positions.length; 317 | int maxPosition = Integer.MIN_VALUE; 318 | for (int i = 0; i < size; i++) { 319 | maxPosition = Math.max(maxPosition, positions[i]); 320 | } 321 | return maxPosition; 322 | } 323 | 324 | /** 325 | * 添加头部view 326 | * 327 | * @param resId 328 | */ 329 | public void addHeaderView(int resId) { 330 | mAutoLoadAdapter.addHeaderView(resId); 331 | } 332 | 333 | /** 334 | * 设置头部view是否展示 335 | * @param enable 336 | */ 337 | public void setHeaderEnable(boolean enable) { 338 | mAutoLoadAdapter.setHeaderEnable(enable); 339 | } 340 | 341 | /** 342 | * 设置是否支持自动加载更多 343 | * 344 | * @param autoLoadMore 345 | */ 346 | public void setAutoLoadMoreEnable(boolean autoLoadMore) { 347 | mIsFooterEnable = autoLoadMore; 348 | } 349 | 350 | /** 351 | * 通知更多的数据已经加载 352 | * 353 | * 每次加载完成之后添加了Data数据,用notifyItemRemoved来刷新列表展示, 354 | * 而不是用notifyDataSetChanged来刷新列表 355 | * 356 | * @param hasMore 357 | */ 358 | public void notifyMoreFinish(boolean hasMore) { 359 | setAutoLoadMoreEnable(hasMore); 360 | getAdapter().notifyItemRemoved(mLoadMorePosition); 361 | mIsLoadingMore = false; 362 | } 363 | } 364 | -------------------------------------------------------------------------------- /app/src/main/java/com/leaf8/alicx/loadmorerecycler/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.leaf8.alicx.loadmorerecycler; 2 | 3 | import android.app.FragmentTransaction; 4 | import android.os.Bundle; 5 | import android.support.design.widget.FloatingActionButton; 6 | import android.support.design.widget.Snackbar; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.support.v7.widget.Toolbar; 9 | import android.view.Menu; 10 | import android.view.MenuItem; 11 | import android.view.View; 12 | 13 | public class MainActivity extends AppCompatActivity /*implements ContentListFragment.OnListFragmentInteractionListener*/ { 14 | 15 | private ContentListFragment mListFragment; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 22 | setSupportActionBar(toolbar); 23 | 24 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 25 | fab.setOnClickListener(new View.OnClickListener() { 26 | @Override 27 | public void onClick(View view) { 28 | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 29 | .setAction("Action", null).show(); 30 | } 31 | }); 32 | attachFragment(0); 33 | } 34 | 35 | @Override 36 | public boolean onCreateOptionsMenu(Menu menu) { 37 | // Inflate the menu; this adds items to the action bar if it is present. 38 | getMenuInflater().inflate(R.menu.menu_main, menu); 39 | return true; 40 | } 41 | 42 | @Override 43 | public boolean onOptionsItemSelected(MenuItem item) { 44 | // Handle action bar item clicks here. The action bar will 45 | // automatically handle clicks on the Home/Up button, so long 46 | // as you specify a parent activity in AndroidManifest.xml. 47 | int id = item.getItemId(); 48 | 49 | //noinspection SimplifiableIfStatement 50 | if (id == R.id.action_settings) { 51 | return true; 52 | } 53 | 54 | return super.onOptionsItemSelected(item); 55 | } 56 | 57 | /** 58 | * 加载布局 59 | */ 60 | private void attachFragment(int type) { 61 | FragmentTransaction transaction = getFragmentManager().beginTransaction(); 62 | switch (type) { 63 | case 0: 64 | if (null == mListFragment) { 65 | mListFragment = new ContentListFragment(); 66 | } 67 | mListFragment.setArguments(getIntent().getExtras()); 68 | transaction.replace(R.id.container, mListFragment); 69 | break; 70 | case 1: 71 | if (null == mListFragment) { 72 | mListFragment = new ContentListFragment(); 73 | } 74 | mListFragment.setArguments(getIntent().getExtras()); 75 | transaction.replace(R.id.container, mListFragment); 76 | break; 77 | } 78 | transaction.commit(); 79 | } 80 | 81 | /** 82 | * 点击事件 83 | * @param view 84 | */ 85 | public void clickCategory(View view) { 86 | int clickId = view.getId(); 87 | if (clickId == R.id.waimai) { 88 | attachFragment(1); 89 | } else { 90 | attachFragment(0); 91 | } 92 | } 93 | 94 | // @Override 95 | // public void onListFragmentInteraction(DummyContent.DummyItem item) { 96 | // 97 | // } 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/com/leaf8/alicx/loadmorerecycler/MyItemRecyclerViewAdapter.java: -------------------------------------------------------------------------------- 1 | package com.leaf8.alicx.loadmorerecycler; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.TextView; 8 | 9 | import com.leaf8.alicx.loadmorerecycler.dummy.DummyContent; 10 | import com.leaf8.alicx.loadmorerecycler.dummy.DummyContent.DummyItem; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the 16 | * TODO: Replace the implementation with code for your data type. 17 | */ 18 | public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter { 19 | 20 | private List mValues; 21 | private boolean mIsStagger; 22 | 23 | public MyItemRecyclerViewAdapter(List items) { 24 | mValues = items; 25 | } 26 | 27 | public void switchMode(boolean mIsStagger) { 28 | this.mIsStagger = mIsStagger; 29 | } 30 | 31 | public void setData(List datas) { 32 | mValues = datas; 33 | } 34 | 35 | public void addDatas(List datas) { 36 | mValues.addAll(datas); 37 | } 38 | 39 | @Override 40 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 41 | if (viewType == LoadMoreRecyclerView.TYPE_STAGGER) { 42 | View view = LayoutInflater.from(parent.getContext()) 43 | .inflate(R.layout.fragment_item_staggel, parent, false); 44 | return new StaggerViewHolder(view); 45 | } else { 46 | View view = LayoutInflater.from(parent.getContext()) 47 | .inflate(R.layout.fragment_item, parent, false); 48 | return new ViewHolder(view); 49 | } 50 | } 51 | 52 | @Override 53 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 54 | if (mIsStagger) { 55 | StaggerViewHolder staggerViewHolder = (StaggerViewHolder) holder; 56 | staggerViewHolder.iconView.setVisibility(View.VISIBLE); 57 | staggerViewHolder.mContentView.setText(mValues.get(position).details); 58 | } else { 59 | ViewHolder mHolder = (ViewHolder) holder; 60 | mHolder.mItem = mValues.get(position); 61 | mHolder.mContentView.setText(mValues.get(position).content); 62 | mHolder.mIdView.setText(mValues.get(position).id); 63 | } 64 | } 65 | 66 | @Override 67 | public int getItemCount() { 68 | return mValues.size(); 69 | } 70 | 71 | public class StaggerViewHolder extends RecyclerView.ViewHolder { 72 | public View mView; 73 | public View iconView; 74 | public TextView mContentView; 75 | 76 | public StaggerViewHolder(View itemView) { 77 | super(itemView); 78 | mView = itemView; 79 | iconView = itemView.findViewById(R.id.icon); 80 | mContentView = (TextView) itemView.findViewById(R.id.content); 81 | } 82 | } 83 | 84 | public class ViewHolder extends RecyclerView.ViewHolder { 85 | public final View mView; 86 | public final TextView mIdView; 87 | public final TextView mContentView; 88 | public DummyItem mItem; 89 | 90 | public ViewHolder(View view) { 91 | super(view); 92 | mView = view; 93 | mIdView = (TextView) view.findViewById(R.id.id); 94 | mContentView = (TextView) view.findViewById(R.id.content); 95 | } 96 | 97 | @Override 98 | public String toString() { 99 | return super.toString() + " '" + mContentView.getText() + "'"; 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/java/com/leaf8/alicx/loadmorerecycler/dummy/DummyContent.java: -------------------------------------------------------------------------------- 1 | package com.leaf8.alicx.loadmorerecycler.dummy; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Helper class for providing sample content for user interfaces created by 8 | * Android template wizards. 9 | *

10 | * TODO: Replace all uses of this class before publishing your app. 11 | */ 12 | public class DummyContent { 13 | 14 | private static final int COUNT = 25; 15 | private static final int TOTALPAGE = 4; 16 | 17 | public static List generyData(int page) { 18 | int start = page * COUNT; 19 | int end = TOTALPAGE == page ? start + COUNT : start + COUNT; 20 | List items = new ArrayList(); 21 | for (int i = start; i < end; i++) { 22 | items.add(createDummyItem(i)); 23 | } 24 | return items; 25 | } 26 | 27 | /** 28 | * 是否还有更多 29 | * 30 | * @param page 31 | * @return 32 | */ 33 | public static boolean hasMore(int page) { 34 | return page < TOTALPAGE; 35 | } 36 | 37 | private static DummyItem createDummyItem(int position) { 38 | return new DummyItem(String.valueOf(position), "Item " + position, makeDetails(position)); 39 | } 40 | 41 | private static String makeDetails(int position) { 42 | StringBuilder builder = new StringBuilder(); 43 | builder.append("Details about Item: ").append(position); 44 | int count = position % 3; 45 | for (int i = 0; i < count; i++) { 46 | builder.append("\nMore details information here."); 47 | } 48 | return builder.toString(); 49 | } 50 | 51 | /** 52 | * A dummy item representing a piece of content. 53 | */ 54 | public static class DummyItem { 55 | public final String id; 56 | public final String content; 57 | public final String details; 58 | 59 | public DummyItem(String id, String content, String details) { 60 | this.id = id; 61 | this.content = content; 62 | this.details = details; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return content; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/allow_down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alicx/LoadMoreRecyclerView/3ba8f78e4585e0b86653777d15ff8c4d26fd6118/app/src/main/res/drawable-xxhdpi/allow_down.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/allow_up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alicx/LoadMoreRecyclerView/3ba8f78e4585e0b86653777d15ff8c4d26fd6118/app/src/main/res/drawable-xxhdpi/allow_up.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alicx/LoadMoreRecyclerView/3ba8f78e4585e0b86653777d15ff8c4d26fd6118/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/loc_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alicx/LoadMoreRecyclerView/3ba8f78e4585e0b86653777d15ff8c4d26fd6118/app/src/main/res/drawable-xxhdpi/loc_logo.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_rect.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 11 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_header_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 21 | 22 | 27 | 28 | 35 | 36 | 46 | 47 | 57 | 58 | 68 | 69 | 78 | 79 | 80 | 89 | 90 | 100 | 101 | 111 | 112 | 122 | 123 | 132 | 133 | 134 | 140 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_item_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 | 21 | 22 | 28 | 29 | 30 | 34 | 35 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_item_staggel.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/list_foot_loading.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 13 | 14 | 15 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 |

5 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #eeeff3 8 | #dadada 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 16dp 6 | 16dp 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MyApplication 3 | Settings 4 | 加载中... 5 | 点我切换模式-列表模式 6 | 点我切换模式-图片模式 7 | 菜单栏todo 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |