├── .gitignore ├── .gitmodules ├── .idea └── vcs.xml ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── xj │ │ └── recyclerviewdemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── xj │ │ │ └── recyclerviewdemo │ │ │ ├── DataUtils.java │ │ │ ├── DividerSampleActivity.java │ │ │ ├── GridDividerItemDecoration.java │ │ │ ├── HorizontalSpacesDecoration.java │ │ │ ├── LinearSampleActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── RecyclerViewDivider.java │ │ │ ├── TextAdapter.java │ │ │ └── grid │ │ │ ├── GridSampleActivity.java │ │ │ ├── GridSampleActivity2.java │ │ │ └── ImageAdapter.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_divider_sample.xml │ │ ├── activity_grid_sample.xml │ │ ├── activity_grid_sample2.xml │ │ ├── activity_linear_sample.xml │ │ ├── activity_main.xml │ │ ├── item_divider.xml │ │ └── item_image.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_round.png │ │ └── ty.jpg │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_round.png │ │ └── ty1.jpg │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── xj │ └── recyclerviewdemo │ └── ExampleUnitTest.java ├── build.gradle ├── config.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches/build_file_checksums.ser 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "submodules/BaseLibrary"] 2 | path = submodules/BaseLibrary 3 | url = git@github.com:gdutxiaoxu/BaseLibrary.git 4 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.xj.recyclerviewdemo" 7 | minSdkVersion 15 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | implementation project(':submodules:BaseLibrary') 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/xj/recyclerviewdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.xj.recyclerviewdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/DataUtils.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.support.annotation.DrawableRes; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by jun xu on 19-4-10. 10 | */ 11 | public class DataUtils { 12 | 13 | public static List produceList(int count, String key) { 14 | ArrayList datas = new ArrayList<>(); 15 | for (int i = 0; i < count; i++) { 16 | datas.add(key + ":Fragment item :" + i); 17 | } 18 | return datas; 19 | } 20 | 21 | public static List produceImageList(@DrawableRes int imageRes, int count) { 22 | ArrayList datas = new ArrayList<>(); 23 | for (int i = 0; i < count; i++) { 24 | datas.add(imageRes); 25 | } 26 | return datas; 27 | } 28 | 29 | 30 | public static List produceImageList(int count) { 31 | ArrayList datas = new ArrayList<>(); 32 | for (int i = 0; i < count; i++) { 33 | datas.add(R.mipmap.ty1); 34 | } 35 | return datas; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/DividerSampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | 9 | import com.xj.library.utils.DisplayUtils; 10 | 11 | import java.util.List; 12 | 13 | public class DividerSampleActivity extends AppCompatActivity { 14 | 15 | private RecyclerView mRecyclerView; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_divider_sample); 21 | initView(); 22 | } 23 | 24 | private void initView() { 25 | mRecyclerView = findViewById(R.id.recycler_view); 26 | LinearLayoutManager layoutManager = new LinearLayoutManager(this); 27 | 28 | mRecyclerView.setLayoutManager(layoutManager); 29 | RecyclerViewDivider recyclerViewDivider = new RecyclerViewDivider(this); 30 | recyclerViewDivider.setHorizontaloffset(DisplayUtils.dp2px(this, 15), 0); 31 | recyclerViewDivider.setDividerColor(Color.GRAY); 32 | recyclerViewDivider.setDividerHeight(DisplayUtils.dp2px(this, 0.5f)); 33 | mRecyclerView.addItemDecoration(recyclerViewDivider); 34 | List chatList = DataUtils.produceList(30, "chat"); 35 | TextAdapter textAdapter = new TextAdapter(this, chatList); 36 | mRecyclerView.setAdapter(textAdapter); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/GridDividerItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.content.Context; 4 | import android.graphics.Rect; 5 | import android.support.v7.widget.GridLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.support.v7.widget.StaggeredGridLayoutManager; 8 | import android.util.Log; 9 | import android.view.View; 10 | 11 | /** 12 | * 博客地址:http://blog.csdn.net/gdutxiaoxu 13 | * 14 | * @author xujun 15 | * @time 19-4-17 16 | */ 17 | public class GridDividerItemDecoration extends RecyclerView.ItemDecoration { 18 | 19 | private static final String TAG = "GridDividerItemDeco"; 20 | 21 | private Context mContext; 22 | 23 | private int mFirstAndLastColumnW; //您所需指定的间隔宽度,主要为第一列和最后一列与父控件的间隔;行间距,列间距将动态分配 24 | private int mFirstRowTopMargin = 0; //第一行顶部是否需要间隔 25 | 26 | private int mLastRowBottomMargin = 0; 27 | 28 | private int mSpanCount = 0; 29 | private int mScreenW = 0; 30 | private int mItemDistance; 31 | 32 | 33 | /** 34 | * @param firstAndLastColumnW 间隔宽度 35 | * @param firstRowTopMargin 第一行顶部是否需要间隔 36 | */ 37 | public GridDividerItemDecoration(Context context, int firstAndLastColumnW, int firstRowTopMargin, int lastRowBottomMargin) { 38 | mContext = context; 39 | mFirstAndLastColumnW = firstAndLastColumnW; 40 | mFirstRowTopMargin = firstRowTopMargin; 41 | mLastRowBottomMargin = lastRowBottomMargin; 42 | } 43 | 44 | 45 | public void setFirstRowTopMargin(int firstRowTopMargin) { 46 | mFirstRowTopMargin = firstRowTopMargin; 47 | } 48 | 49 | public void setLastRowBottomMargin(int lastRowBottomMargin) { 50 | mLastRowBottomMargin = lastRowBottomMargin; 51 | } 52 | 53 | public void setItemDistance(int itemDistance) { 54 | mItemDistance = itemDistance; 55 | } 56 | 57 | @Override 58 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 59 | super.getItemOffsets(outRect, view, parent, state); 60 | 61 | int top = 0; 62 | int left; 63 | int right; 64 | int bottom; 65 | 66 | int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); 67 | mSpanCount = getSpanCount(parent); 68 | int childCount = parent.getAdapter().getItemCount(); 69 | 70 | // 屏幕宽度-View的宽度*spanCount 得到屏幕剩余空间 71 | int maxDividerWidth = getMaxDividerWidth(view); 72 | int spaceWidth = mFirstAndLastColumnW;//首尾两列与父布局之间的间隔 73 | // 除去首尾两列,item与item之间的距离 74 | int eachItemWidth = maxDividerWidth / mSpanCount; 75 | int dividerItemWidth = (maxDividerWidth - 2 * spaceWidth) / (mSpanCount - 1);//item与item之间的距离 76 | 77 | left = itemPosition % mSpanCount * (dividerItemWidth - eachItemWidth) + spaceWidth; 78 | 79 | right = eachItemWidth - left; 80 | bottom = dividerItemWidth; 81 | 82 | // 首行 83 | if (mFirstRowTopMargin > 0 && isFirstRow(parent, itemPosition, mSpanCount, childCount)) { 84 | top = mFirstRowTopMargin; 85 | } 86 | 87 | //最后一行 88 | if (isLastRow(parent, itemPosition, mSpanCount, childCount)) { 89 | if (mLastRowBottomMargin < 0) { 90 | bottom = 0; 91 | } else { 92 | bottom = mLastRowBottomMargin; 93 | } 94 | } 95 | 96 | Log.i(TAG, "getItemOffsets: dividerItemWidth =" + dividerItemWidth + "eachItemWidth = " + eachItemWidth); 97 | 98 | Log.i(TAG, "getItemOffsets: itemPosition =" + itemPosition + " left = " + left + " top = " + top 99 | + " right = " + right + " bottom = " + bottom); 100 | 101 | outRect.set(left, top, right, bottom); 102 | } 103 | 104 | /** 105 | * 获取Item View的大小,若无则自动分配空间 106 | * 并根据 屏ge幕宽度-View的宽度*spanCount 得到屏幕剩余空间 107 | * 108 | * @param view 109 | * @return 110 | */ 111 | private int getMaxDividerWidth(View view) { 112 | int itemWidth = view.getLayoutParams().width; 113 | int itemHeight = view.getLayoutParams().height; 114 | Log.i(TAG, "getMaxDividerWidth: itemWidth =" + itemWidth); 115 | 116 | int screenWidth = getScreenWidth(); 117 | 118 | int maxDividerWidth = screenWidth - itemWidth * mSpanCount; 119 | 120 | if (itemHeight < 0 || itemWidth < 0 || maxDividerWidth <= (mSpanCount - 1) * mFirstAndLastColumnW) { 121 | view.getLayoutParams().width = getAttachCloumnWidth(); 122 | view.getLayoutParams().height = getAttachCloumnWidth(); 123 | maxDividerWidth = screenWidth - view.getLayoutParams().width * mSpanCount; 124 | } 125 | 126 | return maxDividerWidth; 127 | } 128 | 129 | private int getScreenWidth() { 130 | Log.i(TAG, "getScreenWidth: mScreenW =" + mScreenW); 131 | if (mScreenW > 0) { 132 | return mScreenW; 133 | } 134 | 135 | mScreenW = mContext.getResources().getDisplayMetrics().widthPixels > mContext.getResources().getDisplayMetrics().heightPixels 136 | ? mContext.getResources().getDisplayMetrics().heightPixels : mContext.getResources().getDisplayMetrics().widthPixels; 137 | return mScreenW; 138 | } 139 | 140 | /** 141 | * 根据屏幕宽度和item数量分配 item View的width和height 142 | * 143 | * @return 144 | */ 145 | private int getAttachCloumnWidth() { 146 | int itemWidth = 0; 147 | int spaceWidth = 0; 148 | try { 149 | int width = getScreenWidth(); 150 | spaceWidth = 2 * mFirstAndLastColumnW; 151 | itemWidth = (width - spaceWidth) / mSpanCount - 40; 152 | } catch (Exception e) { 153 | e.printStackTrace(); 154 | } 155 | 156 | return itemWidth; 157 | } 158 | 159 | /** 160 | * 判读是否是第一列 161 | * 162 | * @param parent 163 | * @param pos 164 | * @param spanCount 165 | * @return 166 | */ 167 | private boolean isFirstColumn(RecyclerView parent, int pos, int spanCount) { 168 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 169 | if (layoutManager instanceof GridLayoutManager) { 170 | if (pos % spanCount == 0) { 171 | return true; 172 | } 173 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 174 | int orientation = ((StaggeredGridLayoutManager) layoutManager) 175 | .getOrientation(); 176 | if (orientation == StaggeredGridLayoutManager.VERTICAL) { 177 | if (pos % spanCount == 0) {// 第一列 178 | return true; 179 | } 180 | } else { 181 | 182 | } 183 | } 184 | return false; 185 | } 186 | 187 | /** 188 | * 判断是否是最后一列 189 | * 190 | * @param parent 191 | * @param pos 192 | * @param spanCount 193 | * @param childCount 194 | * @return 195 | */ 196 | private boolean isLastColumn(RecyclerView parent, int pos, int spanCount) { 197 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 198 | if (layoutManager instanceof GridLayoutManager) { 199 | if ((pos + 1) % spanCount == 0) {// 如果是最后一列,则不需要绘制右边 200 | return true; 201 | } 202 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 203 | int orientation = ((StaggeredGridLayoutManager) layoutManager) 204 | .getOrientation(); 205 | if (orientation == StaggeredGridLayoutManager.VERTICAL) { 206 | if ((pos + 1) % spanCount == 0) {// 最后一列 207 | return true; 208 | } 209 | } else { 210 | 211 | } 212 | } 213 | return false; 214 | } 215 | 216 | /** 217 | * 判读是否是最后一行 218 | * 219 | * @param parent 220 | * @param pos 221 | * @param spanCount 222 | * @param childCount 223 | * @return 224 | */ 225 | private boolean isLastRow(RecyclerView parent, int pos, int spanCount, int childCount) { 226 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 227 | if (layoutManager instanceof GridLayoutManager) { 228 | int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1; 229 | return lines == pos / spanCount + 1; 230 | } 231 | return false; 232 | } 233 | 234 | /** 235 | * 判断是否是第一行 236 | * 237 | * @param parent 238 | * @param pos 239 | * @param spanCount 240 | * @param childCount 241 | * @return 242 | */ 243 | private boolean isFirstRow(RecyclerView parent, int pos, int spanCount, int childCount) { 244 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 245 | if (layoutManager instanceof GridLayoutManager) { 246 | if ((pos / spanCount + 1) == 1) { 247 | return true; 248 | } else { 249 | return false; 250 | } 251 | } 252 | return false; 253 | } 254 | 255 | /** 256 | * 获取列数 257 | * 258 | * @param parent 259 | * @return 260 | */ 261 | private int getSpanCount(RecyclerView parent) { 262 | int spanCount = -1; 263 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager(); 264 | if (layoutManager instanceof GridLayoutManager) { 265 | spanCount = ((GridLayoutManager) layoutManager).getSpanCount(); 266 | } else if (layoutManager instanceof StaggeredGridLayoutManager) { 267 | spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount(); 268 | } 269 | return spanCount; 270 | } 271 | } -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/HorizontalSpacesDecoration.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.graphics.Rect; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.View; 6 | 7 | /** 8 | * 博客地址:http://blog.csdn.net/gdutxiaoxu 9 | * 10 | * @author xujun 11 | * @time 19-4-17 12 | */ 13 | public class HorizontalSpacesDecoration extends RecyclerView.ItemDecoration { 14 | 15 | private final Rect firstAndlastRect; 16 | private final Rect space; 17 | 18 | public HorizontalSpacesDecoration(Rect space, Rect firstAndlastRect) { 19 | this.space = space; 20 | this.firstAndlastRect = firstAndlastRect; 21 | } 22 | 23 | @Override 24 | public void getItemOffsets(Rect outRect, View view, 25 | RecyclerView parent, RecyclerView.State state) { 26 | int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); 27 | int childCount = parent.getAdapter().getItemCount(); 28 | 29 | if (itemPosition == 0) { 30 | outRect.left = firstAndlastRect.left; 31 | outRect.right = firstAndlastRect.right; 32 | outRect.bottom = firstAndlastRect.bottom; 33 | outRect.top = firstAndlastRect.top; 34 | return; 35 | } 36 | 37 | if (itemPosition == childCount - 1) { 38 | outRect.left = 0; 39 | outRect.right = firstAndlastRect.left; 40 | outRect.bottom = firstAndlastRect.bottom; 41 | outRect.top = firstAndlastRect.top; 42 | return; 43 | } 44 | 45 | setOutRect(outRect, space); 46 | } 47 | 48 | private void setOutRect(Rect outRect, Rect rect) { 49 | outRect.left = rect.left; 50 | outRect.right = rect.right; 51 | outRect.bottom = rect.bottom; 52 | outRect.top = rect.top; 53 | } 54 | } -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/LinearSampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.graphics.Rect; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | 9 | import com.xj.library.utils.DisplayUtils; 10 | import com.xj.recyclerviewdemo.grid.ImageAdapter; 11 | 12 | import java.util.List; 13 | 14 | public class LinearSampleActivity extends AppCompatActivity { 15 | 16 | private RecyclerView mRecyclerView; 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_linear_sample); 22 | int ll_root = R.id.ll_root; 23 | initView(); 24 | } 25 | 26 | private void initView() { 27 | mRecyclerView = findViewById(R.id.recycler_view); 28 | LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); 29 | 30 | mRecyclerView.setLayoutManager(layoutManager); 31 | 32 | int i = DisplayUtils.dp2px(this, 25); 33 | Rect rect = new Rect(0, i, i, 0); 34 | 35 | int j = DisplayUtils.dp2px(this, 15); 36 | Rect firstAndLastRect = new Rect(j, i, i, 0); 37 | HorizontalSpacesDecoration spacesDecoration = new HorizontalSpacesDecoration(rect, firstAndLastRect); 38 | mRecyclerView.addItemDecoration(spacesDecoration); 39 | List integers = DataUtils.produceImageList(R.mipmap.ty1, 30); 40 | ImageAdapter imageAdapter = new ImageAdapter(this, integers); 41 | mRecyclerView.setAdapter(imageAdapter); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | import com.xj.recyclerviewdemo.grid.GridSampleActivity; 9 | import com.xj.recyclerviewdemo.grid.GridSampleActivity2; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_main); 17 | } 18 | 19 | public void onButtonClick(View view) { 20 | switch (view.getId()) { 21 | case R.id.btn_0: 22 | startActivity(new Intent(this, DividerSampleActivity.class)); 23 | break; 24 | case R.id.btn_1: 25 | startActivity(new Intent(this, GridSampleActivity.class)); 26 | break; 27 | case R.id.btn_2: 28 | startActivity(new Intent(this, LinearSampleActivity.class)); 29 | break; 30 | case R.id.btn_3: 31 | startActivity(new Intent(this, GridSampleActivity2.class)); 32 | break; 33 | default: 34 | break; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/RecyclerViewDivider.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Paint; 8 | import android.graphics.Rect; 9 | import android.graphics.drawable.Drawable; 10 | import android.support.v4.content.ContextCompat; 11 | import android.support.v7.widget.LinearLayoutManager; 12 | import android.support.v7.widget.RecyclerView; 13 | import android.util.Log; 14 | import android.view.View; 15 | 16 | 17 | /** 18 | * 博客地址:http://blog.csdn.net/gdutxiaoxu 19 | * 20 | * @author xujun 21 | * @time 19-4-17 22 | */ 23 | public class RecyclerViewDivider extends RecyclerView.ItemDecoration { 24 | 25 | private static final String TAG = "RecyclerViewDivider"; 26 | 27 | private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; 28 | private static final int DIVIDER_COLOR = Color.parseColor("#efefef"); 29 | 30 | private Paint mPaint; 31 | private Drawable mDivider; 32 | private int mDividerHeight = 1; 33 | 34 | //The direction of the list:LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL 35 | private int mOrientation; 36 | private int mLeftOffset; 37 | private int mRightOffset; 38 | private boolean mIsShowLastDivider; 39 | private int mDividerColor; 40 | private int mStart = 0; 41 | private int mEnd = 0; 42 | 43 | 44 | public RecyclerViewDivider(Context context) { 45 | this(context, LinearLayoutManager.VERTICAL, 1, DIVIDER_COLOR); 46 | } 47 | 48 | public RecyclerViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) { 49 | this(context, orientation); 50 | 51 | mDividerColor = dividerColor; 52 | mDividerHeight = dividerHeight; 53 | 54 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 55 | mPaint.setColor(dividerColor); 56 | mPaint.setStyle(Paint.Style.FILL); 57 | } 58 | 59 | public RecyclerViewDivider(Context context, int orientation) { 60 | if (orientation != LinearLayoutManager.VERTICAL 61 | && orientation != LinearLayoutManager.HORIZONTAL) { 62 | throw new IllegalArgumentException("Please input right parameter"); 63 | } 64 | 65 | mOrientation = orientation; 66 | 67 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 68 | mDivider = a.getDrawable(0); 69 | a.recycle(); 70 | } 71 | 72 | public RecyclerViewDivider(Context context, int orientation, int drawableId) { 73 | this(context, orientation); 74 | 75 | mDivider = ContextCompat.getDrawable(context, drawableId); 76 | mDividerHeight = mDivider.getIntrinsicHeight(); 77 | } 78 | 79 | /** 80 | * Set the offset of the horizontal split line 81 | * 82 | * @param leftOffset 83 | * @param rightOffset 84 | */ 85 | public void setHorizontaloffset(int leftOffset, int rightOffset) { 86 | mLeftOffset = leftOffset; 87 | mRightOffset = rightOffset; 88 | } 89 | 90 | /** 91 | * Set whether to display the last split line, not displayed by default 92 | * 93 | * @param isShowLastDivider 94 | */ 95 | public void setShowLastDivider(boolean isShowLastDivider) { 96 | mIsShowLastDivider = isShowLastDivider; 97 | } 98 | 99 | /** 100 | * split line display range 101 | *

