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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| package com.xin.FixBar;
import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle; import android.util.Log;
public class MainActivity extends AppCompatActivity { private MyScrollView myScrollView; private RecyclerView recyclerView; private String[] data; private boolean navBar = false; private MyAdapter myAdapter;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); setRecycler(); setScroll(); }
private void setScroll() { myScrollView.setOnNavBarListener(new MyScrollView.OnNavBarListener() { @Override
public void OnFix() { if (!navBar){ navBar = true; recyclerView.setNestedScrollingEnabled(true); Log.d("setScroll", "OnReset: recyclerView已允许"); } }
@Override
public void OnReset() { if (navBar){ navBar = false; recyclerView.setNestedScrollingEnabled(false); Log.d("setScroll", "OnReset: recyclerView已禁止"); } } }); }
private void setRecycler() { LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager);
int pxHeight = this.getResources().getDisplayMetrics().heightPixels; int StatusBarHeight = (int) (60 * this.getResources().getDisplayMetrics().density + 0.5f); recyclerView.getLayoutParams().height = pxHeight - StatusBarHeight; myAdapter = new MyAdapter(this,data); recyclerView.setAdapter(myAdapter);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy);
if (!recyclerView.canScrollVertically(-1)){ recyclerView.setNestedScrollingEnabled(false); } } }); }
private void init() { myScrollView = findViewById(R.id.myScrollView); recyclerView = findViewById(R.id.recyclerView); data = new String[]{ "测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10","测试11","测试12","测试13","测试14", "测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10","测试11","测试12","测试13","测试14", "测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10","测试11","测试12","测试13","测试14", "测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10","测试11","测试12","测试13","测试14", "测试1","测试2","测试3","测试4","测试5","测试6","测试7","测试8","测试9","测试10","测试11","测试12","测试13","测试14" }; } }
|