tabTitleList) {
15 | this.tabTitleList = tabTitleList;
16 | }
17 |
18 | @NonNull
19 | @Override
20 | public String toString() {
21 | return tabTitleList.toString();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main_java.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/gaohui/nestedrecyclerview/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview
2 |
3 | import android.support.test.InstrumentationRegistry
4 | import android.support.test.runner.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getTargetContext()
22 | assertEquals("com.gaohui.nestedrecyclerview", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/update/viewpager/NestedViewPager.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.update.viewpager;
2 |
3 | import android.content.Context;
4 | import android.support.v4.view.ViewPager;
5 | import android.view.MotionEvent;
6 |
7 | public class NestedViewPager extends ViewPager {
8 | private boolean canScroll = true;
9 |
10 | public NestedViewPager(Context context) {
11 | super(context);
12 | }
13 |
14 |
15 | public void setCanScroll(boolean canScroll) {
16 | this.canScroll = canScroll;
17 | }
18 |
19 | @Override
20 | public boolean onTouchEvent(MotionEvent ev) {
21 | return canScroll && super.onTouchEvent(ev);
22 | }
23 |
24 | @Override
25 | public boolean onInterceptTouchEvent(MotionEvent ev) {
26 | return canScroll && super.onInterceptTouchEvent(ev);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # Kotlin code style for this project: "official" or "obsolete":
15 | kotlin.code.style=official
16 |
17 | #android.useAndroidX=true
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_item_category_default.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
13 |
14 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/update/viewholder/UpdatedTextViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.update.viewholder;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.support.v7.widget.StaggeredGridLayoutManager;
6 | import android.view.View;
7 | import android.widget.TextView;
8 |
9 | import com.gaohui.nestedrecyclerview.R;
10 |
11 |
12 | public class UpdatedTextViewHolder extends RecyclerView.ViewHolder {
13 | public TextView mTv;
14 | public UpdatedTextViewHolder(@NonNull View itemView) {
15 | super(itemView);
16 | mTv = (TextView) itemView.findViewById(R.id.textView);
17 | StaggeredGridLayoutManager.LayoutParams slp;
18 | if ((itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams)) {
19 | slp = (StaggeredGridLayoutManager.LayoutParams)itemView.getLayoutParams();
20 | slp.setFullSpan(true);
21 | }
22 |
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/kotlin/FixedSwipeRefreshLayout.kt:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.kotlin
2 |
3 | import android.content.Context
4 | import android.support.v4.widget.SwipeRefreshLayout
5 | import android.util.AttributeSet
6 |
7 | class StoreSwipeRefreshLayout : SwipeRefreshLayout {
8 |
9 | private var mParentRecyclerView: ParentRecyclerView? = null
10 |
11 | constructor(context: Context) : super(context)
12 | constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
13 |
14 | override fun onAttachedToWindow() {
15 | super.onAttachedToWindow()
16 | if (mParentRecyclerView == null) {
17 | for (i in 0 until childCount) {
18 | val child = getChildAt(i)
19 | if (child is ParentRecyclerView) {
20 | mParentRecyclerView = child
21 | break
22 | }
23 | }
24 | }
25 |
26 | }
27 |
28 | override fun canChildScrollUp(): Boolean {
29 | return super.canChildScrollUp() || mParentRecyclerView?.isChildRecyclerViewCanScrollUp()?:false
30 | }
31 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/laz_recommend_tab_filter_revamp_new_rec.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
18 |
19 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/UIUtils.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview;
2 |
3 | import android.content.res.Resources;
4 | import android.util.DisplayMetrics;
5 |
6 | public class UIUtils {
7 | private static final DisplayMetrics sMetrics = Resources.getSystem().getDisplayMetrics();
8 |
9 | public static int getScreenWidth() {
10 | return sMetrics != null ? sMetrics.widthPixels : 0;
11 | }
12 |
13 | public static int getScreenHeight() {
14 | return sMetrics != null ? sMetrics.heightPixels : 0;
15 | }
16 |
17 |
18 | public static int px2dp(float pxValue) {
19 | final float scale = sMetrics != null ? sMetrics.density : 1;
20 | return (int) (pxValue / scale + 0.5f);
21 | }
22 |
23 | public static int dp2px(float dipValue) {
24 | final float scale = sMetrics != null ? sMetrics.density : 1;
25 | return (int) (dipValue * scale + 0.5f);
26 | }
27 |
28 |
29 | public static int px2sp(float pxValue) {
30 | final float fontScale = sMetrics != null ? sMetrics.scaledDensity : 1;
31 | return (int) (pxValue / fontScale + 0.5f);
32 | }
33 |
34 | public static int sp2px(float spValue) {
35 | final float fontScale = sMetrics != null ? sMetrics.scaledDensity : 1;
36 | return (int) (spValue * fontScale + 0.5f);
37 | }
38 |
39 | }
40 |
41 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/update/viewpager/AbsViewPagerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.update.viewpager;
2 |
3 |
4 | import android.support.v4.view.PagerAdapter;
5 |
6 | import com.gaohui.nestedrecyclerview.update.UpdatedNestedRecyclerView;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * Copyright (C) 2004 - 2023 Lazada Inc. All Rights Reserved.
12 | * Description : 下沉首页主RecyclerView的一些方法
13 | *
14 | * Created by zukai.kzk@alibaba-inc.com on 2023/11/14
15 | */
16 | public abstract class AbsViewPagerAdapter extends PagerAdapter {
17 | /**
18 | * NestedRVOnScrollListener使用到
19 | */
20 | public static int currentPageTab = 0;
21 |
22 | /**
23 | * NestedRecyclerView内部使用到
24 | */
25 | abstract public UpdatedNestedRecyclerView getCurrentView();
26 |
27 | abstract public int getCurrentPageTab();
28 |
29 | /**
30 | * NestedRVOnScrollListener使用到
31 | */
32 | abstract public void clearAllOffset();
33 |
34 | /**
35 | * NestedRVOnScrollListener使用到
36 | */
37 | abstract public List getTabItems();
38 |
39 | /**
40 | * NestedRVOnScrollListener使用到
41 | */
42 | abstract public String getTabItem(int tabIndex);
43 |
44 | /**
45 | * NestedRVOnScrollListener使用到
46 | */
47 | abstract public void saveRecyclerViewState(UpdatedNestedRecyclerView recyclerView, String tabId);
48 | }
49 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | apply plugin: 'kotlin-android'
4 |
5 | apply plugin: 'kotlin-android-extensions'
6 |
7 | android {
8 | compileSdkVersion 28
9 | defaultConfig {
10 | applicationId "com.gaohui.nestedrecyclerview"
11 | minSdkVersion 16
12 | targetSdkVersion 28
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-optimize.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: 'libs', include: ['*.jar'])
27 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
28 | implementation 'com.android.support:appcompat-v7:28.0.0'
29 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
30 | testImplementation 'junit:junit:4.12'
31 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
32 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
33 |
34 | implementation 'com.android.support:recyclerview-v7:28.0.0'
35 | implementation 'com.android.support:design:28.0.0'
36 |
37 | // implementation "com.bytedance.tools.codelocator:codelocator-core-support:2.0.0"
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/java/view/StoreSwipeRefreshLayout.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.java.view;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.NonNull;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.widget.SwipeRefreshLayout;
7 | import android.util.AttributeSet;
8 | import android.view.View;
9 |
10 | public class StoreSwipeRefreshLayout extends SwipeRefreshLayout {
11 | private ParentRecyclerView mParentRecyclerView = null;
12 |
13 | public StoreSwipeRefreshLayout(@NonNull Context context) {
14 | super(context);
15 | }
16 |
17 | public StoreSwipeRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
18 | super(context, attrs);
19 | }
20 |
21 | @Override
22 | protected void onAttachedToWindow() {
23 | super.onAttachedToWindow();
24 | if(mParentRecyclerView == null) {
25 | for(int i =0;i < getChildCount();i++) {
26 | View child = getChildAt(i);
27 | if(child instanceof ParentRecyclerView) {
28 | mParentRecyclerView = (ParentRecyclerView) child;
29 | break;
30 | }
31 | }
32 | }
33 | }
34 |
35 | @Override
36 | public boolean canChildScrollUp() {
37 | return super.canChildScrollUp() || (mParentRecyclerView !=null
38 | && mParentRecyclerView.isChildRecyclerViewCanScrollUp());
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_item_category.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
28 |
29 |
33 |
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/java/utils/FlingHelper.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.java.utils;
2 |
3 | import android.content.Context;
4 | import android.view.ViewConfiguration;
5 |
6 | public class FlingHelper {
7 | private static float DECELERATION_RATE = ((float) (Math.log(0.78d) / Math.log(0.9d)));
8 | private static float mFlingFriction = ViewConfiguration.getScrollFriction();
9 | private static float mPhysicalCoeff;
10 |
11 | public FlingHelper(Context context) {
12 | mPhysicalCoeff = context.getResources().getDisplayMetrics().density * 160.0f * 386.0878f * 0.84f;
13 | }
14 |
15 | private double getSplineDeceleration(int i) {
16 | return Math.log((double) ((0.35f * ((float) Math.abs(i))) / (mFlingFriction * mPhysicalCoeff)));
17 | }
18 |
19 | private double getSplineDecelerationByDistance(double d) {
20 | return ((((double) DECELERATION_RATE) - 1.0d) * Math.log(d / ((double) (mFlingFriction * mPhysicalCoeff)))) / ((double) DECELERATION_RATE);
21 | }
22 |
23 | public double getSplineFlingDistance(int i) {
24 | return Math.exp(getSplineDeceleration(i) * (((double) DECELERATION_RATE) / (((double) DECELERATION_RATE) - 1.0d))) * ((double) (mFlingFriction * mPhysicalCoeff));
25 | }
26 |
27 | public int getVelocityByDistance(double d) {
28 | return Math.abs((int) (((Math.exp(getSplineDecelerationByDistance(d)) * ((double) mFlingFriction)) * ((double) mPhysicalCoeff)) / 0.3499999940395355d));
29 | }
30 |
31 | public int getSplineFlingDuration(int i) {
32 | return (int) (Math.exp(getSplineDeceleration(i) / (((double) DECELERATION_RATE) - 1.0d)) * 1000.0d);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/kotlin/helper/FlingHelper.kt:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.kotlin.helper
2 |
3 | import android.content.Context
4 | import android.view.ViewConfiguration
5 | import kotlin.math.abs
6 | import kotlin.math.exp
7 | import kotlin.math.ln
8 |
9 | /**
10 | * Fling 帮助类
11 | */
12 | class FlingHelper(context: Context) {
13 |
14 | private var mPhysicalCoeff: Float = context.resources.displayMetrics.density * 160.0f * 386.0878f * 0.84f
15 |
16 | private fun getSplineDeceleration(i: Int): Double {
17 | return ln((0.35f * abs(i).toFloat() / (mFlingFriction * mPhysicalCoeff)).toDouble())
18 | }
19 |
20 | private fun getSplineDecelerationByDistance(d: Double): Double {
21 | return (DECELERATION_RATE.toDouble() - 1.0) * ln(d / (mFlingFriction * mPhysicalCoeff).toDouble()) / DECELERATION_RATE.toDouble()
22 | }
23 |
24 | /**
25 | * 根据加速度来获取需要fling的距离
26 | * @param i 加速度
27 | * @return fling的距离
28 | */
29 | fun getSplineFlingDistance(i: Int): Double {
30 | return exp(getSplineDeceleration(i) * (DECELERATION_RATE.toDouble() / (DECELERATION_RATE.toDouble() - 1.0))) * (mFlingFriction * mPhysicalCoeff).toDouble()
31 | }
32 |
33 | /**
34 | * 根据距离来获取加速度
35 | * @param d 距离
36 | * @return 返回加速度
37 | */
38 | fun getVelocityByDistance(d: Double): Int {
39 | return abs((exp(getSplineDecelerationByDistance(d)) * mFlingFriction.toDouble() * mPhysicalCoeff.toDouble() / 0.3499999940395355).toInt())
40 | }
41 |
42 | companion object {
43 | private val DECELERATION_RATE = (ln(0.78) / ln(0.9)).toFloat()
44 | private val mFlingFriction = ViewConfiguration.getScrollFriction()
45 |
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/app/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/kotlin/adapter/CategoryPagerAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.kotlin.adapter
2 |
3 | import android.support.v4.view.PagerAdapter
4 | import android.view.View
5 | import android.view.ViewGroup
6 | import com.gaohui.nestedrecyclerview.kotlin.CategoryView
7 | import com.gaohui.nestedrecyclerview.kotlin.tab.DynamicTabBean
8 | import com.gaohui.nestedrecyclerview.kotlin.tab.DynamicTabLayout
9 |
10 | class CategoryPagerAdapter(
11 | private val viewList: ArrayList,
12 | private val tabTitleList: ArrayList
13 | ) : PagerAdapter(), DynamicTabLayout.DynamicTabProvider {
14 |
15 | private var mCurrentPrimaryItem: CategoryView? = null
16 |
17 | override fun getCount(): Int {
18 | return viewList.size
19 | }
20 |
21 | override fun isViewFromObject(view: View, obj: Any): Boolean {
22 | return view === obj
23 | }
24 |
25 | override fun instantiateItem(container: ViewGroup, position: Int): Any {
26 | val view = viewList[position]
27 | if (container == view.parent) {
28 | container.removeView(view)
29 | }
30 | container.addView(view)
31 | return view
32 | }
33 |
34 | override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
35 | //container.removeView((View) object);
36 | }
37 |
38 | override fun setPrimaryItem(container: ViewGroup, position: Int, obj: Any) {
39 | val item = obj as CategoryView
40 | if(item != mCurrentPrimaryItem) {
41 | mCurrentPrimaryItem?.onUserVisibleChange(false)
42 | }
43 | item.onUserVisibleChange(true)
44 | mCurrentPrimaryItem = item
45 | }
46 |
47 | override fun getPageTitle(position: Int): CharSequence? {
48 | return tabTitleList[position]
49 | }
50 |
51 | override fun getPageTitleItem(position: Int): DynamicTabBean? {
52 | return DynamicTabBean("推荐", "精品推荐")
53 | }
54 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/update/IRecommendTabLayout.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.update;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.ColorInt;
5 | import android.support.v4.view.ViewPager;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 |
9 |
10 | import java.util.List;
11 |
12 | /**
13 | * @author tangmingjian
14 | * @version v1.0
15 | * @date 2022/10/25
16 | * @description 20221121 jfy tab抽象
17 | */
18 | public interface IRecommendTabLayout {
19 | //-----------------------------------------------------------------
20 | // View相关方法 start
21 | //-----------------------------------------------------------------
22 | View getView();
23 |
24 | Context getContext();
25 |
26 | int getWidth();
27 |
28 | void setBackgroundColor(@ColorInt int color);
29 |
30 | ViewGroup.LayoutParams getLayoutParams();
31 |
32 | int getMeasuredHeight();
33 |
34 | View getChildAt(int index);
35 |
36 | void setLayoutParams(ViewGroup.LayoutParams params);
37 |
38 | void requestLayout();
39 | //-----------------------------------------------------------------
40 | // View相关方法 end
41 | //-----------------------------------------------------------------
42 |
43 | void setViewPager(ViewPager viewPager);
44 |
45 | void setIndicatorColor(int indicatorColor);
46 |
47 | void setFixTabFlag(boolean fixTabFlag);
48 |
49 | void updateTabStyles(List list);
50 |
51 | void setLineDrawableEnabled(boolean enabled);
52 |
53 | void setJfyAtTop(boolean jfyAtTop);
54 |
55 | View getTabAt(int position);
56 |
57 | int getTabCount();
58 |
59 | void setOnLayoutListener(OnLayoutListener onLayoutListener);
60 |
61 | interface OnLayoutListener {
62 | void onScrollChanged(int scrollX);
63 |
64 | void onReachStart();
65 |
66 | void onReachEnd();
67 |
68 | void onWidthChanged(int width);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/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/layout/matrix_store_tab_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
25 |
26 |
32 |
33 |
34 |
35 |
48 |
49 |
57 |
58 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/kotlin/ui/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.kotlin.ui
2 |
3 | import android.graphics.Color
4 | import android.support.v7.app.AppCompatActivity
5 | import android.os.Bundle
6 | import android.widget.Toast
7 | import com.gaohui.nestedrecyclerview.BaseMenuActivity
8 | import com.gaohui.nestedrecyclerview.R
9 | import com.gaohui.nestedrecyclerview.kotlin.adapter.MultiTypeAdapter
10 | import com.gaohui.nestedrecyclerview.kotlin.bean.CategoryBean
11 | import kotlinx.android.synthetic.main.activity_main.*
12 | import java.util.*
13 |
14 |
15 | class MainActivity : BaseMenuActivity() {
16 |
17 | private val mDataList = ArrayList()
18 |
19 | private val strArray = arrayOf("推荐", "视频", "直播", "图片", "精华", "热门")
20 |
21 | var lastBackPressedTime = 0L
22 |
23 | private val multiTypeAdapter =
24 | MultiTypeAdapter(mDataList)
25 |
26 | override fun onCreate(savedInstanceState: Bundle?) {
27 | super.onCreate(savedInstanceState)
28 | setContentView(R.layout.activity_main)
29 |
30 | kttRecyclerView.initLayoutManager()
31 | kttRecyclerView.adapter = multiTypeAdapter
32 |
33 | refresh()
34 |
35 | swipeRefreshLayout.setColorSchemeColors(Color.RED)
36 | swipeRefreshLayout.setOnRefreshListener {
37 | refresh()
38 | }
39 |
40 | }
41 |
42 | private fun refresh() {
43 | mDataList.clear()
44 | for (i in 0..8) {
45 | mDataList.add("parent item text $i")
46 | }
47 | val categoryBean = CategoryBean()
48 | categoryBean.tabTitleList.clear()
49 | categoryBean.tabTitleList.addAll(strArray.asList())
50 | mDataList.add(categoryBean)
51 | multiTypeAdapter.notifyDataSetChanged()
52 | swipeRefreshLayout.isRefreshing = false
53 | }
54 |
55 | override fun onBackPressed() {
56 | if (System.currentTimeMillis() - lastBackPressedTime < 2000) {
57 | super.onBackPressed()
58 | } else {
59 | kttRecyclerView.scrollToPosition(0)
60 | Toast.makeText(this,"再按一次退出程序",Toast.LENGTH_SHORT).show()
61 | lastBackPressedTime = System.currentTimeMillis()
62 | }
63 | }
64 |
65 | override fun onDestroy() {
66 | super.onDestroy()
67 | multiTypeAdapter.destroy()
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/java/adapter/CategoryPagerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.java.adapter;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.view.PagerAdapter;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 |
9 |
10 | import com.gaohui.nestedrecyclerview.java.view.CategoryView;
11 |
12 | import java.util.ArrayList;
13 |
14 | public class CategoryPagerAdapter extends PagerAdapter {
15 |
16 | private ArrayList mViewList;
17 | private ArrayList mTabList;
18 |
19 | private CategoryView mCurrentPrimaryItem = null;
20 |
21 |
22 | public CategoryPagerAdapter(ArrayList viewList,ArrayList tabList) {
23 | mViewList = viewList;
24 | mTabList = tabList;
25 | }
26 |
27 | @Override
28 | public int getCount() {
29 | return mViewList.size();
30 | }
31 |
32 | @NonNull
33 | @Override
34 | public Object instantiateItem(@NonNull ViewGroup container, int position) {
35 | CategoryView categoryView = mViewList.get(position);
36 | if(container == categoryView.getParent()) {
37 | container.removeView(categoryView);
38 | }
39 | container.addView(categoryView);
40 | return categoryView;
41 | }
42 |
43 | @Override
44 | public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
45 | // super.destroyItem(container, position, object);
46 | }
47 |
48 | @Override
49 | public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
50 | CategoryView categoryView = (CategoryView) object;
51 | if(categoryView != mCurrentPrimaryItem) {
52 | if(mCurrentPrimaryItem != null) {
53 | mCurrentPrimaryItem.onUserVisibleChange(false);
54 | }
55 | }
56 | categoryView.onUserVisibleChange(true);
57 | mCurrentPrimaryItem = categoryView;
58 | }
59 |
60 | @Override
61 | public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
62 | return view == o;
63 | }
64 |
65 | @Nullable
66 | @Override
67 | public CharSequence getPageTitle(int position) {
68 | return mTabList.get(position);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/kotlin/adapter/MultiTypeAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.kotlin.adapter
2 |
3 | import android.support.v7.widget.RecyclerView
4 | import android.view.LayoutInflater
5 | import android.view.ViewGroup
6 | import com.gaohui.nestedrecyclerview.kotlin.ChildRecyclerView
7 | import com.gaohui.nestedrecyclerview.R
8 | import com.gaohui.nestedrecyclerview.kotlin.bean.CategoryBean
9 | import com.gaohui.nestedrecyclerview.kotlin.holder.SimpleCategoryViewHolder
10 | import com.gaohui.nestedrecyclerview.kotlin.holder.SimpleTextViewHolder
11 |
12 | class MultiTypeAdapter(private val dataSet:ArrayList) : RecyclerView.Adapter() {
13 |
14 | companion object {
15 | private const val TYPE_TEXT = 0
16 | private const val TYPE_CATEGORY = 1
17 | }
18 |
19 | private var mCategoryViewHolder: SimpleCategoryViewHolder? = null
20 |
21 | override fun getItemViewType(position: Int): Int {
22 | return if(dataSet[position] is String) {
23 | TYPE_TEXT
24 | } else {
25 | TYPE_CATEGORY
26 | }
27 | }
28 |
29 | override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
30 | return if(viewType == TYPE_TEXT) {
31 | SimpleTextViewHolder(
32 | LayoutInflater.from(
33 | viewGroup.context
34 | ).inflate(R.layout.layout_item_text, viewGroup, false)
35 | )
36 | } else {
37 | val categoryViewHolder =
38 | SimpleCategoryViewHolder(
39 | LayoutInflater.from(viewGroup.context).inflate(
40 | R.layout.layout_item_category,
41 | viewGroup,
42 | false
43 | )
44 | )
45 | mCategoryViewHolder = categoryViewHolder
46 | return categoryViewHolder
47 | }
48 | }
49 |
50 | override fun getItemCount(): Int = dataSet.size
51 |
52 | override fun onBindViewHolder(holder: RecyclerView.ViewHolder, pos: Int) {
53 | if(holder is SimpleTextViewHolder) {
54 | holder.mTv.text = dataSet[pos] as String
55 | } else if(holder is SimpleCategoryViewHolder){
56 | holder.bindData(dataSet[pos] as CategoryBean)
57 | }
58 | }
59 |
60 | fun getCurrentChildRecyclerView(): ChildRecyclerView? {
61 | mCategoryViewHolder?.apply {
62 | return this.getCurrentChildRecyclerView()
63 | }
64 | return null
65 | }
66 |
67 | fun destroy() {
68 | mCategoryViewHolder?.destroy()
69 |
70 | }
71 |
72 |
73 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/kotlin/CategoryView.kt:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.kotlin
2 |
3 | import android.content.Context
4 | import android.support.v7.widget.RecyclerView
5 | import android.support.v7.widget.StaggeredGridLayoutManager
6 | import android.util.AttributeSet
7 | import com.gaohui.nestedrecyclerview.kotlin.adapter.MultiTypeAdapter
8 |
9 | class CategoryView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ChildRecyclerView(context, attrs, defStyleAttr),
10 | OnUserVisibleChange {
11 |
12 | private val mDataList = ArrayList()
13 |
14 | private var hasLoadData = false
15 |
16 | init {
17 | initRecyclerView()
18 | initLoadMore()
19 | }
20 |
21 | private fun initRecyclerView() {
22 | val staggeredGridLayoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
23 | layoutManager = staggeredGridLayoutManager
24 | adapter = MultiTypeAdapter(mDataList)
25 | }
26 |
27 | private fun initLoadMore() {
28 | addOnScrollListener(object :OnScrollListener(){
29 | override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
30 | super.onScrollStateChanged(recyclerView, newState)
31 | tryLoadMoreIfNeed()
32 | }
33 | })
34 |
35 | }
36 |
37 | private fun tryLoadMoreIfNeed() {
38 | if(adapter == null) return
39 | val layoutManager = layoutManager
40 | val last: IntArray
41 | if (layoutManager is StaggeredGridLayoutManager) {
42 | last = IntArray(layoutManager.spanCount)
43 | layoutManager.findLastVisibleItemPositions(last)
44 | for (i in last.indices) {
45 | if ((last[i] >= adapter!!.itemCount - 4)) {
46 | if (loadMore()) return
47 | break
48 | }
49 | }
50 | }
51 | }
52 |
53 | private fun initData() {
54 | hasLoadData = true
55 | for (i in 0..10) {
56 | mDataList.add("default child item $i")
57 | }
58 | adapter?.notifyDataSetChanged()
59 | }
60 |
61 | private fun loadMore():Boolean {
62 | val loadMoreSize = 5
63 | for (i in 0..loadMoreSize) {
64 | mDataList.add("load more child item $i")
65 | }
66 | adapter?.notifyItemRangeChanged(mDataList.size-loadMoreSize,mDataList.size)
67 | return true
68 | }
69 |
70 | override fun onUserVisibleChange(isVisibleToUser: Boolean) {
71 | if(hasLoadData.not() && isVisibleToUser) {
72 | initData()
73 | }
74 | }
75 |
76 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/update/viewholder/UpdatedCategoryViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.update.viewholder;
2 |
3 | import android.support.annotation.NonNull;
4 | import android.support.v7.widget.RecyclerView;
5 | import android.support.v7.widget.StaggeredGridLayoutManager;
6 | import android.util.Log;
7 | import android.view.View;
8 |
9 |
10 | public class UpdatedCategoryViewHolder extends RecyclerView.ViewHolder {
11 |
12 | // private TabLayout mTabLayout;
13 | // private ViewPager mViewPager;
14 |
15 | // private ChildRecyclerView mCurrentRecyclerView;
16 | //
17 | // HashMap cacheVies = new HashMap();
18 | //
19 | //
20 | // ArrayList viewList = new ArrayList();
21 |
22 |
23 | public UpdatedCategoryViewHolder(@NonNull View itemView) {
24 | super(itemView);
25 | Log.d("gaohui","11111 category" + itemView.getLayoutParams());
26 | StaggeredGridLayoutManager.LayoutParams slp;
27 | if ((itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams)) {
28 | slp = (StaggeredGridLayoutManager.LayoutParams)itemView.getLayoutParams();
29 | slp.setFullSpan(true);
30 | Log.d("gaohui","22222 category");
31 | }
32 | }
33 |
34 | public void bindData(Object obj) {
35 | // if(obj instanceof CategoryBean) {
36 | // CategoryBean categoryBean = (CategoryBean)obj;
37 | // viewList.clear();
38 | // if(cacheVies.size() > categoryBean.getTabTitleList().size()) {
39 | // cacheVies.clear();
40 | // }
41 | // for(String str :categoryBean.getTabTitleList()) {
42 | // CategoryView categoryView = cacheVies.get(str);
43 | // if(categoryView == null || categoryView.getParent() != mViewPager) {
44 | // categoryView = new CategoryView(mViewPager.getContext());
45 | // cacheVies.put(str, categoryView);
46 | // }
47 | // viewList.add(categoryView);
48 | // }
49 | // mCurrentRecyclerView = viewList.get(mViewPager.getCurrentItem());
50 | // int lastItem = mViewPager.getCurrentItem();
51 | // mViewPager.setAdapter(new CategoryPagerAdapter(viewList,categoryBean.getTabTitleList()));
52 | // mTabLayout.setupWithViewPager(mViewPager);
53 | // mViewPager.setCurrentItem(lastItem);
54 | //
55 | // }
56 | }
57 |
58 | public void destroy() {
59 | // cacheVies.clear();
60 | }
61 |
62 | // public ChildRecyclerView getCurrentChildRecyclerView() {
63 | // return mCurrentRecyclerView;
64 | // }
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/BaseMenuActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview
2 |
3 | import android.content.Context
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import android.support.v7.app.AppCompatActivity
7 | import android.support.v7.widget.AppCompatTextView
8 | import android.util.TypedValue
9 | import android.view.Menu
10 | import android.view.MenuItem
11 | import android.view.View
12 | import android.view.View.*
13 | import android.widget.LinearLayout
14 | import android.widget.TextView
15 | import android.widget.Toast
16 | import com.gaohui.nestedrecyclerview.java.MainJavaActivity
17 | import com.gaohui.nestedrecyclerview.update.MainUpdateActivity
18 | import com.gaohui.nestedrecyclerview.kotlin.ui.MainActivity
19 |
20 | open class BaseMenuActivity : AppCompatActivity() {
21 | override fun onCreate(savedInstanceState: Bundle?) {
22 | super.onCreate(savedInstanceState)
23 | // fullscreen()
24 | }
25 |
26 | override fun onResume() {
27 | super.onResume()
28 | // fullscreen()
29 | }
30 |
31 | private fun fullscreen() {
32 | window.decorView.systemUiVisibility = (
33 | window.decorView.systemUiVisibility
34 | or SYSTEM_UI_FLAG_FULLSCREEN
35 | or SYSTEM_UI_FLAG_HIDE_NAVIGATION
36 | or SYSTEM_UI_FLAG_IMMERSIVE_STICKY
37 | )
38 | }
39 |
40 | override fun onCreateOptionsMenu(menu: Menu?): Boolean {
41 | menuInflater.inflate(R.menu.menu, menu)
42 | return true
43 | }
44 |
45 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
46 | when (item.itemId) {
47 | R.id.java -> startActivity(Intent(this, MainJavaActivity::class.java))
48 | R.id.kotlin -> startActivity(Intent(this, MainActivity::class.java))
49 | R.id.update -> startActivity(Intent(this,
50 | MainUpdateActivity::class.java))
51 | }
52 | finish()
53 | return true
54 | }
55 | }
56 |
57 | fun Context.newActionTextView(text: String, onClick: () -> Unit): TextView {
58 | return AppCompatTextView(this)
59 | .apply {
60 | layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
61 | setRippleBackground()
62 | setPadding(6.dp, 6.dp, 6.dp, 6.dp)
63 | setOnClickListener {
64 | try {
65 | onClick()
66 | } catch (e: Exception) {
67 | Toast.makeText(context, "${e.message}", Toast.LENGTH_SHORT).show()
68 | }
69 | }
70 | setHorizontallyScrolling(true)
71 | setText(text)
72 | }
73 | }
74 |
75 | fun View.setRippleBackground() = with(TypedValue()) {
76 | context.theme.resolveAttribute(android.R.attr.selectableItemBackground, this, true)
77 | setBackgroundResource(resourceId)
78 | }
79 |
80 | val Number.dp get() = this.toInt() * 4
--------------------------------------------------------------------------------
/app/src/main/java/com/gaohui/nestedrecyclerview/java/MainJavaActivity.java:
--------------------------------------------------------------------------------
1 | package com.gaohui.nestedrecyclerview.java;
2 |
3 | import android.graphics.Color;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.widget.SwipeRefreshLayout;
7 | import android.widget.Toast;
8 |
9 | import com.gaohui.nestedrecyclerview.BaseMenuActivity;
10 | import com.gaohui.nestedrecyclerview.R;
11 | import com.gaohui.nestedrecyclerview.java.adapter.MultiTypeAdapter;
12 | import com.gaohui.nestedrecyclerview.java.bean.CategoryBean;
13 | import com.gaohui.nestedrecyclerview.java.view.ParentRecyclerView;
14 | import com.gaohui.nestedrecyclerview.java.view.StoreSwipeRefreshLayout;
15 |
16 | import java.util.ArrayList;
17 | import java.util.Arrays;
18 |
19 | public class MainJavaActivity extends BaseMenuActivity {
20 |
21 | ArrayList