欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现顶部悬浮效果

程序员文章站 2023-12-17 13:47:16
本文实例为大家分享了android实现顶部悬浮效果的具体代码,供大家参考,具体内容如下 效果图  布局

本文实例为大家分享了android实现顶部悬浮效果的具体代码,供大家参考,具体内容如下

效果图

Android实现顶部悬浮效果

 布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.swiperefreshlayout 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:id="@+id/swiperefreshlayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".ui.activity.newpersonalcardactivity">

 <android.support.design.widget.coordinatorlayout
  android:id="@+id/rootlayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <android.support.design.widget.appbarlayout
   android:id="@+id/appbarlayout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

   <android.support.design.widget.collapsingtoolbarlayout
    android:id="@+id/collapsingtoolbarlayout"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    app:layout_scrollflags="scroll|exituntilcollapsed"
    app:titleenabled="false">

    <linearlayout
     android:id="@+id/headlayout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     app:layout_collapsemode="pin"
     app:layout_collapseparallaxmultiplier="0.7">

     <include layout="@layout/personal_new_top" />
    </linearlayout>

    <android.support.v7.widget.toolbar
     android:id="@+id/toolbar"
     android:layout_width="match_parent"
     android:layout_height="44dp"
     android:background="@color/white"
     android:navigationicon="@drawable/icon_back"
     app:contentinsetleft="0dp"
     app:contentinsetstart="0dp"
     app:layout_collapsemode="pin"
     app:navigationicon="@drawable/icon_back">

     <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:singleline="true"
      android:textcolor="@color/c333333"
      android:textsize="16sp"
      tools:text="讲师" />
    </android.support.v7.widget.toolbar>
   </android.support.design.widget.collapsingtoolbarlayout>

   <include layout="@layout/new_personal_tabs" />
  </android.support.design.widget.appbarlayout>

  <android.support.v4.widget.nestedscrollview
   android:id="@+id/nestedscrollview"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:fillviewport="true"
   android:scrollbars="none"
   app:layout_behavior="@string/appbar_scrolling_view_behavior">

   <android.support.v4.view.viewpager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  </android.support.v4.widget.nestedscrollview>
 </android.support.design.widget.coordinatorlayout>
</android.support.v4.widget.swiperefreshlayout>


java

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 arouter.getinstance().inject(this);
 setcontentview(r.layout.activity_new_personal_card);
 butterknife.bind(this);
 setsupportactionbar(toolbar);
 getsupportactionbar().setdisplayhomeasupenabled(true);
 getsupportactionbar().setdisplayshowtitleenabled(false);
 toolbar.setnavigationonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view view) {
  onbackpressed();
  }
 });
 initdata();
 }

  private void initrefresh() {
 appbarlayout.addonoffsetchangedlistener(new appbarlayout.onoffsetchangedlistener() {
  @override
  public void onoffsetchanged(appbarlayout appbarlayout, int verticaloffset) {
  if (verticaloffset >= 0) {
   swiperefreshlayout.setenabled(true);
  } else {
   swiperefreshlayout.setenabled(false);
  }
  }
 });
 swiperefreshlayout.setonrefreshlistener(new swiperefreshlayout.onrefreshlistener() {
  @override
  public void onrefresh() {
  initdata();
  swiperefreshlayout.setrefreshing(false);
  }
 });
 }

 注意事项

1.清单文件需要配置

android:theme="@style/personalcard"
<style name="personalcard" parent="theme.appcompat.light.darkactionbar">
  <!-- customize your theme here. -->
  <item name="windowactionbar">false</item>
  <item name="windownotitle">true</item>
  <item name="colorprimary">@color/colorprimary</item>
  <item name="colorprimarydark">@color/colorprimarydark</item>
  <item name="coloraccent">@color/coloraccent</item>
 </style>

2.gradle配置需要24以上

compilesdkversion 24
//------------------ 在线依赖开始 ----------------
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:cardview-v7:24.2.0'
compile 'com.android.support:design:24.2.0'

3.recyclerview的下滑和swiprefreshlayout的下拉刷新会冲突,所以需要监听appbarlayout的位移,当位移等于0时,即appbarlayout全部展示,可以下拉刷新

4.string.xml

复制代码 代码如下:
<string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.appbarlayout$scrollingviewbehavior</string>

切记

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: