Skip to content

Commit

Permalink
add demo CoordinatorLayout 嵌套滑动置顶(原始方案)
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Jul 12, 2022
1 parent 96ff7c2 commit 6a799d2
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<activity android:name=".activity.ItemPayloadActivity" />
<activity android:name=".activity.LoadMoreActivity" />
<activity android:name=".activity.StickyRvCoordinatorActivity" />
<activity android:name=".activity.StickyRvCoordinatorOriginalActivity" />
</application>

</manifest>
9 changes: 9 additions & 0 deletions app/src/main/java/me/jingbin/byrecyclerview/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import androidx.multidex.MultiDexApplication;

import me.weishu.reflection.Reflection;


public class App extends MultiDexApplication {

Expand All @@ -19,4 +21,11 @@ public void onCreate() {
public static Context getContext() {
return mContext;
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// https://github.com/tiann/FreeReflection,解决[CoordinatorLayout 嵌套滑动置顶(惯性滑动方案)]反射问题
Reflection.unseal(base);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import me.jingbin.byrecyclerview.activity.StickyRvCoordinatorActivity;
import me.jingbin.byrecyclerview.activity.StickyItemActivity;
import me.jingbin.byrecyclerview.activity.StickyRvActivity;
import me.jingbin.byrecyclerview.activity.StickyRvCoordinatorOriginalActivity;
import me.jingbin.byrecyclerview.databinding.ActivityMainBinding;
import me.jingbin.byrecyclerview.utils.WebUtil;
import me.jingbin.library.adapter.BaseByViewHolder;
Expand Down Expand Up @@ -53,7 +54,8 @@ public class MainActivity extends AppCompatActivity {
"CoordinatorLayout + RecyclerView 使用示例",
"FlexboxLayoutManager 显示处理",
"RecyclerView 嵌套滑动置顶",
"CoordinatorLayout 嵌套滑动置顶"
"CoordinatorLayout 嵌套滑动置顶(惯性滑动方案)",
"CoordinatorLayout 嵌套滑动置顶(原始方案)"
);
private ActivityMainBinding binding;

Expand Down Expand Up @@ -121,6 +123,9 @@ public void onSingleClick(View v, int position) {
case 13:// CoordinatorLayout 嵌套滑动置顶
startActivity(new Intent(MainActivity.this, StickyRvCoordinatorActivity.class));
break;
case 14:// CoordinatorLayout 嵌套滑动置顶
startActivity(new Intent(MainActivity.this, StickyRvCoordinatorOriginalActivity.class));
break;
default:
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

/**
* @author jingbin
* CoordinatorLayout 嵌套滑动置顶(惯性滑动方案)
* 如要惯性滑动,即滑动头部布局不置顶,现要求其一:
* 1、targetSdkVersion 在30以下。因为HookedScroller反射失败
* 2、集成三方库:https://github.com/tiann/FreeReflection,需要minSdkVersion 21
*/
public class StickyRvCoordinatorActivity extends BaseActivity<ActivityStickyRvCoordBinding> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package me.jingbin.byrecyclerview.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import me.jingbin.byrecyclerview.R;
import me.jingbin.byrecyclerview.app.BaseActivity;
import me.jingbin.byrecyclerview.databinding.ActivityStickyRvCoordBinding;
import me.jingbin.byrecyclerview.databinding.ActivityStickyRvCoordOriginalBinding;
import me.jingbin.byrecyclerview.fragment.CoordinatorOriginalFragment;
import me.jingbin.byrecyclerview.fragment.RefreshFragment;
import me.jingbin.byrecyclerview.fragment.SwipeRefreshFragment;
import me.jingbin.byrecyclerview.stickrvcool.config.FeedsPagerAdapter;
import me.jingbin.byrecyclerview.stickrvcool.config.HomeIndicatorHelper;
import me.jingbin.byrecyclerview.view.MyFragmentPagerAdapter;

/**
* @author jingbin
* CoordinatorLayout 嵌套滑动置顶(原始方案)
*/
public class StickyRvCoordinatorOriginalActivity extends BaseActivity<ActivityStickyRvCoordOriginalBinding> {

private final ArrayList<String> mTitleList = new ArrayList<>(5);
private final ArrayList<Fragment> mFragments = new ArrayList<>(5);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sticky_rv_coord_original);
setTitle("CoordinatorLayout 嵌套滑动置顶");
initView();
}

private void initView() {
initFragmentList();
MyFragmentPagerAdapter myAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitleList);
binding.viewPager.setAdapter(myAdapter);
binding.viewPager.setOffscreenPageLimit(mTitleList.size() - 1);
myAdapter.notifyDataSetChanged();
binding.tabLayout.setupWithViewPager(binding.viewPager);
binding.mainRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
binding.mainRefreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
binding.mainRefreshLayout.finishRefresh();
}
}, 1000);
}
});
}