102 | * fisrt = 0 + start; 103 | * last = childSize - 1 - end - mIsShowLastDivider ? 0 : 1 104 | * 105 | * @param start 106 | * @param end 107 | */ 108 | public void setDividerStartAndEndOffsetCount(int start, int end) { 109 | mStart = start; 110 | mEnd = end; 111 | } 112 | 113 | public void setDividerHeight(int dividerHeight) { 114 | mDividerHeight = dividerHeight; 115 | } 116 | 117 | public void setDividerColor(int dividerColor) { 118 | mDividerColor = dividerColor; 119 | if (mPaint != null) { 120 | mPaint.setColor(mDividerColor); 121 | } 122 | } 123 | 124 | @Override 125 | public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 126 | super.getItemOffsets(outRect, view, parent, state); 127 | outRect.set(0, 0, 0, mDividerHeight); 128 | } 129 | 130 | @Override 131 | public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 132 | super.onDraw(c, parent, state); 133 | if (mOrientation == LinearLayoutManager.VERTICAL) { 134 | drawVerticalDivider(c, parent); 135 | } else { 136 | drawHorizontalDivider(c, parent); 137 | } 138 | } 139 | 140 | //Draw item dividing line 141 | private void drawVerticalDivider(Canvas canvas, RecyclerView parent) { 142 | // mLeftOffset 为自己设置的左边偏移量 143 | final int left = parent.getPaddingLeft() + mLeftOffset; 144 | // mRightOffset 为设置的右边偏移量 145 | final int right = parent.getMeasuredWidth() - parent.getPaddingRight() + mRightOffset; 146 | final int childSize = parent.getChildCount(); 147 | 148 | if (childSize <= 0) { 149 | return; 150 | } 151 | 152 | // 从第一个 item 开始绘制 153 | int first = mStart; 154 | // 到第几个 item 绘制结束 155 | int last = childSize - mEnd - (mIsShowLastDivider ? 0 : 1); 156 | Log.d(TAG, " last = " + last + " childSize =" + childSize + "left = " + left); 157 | 158 | if (last <= 0) { 159 | return; 160 | } 161 | 162 | for (int i = first; i < last; i++) { 163 | drawableVerticalDivider(canvas, parent, left, right, i, mDividerHeight); 164 | } 165 | 166 | } 167 | 168 | private void drawableVerticalDivider(Canvas canvas, RecyclerView parent, int left, int right, int i, int dividerHeight) { 169 | final View child = parent.getChildAt(i); 170 | 171 | if (child == null) { 172 | return; 173 | } 174 | 175 | RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); 176 | final int top = child.getBottom() + layoutParams.bottomMargin; 177 | final int bottom = top + dividerHeight; 178 | 179 | // 适配 drawable 180 | if (mDivider != null) { 181 | mDivider.setBounds(left, top, right, bottom); 182 | mDivider.draw(canvas); 183 | } 184 | 185 | // 适配分割线 186 | if (mPaint != null) { 187 | canvas.drawRect(left, top, right, bottom, mPaint); 188 | } 189 | } 190 | 191 | // Draw vertical item dividing line 192 | private void drawHorizontalDivider(Canvas canvas, RecyclerView parent) { 193 | final int top = parent.getPaddingTop(); 194 | final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom(); 195 | final int childSize = parent.getChildCount(); 196 | 197 | for (int i = 0; i < childSize; i++) { 198 | final View child = parent.getChildAt(i); 199 | RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams(); 200 | final int left = child.getRight() + layoutParams.rightMargin; 201 | final int right = left + mDividerHeight; 202 | 203 | if (mDivider != null) { 204 | mDivider.setBounds(left, top, right, bottom); 205 | mDivider.draw(canvas); 206 | } 207 | 208 | if (mPaint != null) { 209 | canvas.drawRect(left, top, right, bottom, mPaint); 210 | } 211 | } 212 | } 213 | 214 | } -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/TextAdapter.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo; 2 | 3 | import android.content.Context; 4 | import android.widget.TextView; 5 | 6 | import com.xj.library.recyclerView.CommonAdapter; 7 | import com.xj.library.recyclerView.base.ViewHolder; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * Created by jun xu on 19-4-10. 13 | */ 14 | public class TextAdapter extends CommonAdapter { 15 | 16 | public TextAdapter(Context context, List datas) { 17 | super(context, R.layout.item_divider, datas); 18 | } 19 | 20 | @Override 21 | protected void convert(ViewHolder holder, String s, int position) { 22 | TextView tvMsg = holder.getView(R.id.tv_msg); 23 | tvMsg.setText(s); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/grid/GridSampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo.grid; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.GridLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | 8 | import com.xj.library.utils.DisplayUtils; 9 | import com.xj.recyclerviewdemo.DataUtils; 10 | import com.xj.recyclerviewdemo.GridDividerItemDecoration; 11 | import com.xj.recyclerviewdemo.R; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * 博客地址:http://blog.csdn.net/gdutxiaoxu 17 | * @author xujun 18 | * @time 19-4-17 19 | */ 20 | public class GridSampleActivity extends AppCompatActivity { 21 | 22 | RecyclerView mRecyclerView; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_grid_sample); 28 | initView(); 29 | } 30 | 31 | private void initView() { 32 | mRecyclerView = findViewById(R.id.recycler_view); 33 | GridLayoutManager layoutManager = new GridLayoutManager(this, 3); 34 | 35 | mRecyclerView.setLayoutManager(layoutManager); 36 | int firstAndLastColumnW = DisplayUtils.dp2px(this, 15); 37 | int firstRowTopMargin = DisplayUtils.dp2px(this, 15); 38 | GridDividerItemDecoration gridDividerItemDecoration = 39 | new GridDividerItemDecoration(this, firstAndLastColumnW, firstRowTopMargin, firstRowTopMargin); 40 | gridDividerItemDecoration.setFirstRowTopMargin(firstRowTopMargin); 41 | gridDividerItemDecoration.setLastRowBottomMargin(firstRowTopMargin); 42 | mRecyclerView.addItemDecoration(gridDividerItemDecoration); 43 | List imageList = DataUtils.produceImageList(30); 44 | ImageAdapter imageAdapter = new ImageAdapter(this, imageList); 45 | 46 | mRecyclerView.setAdapter(imageAdapter); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/grid/GridSampleActivity2.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo.grid; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.GridLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | 8 | import com.xj.library.utils.DisplayUtils; 9 | import com.xj.recyclerviewdemo.DataUtils; 10 | import com.xj.recyclerviewdemo.GridDividerItemDecoration; 11 | import com.xj.recyclerviewdemo.R; 12 | 13 | import java.util.List; 14 | 15 | public class GridSampleActivity2 extends AppCompatActivity { 16 | 17 | 18 | public static final int SPAN_COUNT = 3; 19 | RecyclerView mRecyclerView; 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_grid_sample); 25 | initView(); 26 | } 27 | 28 | private void initView() { 29 | mRecyclerView = findViewById(R.id.recycler_view); 30 | GridLayoutManager layoutManager = new GridLayoutManager(this, SPAN_COUNT); 31 | 32 | mRecyclerView.setLayoutManager(layoutManager); 33 | int firstAndLastColumnW = DisplayUtils.dp2px(this, 15); 34 | int firstRowTopMargin = DisplayUtils.dp2px(this, 15); 35 | GridDividerItemDecoration gridDividerItemDecoration = 36 | new GridDividerItemDecoration(this, firstAndLastColumnW, firstRowTopMargin, firstRowTopMargin); 37 | gridDividerItemDecoration.setFirstRowTopMargin(firstRowTopMargin); 38 | gridDividerItemDecoration.setLastRowBottomMargin(firstRowTopMargin); 39 | int itemWidth = (DisplayUtils.getScreenWidth(this) 40 | - DisplayUtils.dp2px(this, 20) * (SPAN_COUNT - 1) - firstAndLastColumnW * 2) / SPAN_COUNT; 41 | 42 | mRecyclerView.addItemDecoration(gridDividerItemDecoration); 43 | List imageList = DataUtils.produceImageList(30); 44 | ImageAdapter imageAdapter = new ImageAdapter(this, imageList); 45 | imageAdapter.setWidth(itemWidth); 46 | mRecyclerView.setAdapter(imageAdapter); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/xj/recyclerviewdemo/grid/ImageAdapter.java: -------------------------------------------------------------------------------- 1 | package com.xj.recyclerviewdemo.grid; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.DrawableRes; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | 10 | import com.bumptech.glide.Glide; 11 | import com.xj.library.recyclerView.CommonAdapter; 12 | import com.xj.library.recyclerView.base.ViewHolder; 13 | import com.xj.recyclerviewdemo.R; 14 | 15 | import java.util.List; 16 | 17 | /** 18 | * 博客地址:http://blog.csdn.net/gdutxiaoxu 19 | * 20 | * @author xujun 21 | * @time 19-4-17 22 | */ 23 | public class ImageAdapter extends CommonAdapter { 24 | 25 | private static final String TAG = "ImageAdapter"; 26 | 27 | private int mWidth; 28 | 29 | public ImageAdapter(Context context, List datas) { 30 | super(context, R.layout.item_image, datas); 31 | } 32 | 33 | public void setWidth(int width) { 34 | mWidth = width; 35 | } 36 | 37 | 38 | @Override 39 | protected void onViewHolderCreate(ViewGroup parent, ViewHolder holder, int viewType) { 40 | super.onViewHolderCreate(parent, holder, viewType); 41 | View convertView = holder.getConvertView(); 42 | Log.i(TAG, "onViewHolderCreate: mWidth =" + mWidth); 43 | if (mWidth > 0) { 44 | convertView.getLayoutParams().width = mWidth; 45 | } 46 | 47 | 48 | } 49 | 50 | @Override 51 | protected void convert(ViewHolder holder, @DrawableRes Integer integer, int position) { 52 | ImageView iv = holder.getView(R.id.iv); 53 | Glide.with(mContext).load(integer).into(iv); 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_divider_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_grid_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_grid_sample2.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_linear_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 |