Android固定Tab,下拉刷新,嵌套滑動

使用原生的控件CoordinatorLayout和AppBarLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:elevation="0dp"
        app:layout_scrollFlags="scroll">

        <TextView
            android:id="@+id/move"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/colorAccent"
            android:clickable="true"
            android:gravity="center"
            android:text="Hello World!"
            app:layout_scrollFlags="scroll" />

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:background="@android:color/darker_gray"
            android:gravity="center"
            android:text="title"
            app:layout_scrollFlags="scroll|enterAlways" />


        <com.scwang.smartrefresh.layout.SmartRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:overScrollMode="never"
                android:scrollbars="none">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/hello"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:background="@color/colorAccent"
                        android:clickable="true"
                        android:gravity="center"
                        android:text="Hello World!" />

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/colorPrimary" />
                </LinearLayout>
            </android.support.v4.widget.NestedScrollView>
        </com.scwang.smartrefresh.layout.SmartRefreshLayout>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>
  • 其中頂部的TextView用AppBarLayout包裹,設置
app:layout_scrollFlags="scroll"

,表示滑動該區域可以連帶畫面整體滑動

  • 中間LinearLayout設置
app:layout_behavior="@string/appbar_scrolling_view_behavior"

,表示該區域可以跟隨頂部AppBarLayout一起滑動

  • 中間的title,設置
app:layout_scrollFlags="scroll|enterAlways"

,滑動到頂部固定在頂部

 

附上Activity和Adaper的代碼

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new ListAdapter());

        TextView textView = findViewById(R.id.hello);
        textView.requestFocus();
        textView.setFocusable(true);
        textView.setFocusableInTouchMode(true);
    }
}
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(new TextView(viewGroup.getContext()));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        TextView textView = (TextView) viewHolder.itemView;

        textView.setText("TextView" + i);
    }

    @Override
    public int getItemCount() {
        return 50;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章