美文网首页
使用CoordinatorLayout实现简单的悬浮效果

使用CoordinatorLayout实现简单的悬浮效果

作者: 写不完的bug | 来源:发表于2018-06-14 14:14 被阅读0次

CoordinatorLayout是design包里的控件,主要用来实现悬浮效果,下面简单介绍一下实现过程

  1. 添加依赖
implementation 'com.android.support:design:26.1.0'

就是添加design包

  1. 创建布局
<?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"
    tools:context="com.yuntu.coordinatorlayoutsimple.CoordinatorLayoutActivity"
    >
  <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#00000000"
      app:elevation="0dp"
      >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#FF352E"
        app:layout_scrollFlags="scroll"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#4E65FE"
        android:gravity="center_vertical"
        android:text="测试"
        />
  </android.support.design.widget.AppBarLayout>
  <com.scwang.smartrefresh.layout.SmartRefreshLayout
      android:id="@+id/refresh"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#eeeeee"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
  </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</android.support.design.widget.CoordinatorLayout>

布局顶层是CoordinatorLayout,上半部分采用的是AppBarLayout,当页面上滑时,AppBarLayout会随整个页面一起滑动,当ImageView滑动顶部消失时,下面的TextView会悬浮到顶部,不再滑动,下面的SmartRefreshLayout开始滑动,这就是悬浮效果,里面最关键的两个属性是app:layout_scrollFlags="scroll" 跟app:layout_behavior="@string/appbar_scrolling_view_behavior"> ,介绍layout_scrollFlags 的五种属性介绍layoutbehavior,这两篇比较详细,这里主要说一下,下面下拉加载更多是遇到的一个小问题。

  1. 下拉加载遇到的小问题
   public class CoordinatorLayoutActivity extends AppCompatActivity {

  private int mCurrentPage = 1;

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

  private void initView() {
    RecyclerView recyclerView = findViewById(R.id.recycleview);
    recyclerView.setLayoutManager(
        new LinearLayoutManager(CoordinatorLayoutActivity.this, LinearLayoutManager.VERTICAL,
            false));
    final RecycleViewAdapter recycleViewAdapter =
        new RecycleViewAdapter(CoordinatorLayoutActivity.this);
    recyclerView.setAdapter(recycleViewAdapter);
    SmartRefreshLayout refreshLayout = findViewById(R.id.refresh);
    //模拟加载数据
    ArrayList<String> strings = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      strings.add("测试数据" + i);
    }
    recycleViewAdapter.setCollection(strings);
    //禁止下拉加载
    refreshLayout.setEnableRefresh(false);
    refreshLayout.setHeaderHeight(0);
    //实现上拉加载更多
    refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
      @Override public void onLoadmore(RefreshLayout refreshlayout) {
        mCurrentPage++;
        if (mCurrentPage < 4) {
          ArrayList<String> strings = new ArrayList<>();
          for (int i = 0; i < 10; i++) {
            strings.add("测试数据" + i);
          }
          recycleViewAdapter.addCollection(strings);
          refreshlayout.finishLoadmore();
        } else {
          refreshlayout.finishLoadmoreWithNoMoreData();
        }
      }
    });
  }
}

这个是页面代码,当下拉加载更多完成后,必须调用refreshlayout.finishLoadmore(),如果不调用,会出现滑动错乱的情况,上下两部分一起滑动。

相关文章

网友评论

      本文标题:使用CoordinatorLayout实现简单的悬浮效果

      本文链接:https://www.haomeiwen.com/subject/xzkyeftx.html