├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── org │ │ └── cgsdream │ │ └── demo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── org │ │ │ └── cgsdream │ │ │ └── demo │ │ │ ├── Cloneable.kt │ │ │ ├── DiffCallback.kt │ │ │ ├── FoldAdapter.kt │ │ │ ├── FoldViewHolder.kt │ │ │ ├── MainActivity.kt │ │ │ ├── PinnedSectionItemDecoration.kt │ │ │ ├── Section.kt │ │ │ ├── data.kt │ │ │ └── view │ │ │ ├── SectionHeaderView.kt │ │ │ ├── SectionItemView.kt │ │ │ └── SectionLoadingView.kt │ └── res │ │ ├── color │ │ └── s_topbar_btn_color.xml │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── icon_cell_arrow.xml │ │ ├── layout │ │ └── main_layout.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 │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── org │ └── cgsdream │ └── demo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.bin 2 | *.iml 3 | .idea 4 | .gradle 5 | /local.properties 6 | /.idea/workspace.xml 7 | /.idea/libraries 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RecyclerViewFoldDemo 2 | 3 | 博文: http://blog.cgsdream.org/2018/01/28/recyclerview-fold/ 4 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'kotlin-kapt' 5 | 6 | android { 7 | compileSdkVersion parent.ext.compileSdkVersion 8 | buildToolsVersion parent.ext.buildToolsVersion 9 | defaultConfig { 10 | applicationId "org.cgsdream.demo" 11 | minSdkVersion parent.ext.minSdkVersion 12 | targetSdkVersion parent.ext.targetSdkVersion 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | configurations.all { 26 | resolutionStrategy { 27 | force "com.android.support:support-annotations:${supportVersion}" 28 | force "com.android.support:recyclerview-v7:$supportVersion" 29 | force "com.android.support:appcompat-v7:$supportVersion" 30 | force "com.android.support:design:$supportVersion" 31 | force "com.android.support:support-vector-drawable:$supportVersion" 32 | } 33 | } 34 | 35 | dependencies { 36 | implementation fileTree(dir: 'libs', include: ['*.jar']) 37 | implementation "com.android.support:appcompat-v7:${supportVersion}" 38 | implementation "com.qmuiteam:qmui:${qmuiVersion}" 39 | 40 | implementation "com.jakewharton:butterknife:${butterknifeVersion}" 41 | kapt "com.jakewharton:butterknife-compiler:${butterknifeVersion}" 42 | 43 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 44 | 45 | testImplementation 'junit:junit:4.12' 46 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 47 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 48 | } 49 | -------------------------------------------------------------------------------- /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/org/cgsdream/demo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo; 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("org.cgsdream.demo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/Cloneable.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo 2 | 3 | /** 4 | * Created by cgspine on 2018/1/28. 5 | */ 6 | 7 | interface Cloneable{ 8 | fun clone(): T 9 | } -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/DiffCallback.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo 2 | 3 | import android.support.v7.util.DiffUtil 4 | import android.util.SparseArray 5 | 6 | /** 7 | * Created by cgspine on 2018/1/26. 8 | */ 9 | 10 | class DiffCallback, T: Cloneable>(private val oldList: List>, private val newList: List>) : DiffUtil.Callback() { 11 | 12 | private val mOldSectionIndex: SparseArray = SparseArray() 13 | private val mOldItemIndex: SparseArray = SparseArray() 14 | 15 | private val mNewSectionIndex: SparseArray = SparseArray() 16 | private val mNewItemIndex: SparseArray = SparseArray() 17 | 18 | init { 19 | generateIndex(oldList, mOldSectionIndex, mOldItemIndex) 20 | generateIndex(newList, mNewSectionIndex, mNewItemIndex) 21 | 22 | } 23 | 24 | override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { 25 | 26 | val oldSectionIndex = mOldSectionIndex[oldItemPosition] 27 | val oldItemIndex = mOldItemIndex[oldItemPosition] 28 | val oldModel = oldList[oldSectionIndex] 29 | 30 | val newSectionIndex = mNewSectionIndex[newItemPosition] 31 | val newItemIndex = mNewItemIndex[newItemPosition] 32 | val newModel = newList[newSectionIndex] 33 | 34 | if (oldModel.header != newModel.header) { 35 | return false 36 | } 37 | 38 | if (oldItemIndex < 0 && oldItemIndex == newItemIndex) { 39 | return true 40 | } 41 | 42 | if (oldItemIndex < 0 || newItemIndex < 0) { 43 | return false 44 | } 45 | return oldModel.list[oldItemIndex] == newModel.list[newItemIndex] 46 | } 47 | 48 | override fun getOldListSize() = mOldSectionIndex.size() 49 | 50 | override fun getNewListSize() = mNewSectionIndex.size() 51 | 52 | override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { 53 | 54 | val oldSectionIndex = mOldSectionIndex[oldItemPosition] 55 | val oldItemIndex = mOldItemIndex[oldItemPosition] 56 | val oldModel = oldList[oldSectionIndex] 57 | 58 | val newSectionIndex = mNewSectionIndex[newItemPosition] 59 | val newModel = newList[newSectionIndex] 60 | 61 | if (oldItemIndex == ITEM_INDEX_SECTION_HEADER) { 62 | return oldModel.isFold == newModel.isFold 63 | } 64 | 65 | if (oldItemIndex == ITEM_INDEX_LOAD_BEFORE || oldItemIndex == ITEM_INDEX_LOAD_AFTER) { 66 | // load more 强制返回 false,这样可以通过 FolderAdapter.onViewAttachedToWindow 触发 load more 67 | return false 68 | } 69 | 70 | return true 71 | } 72 | 73 | companion object { 74 | val ITEM_INDEX_SECTION_HEADER = -1 75 | val ITEM_INDEX_LOAD_BEFORE = -2 76 | val ITEM_INDEX_LOAD_AFTER = -3 77 | 78 | fun , T: Cloneable> generateIndex(list: List>, 79 | sectionIndex: SparseArray, 80 | itemIndex: SparseArray){ 81 | sectionIndex.clear() 82 | itemIndex.clear() 83 | var i = 0 84 | list.forEachIndexed { index, it -> 85 | if (!it.isLocked) { 86 | sectionIndex.append(i, index) 87 | itemIndex.append(i, ITEM_INDEX_SECTION_HEADER) 88 | i++ 89 | if (!it.isFold && it.count() > 0) { 90 | if (it.hasBefore) { 91 | sectionIndex.append(i, index) 92 | itemIndex.append(i, ITEM_INDEX_LOAD_BEFORE) 93 | i++ 94 | } 95 | 96 | for (j in 0 until it.count()) { 97 | sectionIndex.append(i, index) 98 | itemIndex.append(i, j) 99 | i++ 100 | } 101 | 102 | if (it.hasAfter) { 103 | sectionIndex.append(i, index) 104 | itemIndex.append(i, ITEM_INDEX_LOAD_AFTER) 105 | i++ 106 | } 107 | } 108 | } 109 | } 110 | } 111 | } 112 | } -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/FoldAdapter.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo 2 | 3 | import android.content.Context 4 | import android.support.v7.util.DiffUtil 5 | import android.support.v7.widget.RecyclerView 6 | import android.util.SparseArray 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import org.cgsdream.demo.view.SectionHeaderView 10 | import org.cgsdream.demo.view.SectionItemView 11 | import org.cgsdream.demo.view.SectionLoadingView 12 | import java.util.* 13 | 14 | /** 15 | * Created by cgspine on 2018/1/26. 16 | */ 17 | 18 | class FoldAdapter(private val context: Context) : RecyclerView.Adapter() { 19 | companion object { 20 | val ITEM_TYPE_SECTION_HEADER = 0 21 | val ITEM_TYPE_SECTION_ITEM = 1 22 | val ITEM_TYPE_SECTION_LOADING = 2 23 | } 24 | 25 | private val mLastData: MutableList> = ArrayList() 26 | private val mData: MutableList> = ArrayList() 27 | 28 | private val mSectionIndex: SparseArray = SparseArray() 29 | private val mItemIndex: SparseArray = SparseArray() 30 | 31 | var actionListener: ActionListener? = null 32 | 33 | fun setData(list: MutableList>) { 34 | mData.clear() 35 | mData.addAll(list) 36 | diff(true) 37 | } 38 | 39 | private fun diff(reValue: Boolean) { 40 | val diffResult = DiffUtil.calculateDiff(DiffCallback(mLastData, mData), false) 41 | DiffCallback.generateIndex(mData, mSectionIndex, mItemIndex) 42 | diffResult.dispatchUpdatesTo(this) 43 | 44 | if (reValue) { 45 | mLastData.clear() 46 | mData.forEach { mLastData.add(it.clone()) } 47 | } else { 48 | // clone status 避免大量创建对象 49 | mData.forEachIndexed { index, it -> 50 | it.cloneStatusTo(mLastData[index]) 51 | } 52 | } 53 | } 54 | 55 | override fun onBindViewHolder(holder: FoldViewHolder, position: Int) { 56 | val sectionIndex = mSectionIndex[position] 57 | val itemIndex = mItemIndex[position] 58 | val section = mData[sectionIndex] 59 | when (itemIndex) { 60 | DiffCallback.ITEM_INDEX_SECTION_HEADER -> (holder.itemView as SectionHeaderView).render(section) 61 | DiffCallback.ITEM_INDEX_LOAD_BEFORE -> (holder.itemView as SectionLoadingView).render(true, section.isLoadBeforeError) 62 | DiffCallback.ITEM_INDEX_LOAD_AFTER -> (holder.itemView as SectionLoadingView).render(false, section.isLoadAfterError) 63 | else -> { 64 | val view = holder.itemView as SectionItemView 65 | val item = section.list[itemIndex] 66 | view.render(item) 67 | } 68 | } 69 | } 70 | 71 | private fun onItemClick(holder: FoldViewHolder, pos: Int) { 72 | val itemIndex = mItemIndex[pos] 73 | if (itemIndex == DiffCallback.ITEM_INDEX_SECTION_HEADER) { 74 | toggleFold(pos) 75 | } 76 | } 77 | 78 | fun scrollToHeader(header: Header) { 79 | for (i in 0 until mSectionIndex.size()) { 80 | val posInSection = mItemIndex[i] 81 | if (posInSection == DiffCallback.ITEM_INDEX_SECTION_HEADER) { 82 | val data = mData[mSectionIndex[i]] 83 | if (data.header == header) { 84 | actionListener?.scrollToPosition(i, false, true) 85 | return 86 | } 87 | } 88 | } 89 | } 90 | 91 | fun successLoadMore(loadSection: Section, data: List, loadBefore: Boolean, hasMore: Boolean){ 92 | if(loadBefore){ 93 | for(i in 0 until mSectionIndex.size()){ 94 | if(mItemIndex[i] == 0){ 95 | if(mData[mSectionIndex[i]] == loadSection){ 96 | val focusVH = actionListener?.findViewHolderForAdapterPosition(i) 97 | if (focusVH != null) { 98 | actionListener?.requestChildFocus(focusVH.itemView) 99 | break 100 | } 101 | } 102 | } 103 | } 104 | loadSection.list.addAll(0, data) 105 | loadSection.hasBefore = hasMore 106 | }else{ 107 | loadSection.list.addAll(data) 108 | loadSection.hasAfter = hasMore 109 | } 110 | lock(loadSection) 111 | 112 | diff(true) 113 | } 114 | 115 | fun scrollToItem(item: Item) { 116 | for (i in mData.indices) { 117 | 118 | val find = mData[i].list.find { it == item } 119 | if (find != null) { 120 | if (mData[i].isFold) { 121 | mData[i].isFold = false 122 | lock(mData[i]) 123 | diff(false) 124 | realScrollToItem(item) 125 | } else { 126 | realScrollToItem(item) 127 | } 128 | } 129 | } 130 | 131 | } 132 | 133 | private fun realScrollToItem(item: Item) { 134 | for (i in 0 until mSectionIndex.size()) { 135 | val posInSection = mItemIndex[i] 136 | if (posInSection >= 0) { 137 | val section = mData[mSectionIndex[i]] 138 | if (section.list[posInSection] == item) { 139 | actionListener?.scrollToPosition(i, true, true) 140 | return 141 | } 142 | } 143 | } 144 | } 145 | 146 | private fun toggleFold(pos: Int) { 147 | val section = mData[mSectionIndex[pos]] 148 | section.isFold = !section.isFold 149 | lock(section) 150 | diff(false) 151 | 152 | if (!section.isFold) { 153 | for (i in 0 until mSectionIndex.size()) { 154 | val index = mSectionIndex[i] 155 | val inner = mItemIndex[i] 156 | if (inner == DiffCallback.ITEM_INDEX_SECTION_HEADER) { 157 | if (section.header == mData[index].header) { 158 | actionListener?.scrollToPosition(i, false, true) 159 | break 160 | } 161 | } 162 | } 163 | } 164 | } 165 | 166 | // 遍历整个列表,以 index 为中心,锁住/解锁前后所有的数据 167 | private fun lock(section: Section) { 168 | val lockPrevious = !section.isFold && section.hasBefore && !section.isLoadBeforeError 169 | val lockAfter = !section.isFold && section.hasAfter && !section.isLoadAfterError 170 | val index = mData.indexOf(section) 171 | section.isLocked = false 172 | for (i in mData.indices) { 173 | if (i < index) { 174 | val data = mData[i] 175 | data.isLocked = lockPrevious 176 | } else if (i > index) { 177 | val data = mData[i] 178 | data.isLocked = lockAfter 179 | } 180 | } 181 | } 182 | 183 | fun getRelativeFixedItemPosition(pos: Int): Int { 184 | var position = pos 185 | while (getItemViewType(position) != ITEM_TYPE_SECTION_HEADER) { 186 | position-- 187 | if (position < 0) { 188 | return RecyclerView.NO_POSITION 189 | } 190 | } 191 | return position 192 | } 193 | 194 | override fun getItemCount(): Int = mItemIndex.size() 195 | 196 | override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): FoldViewHolder { 197 | val view = when (viewType) { 198 | ITEM_TYPE_SECTION_HEADER -> SectionHeaderView(context) 199 | ITEM_TYPE_SECTION_LOADING -> SectionLoadingView(context) 200 | else -> SectionItemView(context) 201 | } 202 | val viewHolder = FoldViewHolder(view) 203 | view.setOnClickListener { 204 | val position = viewHolder.adapterPosition 205 | if (position != RecyclerView.NO_POSITION) { 206 | onItemClick(viewHolder, position) 207 | } 208 | } 209 | return viewHolder 210 | } 211 | 212 | override fun getItemViewType(position: Int): Int { 213 | val itemIndex = mItemIndex[position] 214 | return when (itemIndex) { 215 | DiffCallback.ITEM_INDEX_SECTION_HEADER -> ITEM_TYPE_SECTION_HEADER 216 | DiffCallback.ITEM_INDEX_LOAD_AFTER -> ITEM_TYPE_SECTION_LOADING 217 | DiffCallback.ITEM_INDEX_LOAD_BEFORE -> ITEM_TYPE_SECTION_LOADING 218 | else -> ITEM_TYPE_SECTION_ITEM 219 | } 220 | } 221 | 222 | override fun onViewAttachedToWindow(holder: FoldViewHolder) { 223 | if (holder.itemView is SectionLoadingView) { 224 | val layout = holder.itemView 225 | if (!layout.isLoadError()) { 226 | val section = mData[mSectionIndex.get(holder.adapterPosition)] 227 | actionListener?.loadMore(section, layout.isLoadBefore()) 228 | } 229 | } 230 | } 231 | 232 | fun afterBindFixedViewHolder(viewHolder: FoldViewHolder, pos: Int) { 233 | viewHolder.itemView.setOnClickListener { 234 | onItemClick(viewHolder, pos) 235 | } 236 | } 237 | 238 | interface ActionListener { 239 | fun loadMore(section: Section, loadBefore: Boolean) 240 | fun scrollToPosition(position: Int, underSection: Boolean, forceInScreen: Boolean) 241 | fun findViewHolderForAdapterPosition(position: Int): RecyclerView.ViewHolder? 242 | fun requestChildFocus(view: View) 243 | } 244 | } -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/FoldViewHolder.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo 2 | 3 | import android.support.v7.widget.RecyclerView 4 | import android.view.View 5 | 6 | /** 7 | * Created by cgspine on 2018/1/26. 8 | */ 9 | 10 | class FoldViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.support.v7.widget.LinearLayoutManager 6 | import android.support.v7.widget.RecyclerView 7 | import android.view.Gravity 8 | import android.view.View 9 | import android.view.ViewGroup 10 | import android.widget.FrameLayout 11 | import butterknife.BindView 12 | import butterknife.ButterKnife 13 | import com.qmuiteam.qmui.util.QMUIDisplayHelper 14 | import com.qmuiteam.qmui.util.QMUIStatusBarHelper 15 | import com.qmuiteam.qmui.widget.QMUITopBarLayout 16 | 17 | /** 18 | * Created by cgspine on 2018/1/26. 19 | */ 20 | 21 | class MainActivity : AppCompatActivity() { 22 | 23 | @BindView(R.id.topbar) lateinit var mTopBar: QMUITopBarLayout 24 | @BindView(R.id.recycler_view) lateinit var mRecyclerView: RecyclerView 25 | @BindView(R.id.section_header_container) lateinit var mSectionHeaderView: FrameLayout 26 | 27 | private lateinit var mLayoutManager: LinearLayoutManager 28 | private lateinit var mAdapter: FoldAdapter 29 | 30 | override fun onCreate(savedInstanceState: Bundle?) { 31 | super.onCreate(savedInstanceState) 32 | setContentView(R.layout.main_layout) 33 | QMUIStatusBarHelper.translucent(this) 34 | ButterKnife.bind(this) 35 | initTopBar() 36 | initRecyclerView() 37 | 38 | mAdapter.setData(loadData()) 39 | mAdapter.scrollToItem(Item("section 4, item 5")) 40 | } 41 | 42 | private fun initTopBar() { 43 | mTopBar.setTitle(R.string.app_name) 44 | mTopBar.setTitleGravity(Gravity.CENTER) 45 | } 46 | 47 | 48 | private fun initRecyclerView() { 49 | mAdapter = FoldAdapter(this) 50 | mAdapter.actionListener = object : FoldAdapter.ActionListener { 51 | override fun loadMore(section: Section, loadBefore: Boolean) { 52 | mRecyclerView.postDelayed({ 53 | val list = ArrayList() 54 | val count = 1 + (Math.random() * 20).toInt() 55 | val hasMore = count <= 10 56 | (0..count).mapTo(list) { Item("${section.header.title} load more $it in ${System.currentTimeMillis()}") } 57 | successLoadMore(section, list, loadBefore, hasMore) 58 | },2000) 59 | } 60 | 61 | override fun scrollToPosition(position: Int, underSection: Boolean, forceInScreen: Boolean) { 62 | scrollToPos(position, underSection, forceInScreen) 63 | } 64 | 65 | override fun findViewHolderForAdapterPosition(position: Int): RecyclerView.ViewHolder? { 66 | return mRecyclerView.findViewHolderForAdapterPosition(position) 67 | } 68 | 69 | override fun requestChildFocus(view: View) { 70 | mRecyclerView.requestChildFocus(view, null) 71 | } 72 | } 73 | mRecyclerView.adapter = mAdapter 74 | mLayoutManager = object : LinearLayoutManager(this) { 75 | override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams { 76 | return RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 77 | ViewGroup.LayoutParams.WRAP_CONTENT) 78 | } 79 | 80 | override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) { 81 | try { 82 | super.onLayoutChildren(recycler, state) 83 | } catch (e: IndexOutOfBoundsException) { 84 | e.printStackTrace() 85 | } 86 | } 87 | } 88 | mRecyclerView.layoutManager = mLayoutManager 89 | val callback = object : PinnedSectionItemDecoration.Callback { 90 | override fun getRelativeFixedItemPosition(pos: Int): Int { 91 | return mAdapter.getRelativeFixedItemPosition(pos) 92 | } 93 | 94 | override fun isHeader(pos: Int): Boolean { 95 | return mAdapter.getItemViewType(pos) == FoldAdapter.ITEM_TYPE_SECTION_HEADER 96 | } 97 | 98 | override fun afterCreateFixedViewHolder(viewHolder: FoldViewHolder, viewType: Int) { 99 | 100 | } 101 | 102 | override fun afterBindFixedViewHolder(viewHolder: FoldViewHolder, pos: Int) { 103 | mAdapter.afterBindFixedViewHolder(viewHolder, pos) 104 | } 105 | 106 | override fun createViewHolder(parent: ViewGroup, viewType: Int): FoldViewHolder { 107 | return mAdapter.createViewHolder(parent, viewType) 108 | } 109 | 110 | override fun bindViewHolder(holder: FoldViewHolder, position: Int) { 111 | mAdapter.bindViewHolder(holder, position) 112 | } 113 | 114 | override fun getItemViewType(position: Int): Int { 115 | return mAdapter.getItemViewType(position) 116 | } 117 | 118 | override fun registerAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) { 119 | mAdapter.registerAdapterDataObserver(observer) 120 | } 121 | 122 | override fun onHeaderVisibilityChanged(visible: Boolean) { 123 | 124 | } 125 | 126 | } 127 | mRecyclerView.addItemDecoration(PinnedSectionItemDecoration(mSectionHeaderView, callback)) 128 | } 129 | 130 | private fun successLoadMore(loadSection: Section, data: List, loadBefore: Boolean, hasMore: Boolean) { 131 | if (mRecyclerView.isComputingLayout) { 132 | mRecyclerView.postDelayed({ 133 | successLoadMore(loadSection, data, loadBefore, hasMore) 134 | }, 250) 135 | } else { 136 | mAdapter.successLoadMore(loadSection, data, loadBefore, hasMore) 137 | } 138 | } 139 | 140 | 141 | private fun scrollToPos(pos: Int, underSection: Boolean = true, force: Boolean = false) { 142 | if (pos < 0 || pos >= mAdapter.itemCount) { 143 | return 144 | } 145 | val firstVPos = mLayoutManager.findFirstCompletelyVisibleItemPosition() 146 | val lastVPos = mLayoutManager.findLastCompletelyVisibleItemPosition() 147 | if (pos < firstVPos || pos > lastVPos || force) { 148 | if (underSection) { 149 | mLayoutManager.scrollToPositionWithOffset(pos, QMUIDisplayHelper.dp2px(this, 60)) // 滚到不被 SectionHeader 盖住的位置 150 | } else { 151 | mLayoutManager.scrollToPositionWithOffset(pos, 0) 152 | } 153 | 154 | } 155 | } 156 | 157 | private fun loadData(): MutableList> { 158 | val data = ArrayList>() 159 | for (i in 1..10) { 160 | val header = Header("section $i") 161 | val items = ArrayList() 162 | (1..10).mapTo(items) { Item("section $i, item $it") } 163 | val section = Section(header, items, true, true, true, false) 164 | data.add(section) 165 | } 166 | return data 167 | } 168 | } -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/PinnedSectionItemDecoration.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo 2 | 3 | import android.graphics.Canvas 4 | import android.support.v7.widget.LinearLayoutManager 5 | import android.support.v7.widget.RecyclerView 6 | import android.view.View 7 | import android.view.ViewGroup 8 | import java.lang.ref.WeakReference 9 | 10 | /** 11 | * Created by cgspine on 2018/1/28. 12 | */ 13 | 14 | class PinnedSectionItemDecoration(sectionContainer: ViewGroup, private val mCallback: Callback) : RecyclerView.ItemDecoration() { 15 | private var mFixedHeaderViewHolder: VH? = null 16 | private var mFixedHeaderViewPosition = RecyclerView.NO_POSITION 17 | private val mWeakSectionContainer: WeakReference = WeakReference(sectionContainer) 18 | 19 | init { 20 | 21 | mCallback.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { 22 | // adapter 里的 ViewHolder 更新的话,fixedHeaderViewHolder 也要更新 23 | override fun onItemRangeChanged(positionStart: Int, itemCount: Int) { 24 | super.onItemRangeChanged(positionStart, itemCount) 25 | if (mFixedHeaderViewPosition >= positionStart && mFixedHeaderViewPosition < positionStart + itemCount) { 26 | if (mFixedHeaderViewHolder != null) { 27 | if (mWeakSectionContainer.get() != null) { 28 | bindFixedViewHolder(mWeakSectionContainer.get(), mFixedHeaderViewHolder!!, mFixedHeaderViewPosition) 29 | } 30 | } 31 | } 32 | } 33 | 34 | override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { 35 | super.onItemRangeRemoved(positionStart, itemCount) 36 | if (mFixedHeaderViewPosition >= positionStart && mFixedHeaderViewPosition < positionStart + itemCount) { 37 | mFixedHeaderViewPosition = RecyclerView.NO_POSITION 38 | } 39 | } 40 | }) 41 | } 42 | 43 | private fun setHeaderVisibility(visibility: Boolean) { 44 | val sectionContainer = mWeakSectionContainer.get() ?: return 45 | sectionContainer.visibility = if (visibility) View.VISIBLE else View.GONE 46 | mCallback.onHeaderVisibilityChanged(visibility) 47 | } 48 | 49 | override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State?) { 50 | super.onDrawOver(c, parent, state) 51 | val sectionContainer = mWeakSectionContainer.get() ?: return 52 | 53 | val layoutManager = parent.layoutManager 54 | if (layoutManager == null || layoutManager !is LinearLayoutManager) { 55 | setHeaderVisibility(false) 56 | return 57 | } 58 | val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition() 59 | 60 | if (firstVisibleItemPosition == RecyclerView.NO_POSITION) { 61 | setHeaderVisibility(false) 62 | return 63 | } 64 | 65 | val headerPos = mCallback.getRelativeFixedItemPosition(firstVisibleItemPosition) 66 | if (headerPos == RecyclerView.NO_POSITION) { 67 | setHeaderVisibility(false) 68 | return 69 | } 70 | if (mFixedHeaderViewHolder == null || mFixedHeaderViewHolder!!.itemViewType != mCallback.getItemViewType(headerPos)) { 71 | mFixedHeaderViewHolder = createFixedViewHolder(parent, headerPos) 72 | } 73 | 74 | if (mFixedHeaderViewPosition != headerPos) { 75 | mFixedHeaderViewPosition = headerPos 76 | bindFixedViewHolder(sectionContainer, mFixedHeaderViewHolder!!, headerPos) 77 | } 78 | 79 | setHeaderVisibility(true) 80 | 81 | val contactPoint = sectionContainer.height 82 | val childInContact = parent.findChildViewUnder((parent.width / 2).toFloat(), contactPoint.toFloat()) 83 | if (childInContact == null) { 84 | sectionContainer.offsetTopAndBottom(parent.top - sectionContainer.top) 85 | return 86 | } 87 | 88 | if (mCallback.isHeader(parent.getChildAdapterPosition(childInContact))) { 89 | sectionContainer.offsetTopAndBottom(childInContact.top + parent.top - sectionContainer.height - sectionContainer.top) 90 | return 91 | } 92 | 93 | sectionContainer.offsetTopAndBottom(parent.top - sectionContainer.top) 94 | } 95 | 96 | 97 | private fun createFixedViewHolder(recyclerView: RecyclerView, position: Int): VH { 98 | val viewType = mCallback.getItemViewType(position) 99 | val vh = mCallback.createViewHolder(recyclerView, viewType) 100 | mCallback.afterCreateFixedViewHolder(vh, viewType) 101 | return vh 102 | } 103 | 104 | private fun bindFixedViewHolder(sectionContainer: ViewGroup?, viewHolder: VH, position: Int) { 105 | mCallback.bindViewHolder(viewHolder, position) 106 | mCallback.afterBindFixedViewHolder(viewHolder, position) 107 | sectionContainer!!.removeAllViews() 108 | sectionContainer.addView(viewHolder!!.itemView) 109 | } 110 | 111 | 112 | interface Callback { 113 | /** 114 | * @param pos 任意 pos 115 | * @return 获取 pos 对应的 sectionHeader 的 pos 116 | */ 117 | fun getRelativeFixedItemPosition(pos: Int): Int 118 | 119 | 120 | fun isHeader(pos: Int): Boolean 121 | 122 | /** 123 | * 在 RecyclerView.Adapter#onCreateViewHolder 之后的方法 124 | */ 125 | fun afterCreateFixedViewHolder(viewHolder: ViewHolder, viewType: Int) 126 | 127 | /** 128 | * 在 RecyclerView.Adapter#bindViewHolder 之后的渲染方法 129 | */ 130 | fun afterBindFixedViewHolder(viewHolder: ViewHolder, pos: Int) 131 | 132 | fun createViewHolder(parent: ViewGroup, viewType: Int): ViewHolder 133 | 134 | fun bindViewHolder(holder: ViewHolder, position: Int) 135 | 136 | fun getItemViewType(position: Int): Int 137 | 138 | fun registerAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) 139 | 140 | fun onHeaderVisibilityChanged(visible: Boolean) 141 | } 142 | } -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/Section.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo 2 | 3 | /** 4 | * Created by cgspine on 2018/1/26. 5 | */ 6 | 7 | data class Section, T: Cloneable>( 8 | val header: H, 9 | val list: MutableList, 10 | var hasBefore: Boolean, 11 | var hasAfter: Boolean, 12 | var isFold: Boolean, 13 | var isLocked: Boolean): Cloneable>{ 14 | 15 | var isLoadBeforeError: Boolean = false 16 | var isLoadAfterError: Boolean = false 17 | 18 | fun count() = list.size 19 | 20 | fun cloneStatusTo(other: Section){ 21 | other.hasBefore = hasBefore 22 | other.hasAfter = hasAfter 23 | other.isFold = isFold 24 | other.isLocked = isLocked 25 | other.isLoadAfterError = isLoadAfterError 26 | other.isLoadBeforeError = isLoadBeforeError 27 | } 28 | 29 | override fun clone(): Section { 30 | val newList = ArrayList() 31 | list.forEach{ it: T -> 32 | newList.add(it.clone()) 33 | } 34 | val section = Section(header.clone(), newList, hasBefore, hasAfter, isFold, isLocked) 35 | section.isLoadBeforeError = isLoadBeforeError 36 | section.isLoadAfterError = isLoadAfterError 37 | return section 38 | } 39 | } -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/data.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo 2 | 3 | /** 4 | * Created by cgspine on 2018/1/26. 5 | */ 6 | 7 | data class Header(val title: String) : Cloneable
{ 8 | override fun clone(): Header { 9 | return Header(title) 10 | } 11 | 12 | override fun equals(other: Any?): Boolean { 13 | if (other == null || other !is Header) { 14 | return false 15 | } 16 | 17 | return other.title == title 18 | } 19 | } 20 | 21 | data class Item(val content: String) : Cloneable { 22 | override fun clone(): Item { 23 | return Item(content) 24 | } 25 | 26 | override fun equals(other: Any?): Boolean { 27 | if (other == null || other !is Item) { 28 | return false 29 | } 30 | 31 | return other.content == content 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/view/SectionHeaderView.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo.view 2 | 3 | import android.content.Context 4 | import android.support.v4.content.ContextCompat 5 | import android.support.v7.widget.AppCompatImageView 6 | import android.view.Gravity 7 | import android.view.ViewGroup 8 | import android.widget.LinearLayout 9 | import android.widget.TextView 10 | import com.qmuiteam.qmui.alpha.QMUIAlphaLinearLayout 11 | import com.qmuiteam.qmui.util.QMUIDisplayHelper 12 | import com.qmuiteam.qmui.util.QMUIResHelper 13 | import com.qmuiteam.qmui.widget.QMUIRadiusImageView 14 | import org.cgsdream.demo.Section 15 | import org.cgsdream.demo.Header 16 | import org.cgsdream.demo.Item 17 | import org.cgsdream.demo.R 18 | 19 | /** 20 | * Created by cgspine on 2018/1/28. 21 | */ 22 | class SectionHeaderView(context: Context) : LinearLayout(context) { 23 | 24 | private val iconView: QMUIRadiusImageView 25 | private val titleView: TextView 26 | private val foldView: AppCompatImageView 27 | 28 | init { 29 | orientation = HORIZONTAL 30 | gravity = Gravity.CENTER_VERTICAL 31 | val paddingHor = QMUIDisplayHelper.dp2px(context, 16) 32 | val paddingVer = QMUIDisplayHelper.dp2px(context, 11) 33 | setPadding(paddingHor, paddingVer, paddingHor, paddingVer) 34 | setBackgroundColor(ContextCompat.getColor(context, R.color.qmui_config_color_white)) 35 | 36 | iconView = QMUIRadiusImageView(context) 37 | iconView.borderWidth = 1 38 | iconView.isCircle = true 39 | iconView.borderColor = QMUIResHelper.getAttrColor(context, R.attr.qmui_config_color_gray_3) 40 | iconView.setImageResource(R.mipmap.ic_launcher) 41 | val iconSize = QMUIDisplayHelper.dp2px(context, 32) 42 | val iconLp = LinearLayout.LayoutParams(iconSize, iconSize) 43 | iconLp.rightMargin = QMUIDisplayHelper.dp2px(context, 12) 44 | addView(iconView, iconLp) 45 | 46 | titleView = TextView(context) 47 | titleView.textSize = 16f 48 | titleView.setTextColor(QMUIResHelper.getAttrColor(context, R.attr.qmui_config_color_gray_1)) 49 | addView(titleView, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f)) 50 | 51 | foldView = AppCompatImageView(context) 52 | foldView.setImageResource(R.drawable.icon_cell_arrow) 53 | val foldLp = LinearLayout.LayoutParams( 54 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) 55 | foldLp.leftMargin = QMUIDisplayHelper.dp2px(context, 12) 56 | addView(foldView, foldLp) 57 | } 58 | 59 | fun render(section: Section) { 60 | titleView.text = section.header.title 61 | foldView.rotation = if (section.isFold) 90f else 270f 62 | } 63 | } -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/view/SectionItemView.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo.view 2 | 3 | import android.content.Context 4 | import android.widget.TextView 5 | import com.qmuiteam.qmui.util.QMUIDisplayHelper 6 | import com.qmuiteam.qmui.util.QMUIResHelper 7 | import org.cgsdream.demo.Item 8 | import org.cgsdream.demo.R 9 | 10 | /** 11 | * Created by cgspine on 2018/1/28. 12 | */ 13 | 14 | internal class SectionItemView(context: Context): TextView(context){ 15 | 16 | init { 17 | textSize = 15f 18 | setTextColor(QMUIResHelper.getAttrColor(context, R.attr.qmui_config_color_gray_3)) 19 | val paddingHor = QMUIDisplayHelper.dp2px(context, 16) 20 | val paddingVer = QMUIDisplayHelper.dp2px(context, 11) 21 | setPadding(paddingHor, paddingVer, paddingHor, paddingVer) 22 | } 23 | 24 | fun render(item: Item){ 25 | text = item.content 26 | } 27 | } -------------------------------------------------------------------------------- /app/src/main/java/org/cgsdream/demo/view/SectionLoadingView.kt: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo.view 2 | 3 | import android.content.Context 4 | import android.text.SpannableStringBuilder 5 | import android.text.Spanned 6 | import android.text.style.ForegroundColorSpan 7 | import android.view.Gravity 8 | import android.view.ViewGroup 9 | import android.widget.FrameLayout 10 | import android.widget.TextView 11 | import com.qmuiteam.qmui.util.QMUIDisplayHelper 12 | import com.qmuiteam.qmui.util.QMUIResHelper 13 | import com.qmuiteam.qmui.widget.QMUILoadingView 14 | import org.cgsdream.demo.R 15 | 16 | /** 17 | * Created by cgspine on 2018/1/28. 18 | */ 19 | 20 | class SectionLoadingView(context: Context) : FrameLayout(context) { 21 | private var mQMUILoadingView: QMUILoadingView 22 | private var mErrorTextView: TextView? = null 23 | 24 | private var mIsLoadBefore: Boolean = false 25 | private var mIsLoadError = false 26 | 27 | var reLoadAction: ((Boolean) -> Unit)? = null 28 | 29 | init { 30 | val paddingVer = QMUIDisplayHelper.dp2px(getContext(), 24) 31 | val paddingHor = QMUIDisplayHelper.dp2px(getContext(), 16) 32 | setPadding(paddingHor, paddingVer, paddingHor, paddingVer) 33 | 34 | mQMUILoadingView = QMUILoadingView(context) 35 | val lp = LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) 36 | lp.gravity = Gravity.CENTER 37 | addView(mQMUILoadingView, lp) 38 | } 39 | 40 | private fun showError(isError: Boolean) { 41 | if (isError) { 42 | if (mErrorTextView == null) { 43 | initTextView() 44 | } 45 | mErrorTextView?.visibility = VISIBLE 46 | mQMUILoadingView.visibility = GONE 47 | } else { 48 | mErrorTextView?.visibility = GONE 49 | mQMUILoadingView.visibility = VISIBLE 50 | } 51 | } 52 | 53 | private fun initTextView() { 54 | val errTv = TextView(context) 55 | errTv.setTextColor(QMUIResHelper.getAttrColor(context, R.attr.qmui_config_color_gray_4)) 56 | errTv.textSize = 14f 57 | errTv.gravity = Gravity.CENTER 58 | val builder = SpannableStringBuilder() 59 | builder.append("加载失败 ") 60 | val start = builder.length 61 | builder.append("重试") 62 | val linkNormalColor = QMUIResHelper.getAttrColor(context, R.attr.qmui_config_color_blue) 63 | builder.setSpan(ForegroundColorSpan(linkNormalColor), start, builder.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE) 64 | errTv.text = builder 65 | errTv.visibility = GONE 66 | errTv.setOnClickListener { 67 | showError(false) 68 | reLoadAction?.invoke(isLoadBefore()) 69 | } 70 | val lp = LayoutParams(LayoutParams.MATCH_PARENT, QMUIDisplayHelper.dpToPx(32)) 71 | lp.gravity = Gravity.CENTER 72 | addView(mErrorTextView, lp) 73 | mErrorTextView = errTv 74 | } 75 | 76 | fun render(isLoadBefore: Boolean, isLoadError: Boolean) { 77 | mIsLoadBefore = isLoadBefore 78 | mIsLoadError = isLoadError 79 | showError(isLoadError) 80 | } 81 | 82 | fun isLoadBefore(): Boolean { 83 | return mIsLoadBefore 84 | } 85 | 86 | fun isLoadError(): Boolean { 87 | return mIsLoadError 88 | } 89 | } -------------------------------------------------------------------------------- /app/src/main/res/color/s_topbar_btn_color.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /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 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/icon_cell_arrow.xml: -------------------------------------------------------------------------------- 1 | 6 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/main_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 19 | 20 | 24 | 25 | 26 | 31 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RecyclerViewFoldDemo 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 8 | 9 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/test/java/org/cgsdream/demo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package org.cgsdream.demo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.0.1' 11 | 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.10" 13 | classpath "org.jetbrains.kotlin:kotlin-android-extensions:1.2.10" 14 | 15 | 16 | // NOTE: Do not place your application dependencies here; they belong 17 | // in the individual module build.gradle files 18 | } 19 | } 20 | 21 | allprojects { 22 | repositories { 23 | google() 24 | jcenter() 25 | } 26 | 27 | ext { 28 | butterknifeVersion = '8.5.1' 29 | buildToolsVersion = "27.0.1" 30 | minSdkVersion = 21 31 | targetSdkVersion = 27 32 | compileSdkVersion = 27 33 | supportVersion = "27.0.2" 34 | qmuiVersion = "1.0.7" 35 | kotlin_version = "1.2.10" 36 | } 37 | } 38 | 39 | task clean(type: Delete) { 40 | delete rootProject.buildDir 41 | } 42 | -------------------------------------------------------------------------------- /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 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cgspine/RecyclerViewFoldDemo/e858e90ca95286aea1b6a34e67442111c05267b0/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jan 26 11:43:42 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-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 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------