├── .gitignore ├── README.md ├── app ├── .gitignore ├── AndroidManifest.xml ├── build.gradle ├── proguard-rules.pro ├── res │ ├── layout │ │ ├── activity_main.xml │ │ ├── recycler_empty_item.xml │ │ ├── recycler_footer_item.xml │ │ ├── recycler_header_item.xml │ │ └── recycler_item_view.xml │ ├── menu │ │ └── menu_main.xml │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ ├── values-w820dp │ │ └── dimens.xml │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml └── src │ └── com │ └── rcv │ └── lee │ └── androidrecyclerview │ ├── MainActivity.java │ ├── OnBottomListener.java │ ├── OnRcvScrollListener.java │ └── item │ └── Item.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── recyclerview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── lee │ │ └── recyclerview │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── lee │ │ │ └── recyclerview │ │ │ ├── adapter │ │ │ ├── AdapterItem.java │ │ │ ├── AdapterItemUtil.java │ │ │ ├── CommonRcvAdapter.java │ │ │ └── MfCommonRcvAdapter.java │ │ │ └── recyclerview │ │ │ └── MfRecycylerVIew.java │ └── res │ │ └── values │ │ ├── ids.xml │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── lee │ └── recyclerview │ └── ExampleUnitTest.java ├── recyclerview_support_head_footer_v3.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | *.iml 8 | 9 | # Java class files 10 | *.class 11 | 12 | # Generated files 13 | bin/ 14 | gen/ 15 | build/ 16 | target/ 17 | 18 | # Gradle files 19 | .gradle/ 20 | 21 | # Local configuration file (sdk path, etc) 22 | local.properties 23 | 24 | # Proguard folder generated by Eclipse 25 | .classpath 26 | .project 27 | .gradle 28 | .idea 29 | 30 | # Log Files 31 | *.log 32 | 33 | # Android Studio Navigation editor temp files 34 | .navigation/ 35 | 36 | #Mac os 37 | .DS_Store 38 | gen-external-apklibs 39 | 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidRecyclerView 2 | Android RecyclerView support addHeaderView,addFooterView and loadMore. 3 | 4 | ![RecyclerView logo](http://raw.github.com/DukeLee1989/AndroidRecyclerView/master/recyclerview_support_head_footer_v3.gif) 5 | 6 | ##代码设置 7 | 8 | **添加headView** 9 | `MfRecycylerView mRecyclerView;` 10 | `mRecyclerView.addHeaderView(headerView);` 11 | 12 | **添加footerView** 13 | `mRecyclerView.addFooterView(footerView);` 14 | 15 |  **设置emptyView** 16 | `mRecyclerView.setEmptyView(emptyView);` 17 | 18 | **RecyclerView滑动到bottom** 19 | `mRecyclerView.addOnScrollListener(new 20 | OnRcvScrollListener(OnRcvScrollListener.LAYOUT_MANAGERTYPE.LINEAR, new OnBottomListener() { 21 | @Override 22 | public void onBottom() { 23 | //TODO 24 | } 25 | }));` -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "22.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.rcv.lee.androidrecyclerview" 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 | sourceSets { 22 | main { 23 | manifest.srcFile 'AndroidManifest.xml' 24 | java.srcDirs = ['src'] 25 | resources.srcDirs = ['src'] 26 | aidl.srcDirs = ['src'] 27 | renderscript.srcDirs = ['src'] 28 | res.srcDirs = ['res'] 29 | assets.srcDirs = ['assets'] 30 | jniLibs.srcDirs = ['libs'] 31 | } 32 | 33 | // Move the tests to tests/java, tests/res, etc... 34 | androidTest.setRoot('tests') 35 | // Move the build types to build-types/ 36 | // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... 37 | // This moves them out of them default location under src//... which would 38 | // conflict with src/ being used by the main source set. 39 | // Adding new build types or product flavors should be accompanied 40 | // by a similar customization. 41 | debug.setRoot('build-types/debug') 42 | release.setRoot('build-types/release') 43 | } 44 | } 45 | 46 | dependencies { 47 | compile fileTree(dir: 'libs', include: ['*.jar']) 48 | compile project(':recyclerview') 49 | } 50 | -------------------------------------------------------------------------------- /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 /develop/android/sdk/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/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 19 | -------------------------------------------------------------------------------- /app/res/layout/recycler_empty_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 16 | -------------------------------------------------------------------------------- /app/res/layout/recycler_footer_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 14 | 15 | 23 | -------------------------------------------------------------------------------- /app/res/layout/recycler_header_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 14 | -------------------------------------------------------------------------------- /app/res/layout/recycler_item_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 19 | -------------------------------------------------------------------------------- /app/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /app/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaNaLee/AndroidRecyclerView/c05d4856613041d2e28af599306456e060f03ec1/app/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaNaLee/AndroidRecyclerView/c05d4856613041d2e28af599306456e060f03ec1/app/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaNaLee/AndroidRecyclerView/c05d4856613041d2e28af599306456e060f03ec1/app/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaNaLee/AndroidRecyclerView/c05d4856613041d2e28af599306456e060f03ec1/app/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidRecyclerView 3 | 4 | Hello world! 5 | Settings 6 | load more… 7 | last_page 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/com/rcv/lee/androidrecyclerview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.rcv.lee.androidrecyclerview; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.os.Message; 6 | import android.os.SystemClock; 7 | import android.support.annotation.NonNull; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.support.v7.widget.LinearLayoutManager; 10 | import android.support.v7.widget.RecyclerView; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.widget.ProgressBar; 14 | import android.widget.TextView; 15 | 16 | import com.lee.recyclerview.adapter.AdapterItem; 17 | import com.lee.recyclerview.adapter.MfCommonRcvAdapter; 18 | import com.lee.recyclerview.recyclerview.MfRecycylerView; 19 | import com.rcv.lee.androidrecyclerview.item.Item; 20 | 21 | import java.lang.ref.WeakReference; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import in.srain.cube.views.ptr.PtrClassicFrameLayout; 26 | import in.srain.cube.views.ptr.PtrDefaultHandler; 27 | import in.srain.cube.views.ptr.PtrFrameLayout; 28 | import in.srain.cube.views.ptr.PtrHandler; 29 | 30 | /** 31 | * Created by lee on 2015/7/16. 32 | * Modified by lee on 15/10/28. 33 | * Email:lee131483@gmail.com 34 | */ 35 | public class MainActivity extends AppCompatActivity { 36 | 37 | private PtrClassicFrameLayout mPtrClassicFrameLayout; 38 | 39 | private MfRecycylerView mRecyclerView; 40 | 41 | private LinearLayoutManager mLayoutManager; 42 | 43 | private TextView mFooterTv; 44 | 45 | private ProgressBar mFooterPb; 46 | 47 | private List mShowDatas; 48 | 49 | private int mCurPageNo; 50 | 51 | private int MAX_PAGE_NUM = 5; 52 | 53 | private MfCommonRcvAdapter mAdapter; 54 | 55 | private WeakHandler mWeakHandler; 56 | 57 | private static final int MSG_LOAD_MORE_DATA = 0x01; 58 | 59 | private static final int MSG_REFRESH_DATA = 0x02; 60 | 61 | private boolean mIsRefreshing; 62 | 63 | @Override 64 | protected void onCreate(Bundle savedInstanceState) { 65 | super.onCreate(savedInstanceState); 66 | setContentView(R.layout.activity_main); 67 | initData(); 68 | setUpView(); 69 | } 70 | 71 | private void initData() { 72 | mShowDatas = new ArrayList<>(); 73 | mWeakHandler = new WeakHandler(this); 74 | mCurPageNo = 1; 75 | } 76 | 77 | private void loadMoreData() { 78 | new Thread(new Runnable() { 79 | @Override 80 | public void run() { 81 | mIsRefreshing = true; 82 | SystemClock.sleep(2000); 83 | int size = mShowDatas.size(); 84 | List data = new ArrayList(); 85 | for (int i = 1; i <= 30; i++) { 86 | data.add(String.valueOf(size + i - 1)); 87 | } 88 | Message message = mWeakHandler.obtainMessage(); 89 | message.what = MSG_LOAD_MORE_DATA; 90 | message.obj = data; 91 | mWeakHandler.sendMessage(message); 92 | } 93 | }).start(); 94 | } 95 | 96 | private void refreshData() { 97 | mFooterPb.setVisibility(View.VISIBLE); 98 | mFooterTv.setText(R.string.load_more); 99 | new Thread(new Runnable() { 100 | @Override 101 | public void run() { 102 | mCurPageNo = 1; 103 | mIsRefreshing = true; 104 | SystemClock.sleep(2000); 105 | List data = new ArrayList(); 106 | for (int i = 1; i <= 30; i++) { 107 | data.add(String.valueOf(i - 1)); 108 | } 109 | Message message = mWeakHandler.obtainMessage(); 110 | message.what = MSG_REFRESH_DATA; 111 | message.obj = data; 112 | mWeakHandler.sendMessage(message); 113 | } 114 | }).start(); 115 | } 116 | 117 | private void setUpView() { 118 | mPtrClassicFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.ptr_frame_id); 119 | 120 | mPtrClassicFrameLayout.setPtrHandler(new PtrHandler() { 121 | @Override 122 | public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) { 123 | return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header); 124 | } 125 | 126 | @Override 127 | public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) { 128 | refreshData(); 129 | } 130 | }); 131 | mRecyclerView = (MfRecycylerView) findViewById(R.id.recyclerView); 132 | mRecyclerView.setHasFixedSize(true); 133 | mLayoutManager = new LinearLayoutManager(this); 134 | mRecyclerView.setLayoutManager(mLayoutManager); 135 | mRecyclerView.addOnScrollListener(new OnRcvScrollListener(OnRcvScrollListener.LAYOUT_MANAGER_TYPE.LINEAR, new OnBottomListener() { 136 | @Override 137 | public void onBottom() { 138 | if (!mIsRefreshing && !mShowDatas.isEmpty()) 139 | if (mCurPageNo < MAX_PAGE_NUM) { 140 | loadMoreData(); 141 | mCurPageNo++; 142 | } else { 143 | updateFooterView(); 144 | } 145 | } 146 | })); 147 | View emptyView = LayoutInflater.from(MainActivity.this) 148 | .inflate(R.layout.recycler_empty_item, null); 149 | RecyclerView.LayoutParams emptyLayoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT); 150 | emptyView.setLayoutParams(emptyLayoutParams); 151 | mRecyclerView.setEmptyView(emptyView); 152 | 153 | View headerView = LayoutInflater.from(MainActivity.this) 154 | .inflate(R.layout.recycler_header_item, null); 155 | RecyclerView.LayoutParams headerLayoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, 200); 156 | headerView.setLayoutParams(headerLayoutParams); 157 | mRecyclerView.addHeaderView(headerView); 158 | 159 | View footerView = LayoutInflater.from(MainActivity.this) 160 | .inflate(R.layout.recycler_footer_item, null); 161 | mFooterTv = (TextView) footerView.findViewById(R.id.footer_item_text); 162 | mFooterPb = (ProgressBar) footerView.findViewById(R.id.footer_item_progressBar); 163 | RecyclerView.LayoutParams footerLayoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, 200); 164 | footerView.setLayoutParams(footerLayoutParams); 165 | mRecyclerView.addFooterView(footerView); 166 | 167 | mAdapter = new CommonRcvAdapter(mShowDatas); 168 | mRecyclerView.setAdapter(mAdapter); 169 | 170 | 171 | mWeakHandler.postDelayed(new Runnable() { 172 | @Override 173 | public void run() { 174 | mPtrClassicFrameLayout.autoRefresh(); 175 | } 176 | }, 500); 177 | } 178 | 179 | class CommonRcvAdapter extends MfCommonRcvAdapter { 180 | 181 | protected CommonRcvAdapter(List data) { 182 | super(data); 183 | } 184 | 185 | @NonNull 186 | @Override 187 | public AdapterItem getItemView(Object type) { 188 | return new Item(); 189 | } 190 | } 191 | 192 | private void updateFooterView() { 193 | mFooterPb.setVisibility(View.INVISIBLE); 194 | mFooterTv.setText(R.string.last_page); 195 | } 196 | 197 | private static class WeakHandler extends Handler { 198 | WeakReference weakReference; 199 | 200 | public WeakHandler(MainActivity activity) { 201 | weakReference = new WeakReference<>(activity); 202 | } 203 | 204 | @Override 205 | public void handleMessage(Message msg) { 206 | super.handleMessage(msg); 207 | MainActivity activity = weakReference.get(); 208 | if (null != activity) { 209 | switch (msg.what) { 210 | case MSG_LOAD_MORE_DATA: 211 | activity.updateView((List) msg.obj); 212 | break; 213 | case MSG_REFRESH_DATA: 214 | activity.refreshView((List) msg.obj); 215 | default: 216 | break; 217 | } 218 | } 219 | } 220 | } 221 | 222 | private void updateView(List data) { 223 | if (mPtrClassicFrameLayout.isRefreshing()) 224 | mPtrClassicFrameLayout.refreshComplete(); 225 | mAdapter.addDatas(data); 226 | mIsRefreshing = false; 227 | } 228 | 229 | private void refreshView(List data) { 230 | if (mPtrClassicFrameLayout.isRefreshing()) 231 | mPtrClassicFrameLayout.refreshComplete(); 232 | mAdapter.refreshData(data); 233 | mIsRefreshing = false; 234 | } 235 | 236 | 237 | } 238 | -------------------------------------------------------------------------------- /app/src/com/rcv/lee/androidrecyclerview/OnBottomListener.java: -------------------------------------------------------------------------------- 1 | package com.rcv.lee.androidrecyclerview; 2 | 3 | /** 4 | * Created by lee on 15/7/16. 5 | */ 6 | public interface OnBottomListener { 7 | void onBottom(); 8 | } 9 | -------------------------------------------------------------------------------- /app/src/com/rcv/lee/androidrecyclerview/OnRcvScrollListener.java: -------------------------------------------------------------------------------- 1 | package com.rcv.lee.androidrecyclerview; 2 | 3 | import android.support.v7.widget.GridLayoutManager; 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 lee on 15/7/16. 10 | */ 11 | public class OnRcvScrollListener extends RecyclerView.OnScrollListener { 12 | 13 | private String TAG = getClass().getSimpleName(); 14 | 15 | public enum LAYOUT_MANAGER_TYPE { 16 | LINEAR, 17 | GRID, 18 | STAGGERED_GRID 19 | } 20 | 21 | /** 22 | * layoutManager的类型(枚举) 23 | */ 24 | protected LAYOUT_MANAGER_TYPE layoutManagerType; 25 | 26 | /** 27 | * 最后一个的位置 28 | */ 29 | private int[] lastPositions; 30 | 31 | /** 32 | * 最后一个可见的item的位置 33 | */ 34 | private int lastVisibleItemPosition; 35 | /** 36 | * 当前滑动的状态 37 | */ 38 | private int currentScrollState = 0; 39 | 40 | private OnBottomListener onBottomListener; 41 | 42 | public OnRcvScrollListener(LAYOUT_MANAGER_TYPE layoutManagerType, OnBottomListener onBottomListener){ 43 | this.layoutManagerType=layoutManagerType; 44 | this.onBottomListener=onBottomListener; 45 | } 46 | 47 | @Override 48 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 49 | super.onScrolled(recyclerView, dx, dy); 50 | 51 | RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); 52 | // int lastVisibleItemPosition = -1; 53 | if (layoutManagerType == null) { 54 | if (layoutManager instanceof LinearLayoutManager) { 55 | layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR; 56 | } else if (layoutManager instanceof GridLayoutManager) { 57 | layoutManagerType = LAYOUT_MANAGER_TYPE.GRID; 58 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 59 | layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID; 60 | } else { 61 | throw new RuntimeException( 62 | "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); 63 | } 64 | } 65 | 66 | switch (layoutManagerType) { 67 | case LINEAR: 68 | lastVisibleItemPosition = ((LinearLayoutManager) layoutManager) 69 | .findLastVisibleItemPosition(); 70 | break; 71 | case GRID: 72 | lastVisibleItemPosition = ((GridLayoutManager) layoutManager) 73 | .findLastVisibleItemPosition(); 74 | break; 75 | case STAGGERED_GRID: 76 | StaggeredGridLayoutManager staggeredGridLayoutManager 77 | = (StaggeredGridLayoutManager) layoutManager; 78 | if (lastPositions == null) { 79 | lastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; 80 | } 81 | staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions); 82 | lastVisibleItemPosition = findMax(lastPositions); 83 | break; 84 | } 85 | 86 | } 87 | 88 | @Override 89 | public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 90 | super.onScrollStateChanged(recyclerView, newState); 91 | currentScrollState = newState; 92 | RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); 93 | int visibleItemCount = layoutManager.getChildCount(); 94 | int totalItemCount = layoutManager.getItemCount(); 95 | if ((visibleItemCount > 0 && currentScrollState == RecyclerView.SCROLL_STATE_IDLE && 96 | (lastVisibleItemPosition) >= totalItemCount - 1)) { 97 | if(null!=onBottomListener) 98 | onBottomListener.onBottom(); 99 | } 100 | } 101 | 102 | 103 | private int findMax(int[] lastPositions) { 104 | int max = lastPositions[0]; 105 | for (int value : lastPositions) { 106 | if (value > max) { 107 | max = value; 108 | } 109 | } 110 | return max; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /app/src/com/rcv/lee/androidrecyclerview/item/Item.java: -------------------------------------------------------------------------------- 1 | package com.rcv.lee.androidrecyclerview.item; 2 | 3 | import android.view.View; 4 | import android.widget.TextView; 5 | 6 | import com.lee.recyclerview.adapter.AdapterItem; 7 | import com.rcv.lee.androidrecyclerview.R; 8 | 9 | /** 10 | * AndroidRecyclerView 11 | * com.rcv.lee.androidrecyclerview.item 12 | * Created by lee on 15/10/28. 13 | * Email:lee131483@gmail.com 14 | */ 15 | public class Item implements AdapterItem { 16 | 17 | private TextView mTextView; 18 | 19 | @Override 20 | public int getLayoutResId() { 21 | return R.layout.recycler_item_view; 22 | } 23 | 24 | @Override 25 | public void onBindViews(View root) { 26 | mTextView = (TextView) root.findViewById(R.id.textView); 27 | } 28 | 29 | @Override 30 | public void onSetViews() { 31 | 32 | } 33 | 34 | @Override 35 | public void onUpdateViews(String model, int position) { 36 | mTextView.setText(String.valueOf(position)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.2.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaNaLee/AndroidRecyclerView/c05d4856613041d2e28af599306456e060f03ec1/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Aug 06 21:16:15 CST 2015 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-2.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 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 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /recyclerview/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /recyclerview/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | minSdkVersion 14 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | compile 'com.android.support:appcompat-v7:23.0.1' 24 | compile 'com.android.support:recyclerview-v7:23.0.1' 25 | compile 'in.srain.cube:ultra-ptr:1.0.10' 26 | } 27 | -------------------------------------------------------------------------------- /recyclerview/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 /develop/android/sdk/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 | -------------------------------------------------------------------------------- /recyclerview/src/androidTest/java/com/lee/recyclerview/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.lee.recyclerview; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /recyclerview/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /recyclerview/src/main/java/com/lee/recyclerview/adapter/AdapterItem.java: -------------------------------------------------------------------------------- 1 | package com.lee.recyclerview.adapter; 2 | 3 | import android.support.annotation.LayoutRes; 4 | import android.view.View; 5 | 6 | /** 7 | * adapter的所有item必须实现此接口. 8 | * 通过返回{@link #getLayoutResId()}来自动初始化view,之后在{@link #onBindViews(View)}中就可以初始化item的内部视图了。
9 | * @author Jack Tony 10 | * @date 2015/5/15 11 | */ 12 | public interface AdapterItem { 13 | 14 | /** 15 | * @return item布局文件的layoutId 16 | */ 17 | @LayoutRes 18 | int getLayoutResId(); 19 | 20 | /** 21 | * 初始化views 22 | */ 23 | void onBindViews(final View root); 24 | 25 | /** 26 | * 设置view的参数 27 | */ 28 | void onSetViews(); 29 | 30 | /** 31 | * 根据数据来设置item的内部views 32 | * 33 | * @param model 数据list内部的model 34 | * @param position 当前adapter调用item的位置 35 | */ 36 | void onUpdateViews(T model, int position); 37 | 38 | } -------------------------------------------------------------------------------- /recyclerview/src/main/java/com/lee/recyclerview/adapter/AdapterItemUtil.java: -------------------------------------------------------------------------------- 1 | package com.lee.recyclerview.adapter; 2 | 3 | import android.util.SparseArray; 4 | 5 | /** 6 | * @author Jack Tony 7 | * @date 2015/8/29 8 | */ 9 | public class AdapterItemUtil { 10 | 11 | private SparseArray typeSArr = new SparseArray<>(); 12 | 13 | /** 14 | * @param type item的类型 15 | * 16 | * @return 通过object类型的type来得到int类型的type 17 | */ 18 | public int getIntType(Object type) { 19 | int index = typeSArr.indexOfValue(type); 20 | if (index == -1) { 21 | index = typeSArr.size(); 22 | // 如果没用这个type,就存入这个type 23 | typeSArr.put(index, type); 24 | } 25 | return index; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /recyclerview/src/main/java/com/lee/recyclerview/adapter/CommonRcvAdapter.java: -------------------------------------------------------------------------------- 1 | package com.lee.recyclerview.adapter; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.ViewGroup; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author Jack Tony 13 | * @date 2015/5/17 14 | */ 15 | public abstract class CommonRcvAdapter extends RecyclerView.Adapter { 16 | 17 | private final boolean DEBUG = false; 18 | 19 | private List mDataList; 20 | 21 | private Object mItemType; 22 | 23 | private AdapterItemUtil mUtil = new AdapterItemUtil(); 24 | 25 | protected CommonRcvAdapter(List data) { 26 | mDataList = data; 27 | } 28 | 29 | @Override 30 | public int getItemCount() { 31 | return mDataList.size(); 32 | } 33 | 34 | public List getDataList() { 35 | return mDataList; 36 | } 37 | 38 | /** 39 | * 可以被复写用于单条刷新等 40 | */ 41 | public void updateData(@NonNull List data) { 42 | mDataList = data; 43 | notifyDataSetChanged(); 44 | } 45 | 46 | @Override 47 | public long getItemId(int position) { 48 | return position; 49 | } 50 | 51 | /** 52 | * instead by{@link #getItemViewType(Object)} 53 | */ 54 | @Deprecated 55 | @Override 56 | public int getItemViewType(int position) { 57 | mItemType = getItemViewType(mDataList.get(position)); 58 | return mUtil.getIntType(mItemType); 59 | } 60 | 61 | public Object getItemViewType(T t) { 62 | return null; 63 | } 64 | 65 | @Override 66 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 67 | return new RcvAdapterItem(parent.getContext(), parent, getItemView(mItemType)); 68 | } 69 | 70 | @SuppressWarnings("unchecked") 71 | @Override 72 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 73 | if (DEBUG) { 74 | RcvAdapterItem item = (RcvAdapterItem) holder; 75 | item.itemView.setBackgroundColor(item.isNew ? 0xffff0000 : 0xff00ff00); 76 | item.isNew = false; 77 | } 78 | ((RcvAdapterItem) holder).getItem().onUpdateViews(mDataList.get(position), position); 79 | } 80 | 81 | public abstract 82 | @NonNull 83 | AdapterItem getItemView(Object type); 84 | 85 | private class RcvAdapterItem extends RecyclerView.ViewHolder { 86 | 87 | private AdapterItem mItem; 88 | 89 | public boolean isNew = true; // debug中才用到 90 | 91 | protected RcvAdapterItem(Context context, ViewGroup parent, AdapterItem item) { 92 | super(LayoutInflater.from(context).inflate(item.getLayoutResId(), parent, false)); 93 | mItem = item; 94 | mItem.onBindViews(itemView); 95 | mItem.onSetViews(); 96 | } 97 | 98 | protected AdapterItem getItem() { 99 | return mItem; 100 | } 101 | 102 | } 103 | 104 | 105 | 106 | } 107 | -------------------------------------------------------------------------------- /recyclerview/src/main/java/com/lee/recyclerview/adapter/MfCommonRcvAdapter.java: -------------------------------------------------------------------------------- 1 | package com.lee.recyclerview.adapter; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.util.Log; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.AdapterView; 8 | 9 | import java.util.List; 10 | 11 | 12 | /** 13 | * Created by lee on 15/10/28. 14 | * Email:lee131483@gmail.com 15 | */ 16 | public abstract class MfCommonRcvAdapter extends CommonRcvAdapter { 17 | 18 | private static final String TAG = "MfCommonRcvAdapter"; 19 | 20 | public AdapterView.OnItemClickListener mOnItemClickListener; 21 | 22 | public AdapterView.OnItemLongClickListener mOnItemLongClickListener; 23 | 24 | protected MfCommonRcvAdapter(List data) { 25 | super(data); 26 | } 27 | 28 | private static final int TYPE_HEADER = 0; 29 | 30 | private static final int TYPE_FOOTER = 1; 31 | 32 | private static final int TYPE_ITEM = 2; 33 | 34 | private static final int TYPE_EMPTY = 3; 35 | 36 | public View mHeaderView; 37 | 38 | public View mFooterView; 39 | 40 | public View mEmptyView; 41 | 42 | class SimpleViewHolder extends RecyclerView.ViewHolder { 43 | public SimpleViewHolder(View itemView) { 44 | super(itemView); 45 | } 46 | } 47 | 48 | @Override 49 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 50 | if (viewType == TYPE_HEADER && mHeaderView != null) { 51 | return new SimpleViewHolder(mHeaderView); 52 | } else if (viewType == TYPE_FOOTER && mFooterView != null) { 53 | return new SimpleViewHolder(mFooterView); 54 | } else if (viewType == TYPE_EMPTY && mEmptyView != null) { 55 | return new SimpleViewHolder(mEmptyView); 56 | } 57 | return super.onCreateViewHolder(parent, viewType); 58 | } 59 | 60 | @Override 61 | public int getItemCount() { 62 | int size = super.getItemCount(); 63 | if (size == 0 && null != mEmptyView) { 64 | size = 1; 65 | } else { 66 | if (null != mHeaderView) 67 | size++; 68 | if (null != mFooterView) 69 | size++; 70 | } 71 | return size; 72 | } 73 | 74 | @Override 75 | public int getItemViewType(int position) { 76 | int size = super.getItemCount(); 77 | if (size == 0 && null != mEmptyView) { 78 | return TYPE_EMPTY; 79 | } else if (position < getHeadViewSize()) { 80 | return TYPE_HEADER; 81 | } else if (position >= getHeadViewSize() + size) { 82 | return TYPE_FOOTER; 83 | } 84 | return TYPE_ITEM; 85 | } 86 | 87 | /** 88 | * 载入ViewHolder,这里仅仅处理header和footer视图的逻辑 89 | */ 90 | @Override 91 | public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) { 92 | 93 | if (super.getItemCount() == 0 && getItemCount() == 1 && null != mEmptyView && position == 0) { 94 | Log.d(TAG, "处理emptyView"); 95 | //处理emptyView 96 | } else if (null != mHeaderView && position == 0) { 97 | //处理headView 98 | Log.d(TAG, "处理headView"); 99 | } else if (null != mFooterView && position == getItemCount() - 1) { 100 | //处理footView 101 | Log.d(TAG, "处理footView"); 102 | } else { 103 | Log.d(TAG, "处理其他"); 104 | if (mHeaderView != null) { 105 | position--; 106 | } 107 | super.onBindViewHolder(viewHolder, position); 108 | 109 | final int pos = position; 110 | // 设置点击事件 111 | if (mOnItemClickListener != null) { 112 | viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 113 | @Override 114 | public void onClick(View v) { 115 | mOnItemClickListener.onItemClick(null, viewHolder.itemView, pos, pos); 116 | } 117 | }); 118 | } 119 | // 设置长按事件 120 | if (mOnItemLongClickListener != null) { 121 | viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { 122 | @Override 123 | public boolean onLongClick(View view) { 124 | return mOnItemLongClickListener.onItemLongClick(null, viewHolder.itemView, pos, pos); 125 | } 126 | }); 127 | } 128 | } 129 | } 130 | 131 | private int getHeadViewSize() { 132 | return mHeaderView == null ? 0 : 1; 133 | } 134 | 135 | private int getFooterViewSize() { 136 | return mFooterView == null ? 0 : 1; 137 | } 138 | 139 | 140 | private T getItem(int position) { 141 | return getDataList().get(position - getHeadViewSize()); 142 | } 143 | 144 | 145 | //remove a header from the adapter 146 | public void removeHeader(View header) { 147 | notifyItemRemoved(0); 148 | mHeaderView = null; 149 | } 150 | 151 | //add datas 152 | public void addDatas(List data) { 153 | getDataList().addAll(data); 154 | notifyDataSetChanged(); 155 | } 156 | 157 | //add data 158 | public void addData(T data) { 159 | getDataList().add(data); 160 | notifyItemInserted(getHeadViewSize() + getDataList().size() - 1); 161 | } 162 | 163 | //refresh data 164 | public void refreshData(List datas) { 165 | getDataList().clear(); 166 | addDatas(datas); 167 | } 168 | 169 | 170 | } 171 | -------------------------------------------------------------------------------- /recyclerview/src/main/java/com/lee/recyclerview/recyclerview/MfRecycylerVIew.java: -------------------------------------------------------------------------------- 1 | package com.lee.recyclerview.recyclerview; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.util.AttributeSet; 6 | import android.view.View; 7 | import android.widget.AdapterView; 8 | 9 | import com.lee.recyclerview.adapter.MfCommonRcvAdapter; 10 | 11 | /** 12 | * MfRecycylerView 13 | * 多功能 RecycylerView 14 | * Created by lee on 15/10/28. 15 | * Email:lee131483@gmail.com 16 | */ 17 | public class MfRecycylerView extends RecyclerView { 18 | 19 | private View mHeaderView; 20 | 21 | private View mFooterView; 22 | 23 | private View mEmptyView; 24 | 25 | public MfRecycylerView(Context context) { 26 | super(context); 27 | } 28 | 29 | public MfRecycylerView(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | } 32 | 33 | public MfRecycylerView(Context context, AttributeSet attrs, int defStyle) { 34 | super(context, attrs, defStyle); 35 | } 36 | 37 | 38 | public void addHeaderView(View headerView) { 39 | mHeaderView = headerView; 40 | } 41 | 42 | /** 43 | * @return recycle的头部视图 44 | */ 45 | public View getHeaderView() { 46 | return mHeaderView; 47 | } 48 | 49 | 50 | /** 51 | * 设置底部的视图 52 | */ 53 | public void addFooterView(View footerView) { 54 | mFooterView = footerView; 55 | } 56 | 57 | /** 58 | * 得到底部的视图 59 | */ 60 | public View getFooterView() { 61 | return mFooterView; 62 | } 63 | 64 | public View getEmptyView() { 65 | return mEmptyView; 66 | } 67 | 68 | public void setEmptyView(final View emptyView) { 69 | mEmptyView = emptyView; 70 | } 71 | 72 | @Override 73 | public void setAdapter(Adapter adapter) { 74 | super.setAdapter(adapter); 75 | if (adapter instanceof MfCommonRcvAdapter) { 76 | ((MfCommonRcvAdapter) adapter).mOnItemClickListener = mOnItemClickListener; 77 | ((MfCommonRcvAdapter) adapter).mOnItemLongClickListener = mOnItemLongClickListener; 78 | ((MfCommonRcvAdapter) adapter).mHeaderView = mHeaderView; 79 | ((MfCommonRcvAdapter) adapter).mFooterView = mFooterView; 80 | ((MfCommonRcvAdapter) adapter).mEmptyView = mEmptyView; 81 | }else{ 82 | throw new IllegalArgumentException("adapter must extends MfCommonRcvAdapter!"); 83 | } 84 | } 85 | 86 | /** 87 | * 平滑滚动到某个位置 88 | * 89 | * @param isAbsolute position是否是绝对的,如果是绝对的,那么header的位置就是0 90 | * 如果是相对的,那么position就是相对内容的list的位置 91 | */ 92 | public void smoothScrollToPosition(int position, boolean isAbsolute) { 93 | if (!isAbsolute && mHeaderView != null) { 94 | position++; 95 | } 96 | smoothScrollToPosition(position); 97 | } 98 | 99 | /** 100 | * 设置item的点击事件 101 | */ 102 | private static AdapterView.OnItemClickListener mOnItemClickListener = null; 103 | 104 | public void setOnItemClickListener(AdapterView.OnItemClickListener listener) { 105 | mOnItemClickListener = listener; 106 | } 107 | 108 | /** 109 | * 设置item的长按事件 110 | */ 111 | public static AdapterView.OnItemLongClickListener mOnItemLongClickListener = null; 112 | 113 | public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) { 114 | mOnItemLongClickListener = listener; 115 | } 116 | 117 | 118 | } 119 | -------------------------------------------------------------------------------- /recyclerview/src/main/res/values/ids.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /recyclerview/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | recyclerview 3 | 4 | -------------------------------------------------------------------------------- /recyclerview/src/test/java/com/lee/recyclerview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.lee.recyclerview; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /recyclerview_support_head_footer_v3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/XiaNaLee/AndroidRecyclerView/c05d4856613041d2e28af599306456e060f03ec1/recyclerview_support_head_footer_v3.gif -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':recyclerview' 2 | --------------------------------------------------------------------------------