diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5304a03..46c4c21 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,6 +13,7 @@ android:theme="@style/AppTheme"> + diff --git a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/MainActivity.java b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/MainActivity.java index 7e6d68b..68017a2 100644 --- a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/MainActivity.java +++ b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/MainActivity.java @@ -28,6 +28,14 @@ public void onClick(View v) { } }); + findViewById(R.id.btn_sink_sticky).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(MainActivity.this,SinkStickyActivity.class); + startActivity(intent); + } + }); + findViewById(R.id.btn_consecutive).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/app/src/main/java/com/donkingliang/consecutivescrollerdemo/SinkStickyActivity.java b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/SinkStickyActivity.java new file mode 100644 index 0000000..9b3c677 --- /dev/null +++ b/app/src/main/java/com/donkingliang/consecutivescrollerdemo/SinkStickyActivity.java @@ -0,0 +1,55 @@ +package com.donkingliang.consecutivescrollerdemo; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.view.View; +import android.widget.FrameLayout; + +import com.donkingliang.consecutivescroller.ConsecutiveScrollerLayout; +import com.donkingliang.consecutivescrollerdemo.adapter.RecyclerViewAdapter; + +public class SinkStickyActivity extends AppCompatActivity { + + private ConsecutiveScrollerLayout scrollerLayout; + private FrameLayout flSink; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_sink_sticky); + + scrollerLayout = findViewById(R.id.scrollerLayout); + + flSink = findViewById(R.id.fl_sink); + + RecyclerView recyclerView1 = findViewById(R.id.recyclerView1); + recyclerView1.setLayoutManager(new LinearLayoutManager(this)); + RecyclerViewAdapter adapter1 = new RecyclerViewAdapter(this,"RecyclerView1-"); + recyclerView1.setAdapter(adapter1); + + RecyclerView recyclerView2 = findViewById(R.id.recyclerView2); + recyclerView2.setLayoutManager(new LinearLayoutManager(this)); + RecyclerViewAdapter adapter2 = new RecyclerViewAdapter(this,"RecyclerView2-"); + recyclerView2.setAdapter(adapter2); + + // 监听滑动 + scrollerLayout.setOnVerticalScrollChangeListener(new ConsecutiveScrollerLayout.OnScrollChangeListener() { + @Override + public void onScrollChange(View v, int scrollY, int oldScrollY, int scrollState) { + if (scrollY > flSink.getHeight()){ + scrollerLayout.setStickyOffset(0); // 恢复吸顶偏移量 + } else { + // 通过设置吸顶便宜量,实现flSink滑动隐藏时的向上移动效果 + scrollerLayout.setStickyOffset(-scrollY / 2); + } + } + }); + + + // 如果flSink不需要设置隐藏时的平移动画,就不用设置滑动监听和setStickyOffset + + // 如果要禁止滑动flSink来滑动整个布局,flSink不用设置app:layout_isTriggerScroll="true"即可。 + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index d341bda..5efbc33 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -23,6 +23,12 @@ android:layout_height="wrap_content" android:text="布局吸顶常驻" /> +