private void initFragmentList() {
mTitleList.clear();
mTitleList.add("关注");
mTitleList.add("推荐");
mTitleList.add("直播");
mTitleList.add("进口");
mTitleList.add("实惠");
mFragments.add(CoordinatorOriginalFragment.newInstance(""));
mFragments.add(CoordinatorOriginalFragment.newInstance(""));
mFragments.add(CoordinatorOriginalFragment.newInstance(""));
mFragments.add(CoordinatorOriginalFragment.newInstance(""));
mFragments.add(CoordinatorOriginalFragment.newInstance(""));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package me.jingbin.byrecyclerview.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import me.jingbin.byrecyclerview.R;
import me.jingbin.byrecyclerview.adapter.DataAdapter;
import me.jingbin.byrecyclerview.adapter.FeedStaggerAdapter;
import me.jingbin.byrecyclerview.bean.DataItemBean;
import me.jingbin.byrecyclerview.databinding.FragmentRefreshBinding;
import me.jingbin.byrecyclerview.utils.DataUtil;
import me.jingbin.byrecyclerview.utils.DensityUtil;
import me.jingbin.byrecyclerview.utils.ToastUtil;
import me.jingbin.library.ByRecyclerView;
import me.jingbin.library.decoration.GridSpaceItemDecoration;
import me.jingbin.library.decoration.SpacesItemDecoration;
import me.jingbin.library.view.OnItemFilterClickListener;

/**
* @author jingbin
*/
public class CoordinatorOriginalFragment extends BaseFragment<FragmentRefreshBinding> {

private static final String TYPE = "mType";
private String mType = "Android";
private boolean mIsPrepared;
private boolean mIsFirst = true;
private FeedStaggerAdapter feedStaggerAdapter;
private int page = 0;


@Override
public void onAttach(Context context) {
super.onAttach(context);
}

public static CoordinatorOriginalFragment newInstance(String type) {
CoordinatorOriginalFragment fragment = new CoordinatorOriginalFragment();
Bundle args = new Bundle();
args.putString(TYPE, type);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mType = getArguments().getString(TYPE);
}
}

@Override
public int setContent() {
return R.layout.fragment_refresh;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

// 准备就绪
mIsPrepared = true;
initAdapter();
}

@Override
protected void loadData() {
if (!mIsPrepared || !mIsVisible || !mIsFirst) {
return;
}
initAdapter();
}

private void initAdapter() {
binding.recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, RecyclerView.VERTICAL));
binding.recyclerView.addItemDecoration(new GridSpaceItemDecoration(DensityUtil.dip2px(activity, 8f)));
feedStaggerAdapter = new FeedStaggerAdapter((DataUtil.getStickyData()));
binding.recyclerView.setAdapter(feedStaggerAdapter);
binding.recyclerView.setOnItemClickListener(new OnItemFilterClickListener() {
@Override
protected void onSingleClick(View v, int position) {
if ("点我回到顶部".equals(feedStaggerAdapter.getItemData(position).getTitle())) {
binding.recyclerView.scrollToPosition(0);
}
}
});
// 设置自动刷新
binding.recyclerView.setOnLoadMoreListener(true, 1, new ByRecyclerView.OnLoadMoreListener() {
@Override
public void onLoadMore() {
if (page == 2) {
binding.recyclerView.loadMoreEnd();
return;
}
page = 2;
feedStaggerAdapter.addData(DataUtil.getStickyData());
binding.recyclerView.loadMoreComplete();
}
}, 1000);

mIsFirst = false;
}
}
76 changes: 76 additions & 0 deletions app/src/main/res/layout/activity_sticky_rv_coord_original.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<!--app:srlEnableOverScrollBounce="false" 关闭越界回弹功能-->
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/main_refresh_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_toolbar"
app:srlAccentColor="#fff"
app:srlReboundDuration="600">

<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlAccentColor="@color/color_4"
app:srlEnableLastTime="false" />

<!--coordinator部分-->
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/main_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/main_appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:fadingEdge="none"
app:elevation="0dp"
app:layout_behavior=".view.FlingBehavior"
app:layout_scrollFlags="scroll|enterAlways"
tools:ignore="WebViewLayout">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<include layout="@layout/layout_skeleton_headerview" />

</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@+id/tv_name_tool"
android:background="@color/colorWhite"
android:visibility="visible"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" />

</com.google.android.material.appbar.AppBarLayout>

<!--商品流-->
<me.jingbin.byrecyclerview.view.ViewPagerFixed
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f3f3f3"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

</com.scwang.smartrefresh.layout.SmartRefreshLayout>


</layout>

0 comments on commit 6a799d2

Please sign in to comment.