自定義tab吸頂效果一(原理)


PS:問題:什么是吸頂,吸頂有什么作用,吸頂怎么使用?

在很多app商城中,介紹軟件的時候就會使用吸頂效果,

吸頂有很多作用,一個最簡單粗暴的作用就是,讓用戶知道此刻在瀏覽哪個模塊,並可以選擇另外的模塊,不需要再滑到頂部,有時我們在查看一個軟件的簡介的時候上拉布局,導航欄還在,這里以App Store為例:如

吸頂該怎么用呢,這里有一個簡單的實現方法,在這期間有一個問題,是說ScrollViewd的滑動監聽不能檢測布局距離的問題,我查了寫資料,說是在6.0之前的sdk不支持,但是可以自己重寫方法。反正重寫很簡單。

先上效果圖:

實現原理:創建兩個布局,這兩個布局處於重疊狀態,一個布局上面顯示   背景為藍色+導航欄+數據內容,注意這里的導航欄隨着布局可移動,第二個布局顯示導航欄但處於影藏狀態,當滑動屏幕時,藍色部分全部被拉上去后,第二個布局顯示導航欄即可。

1:首先重寫ScrollView里的滑動方法,可創建一個接口,來實現接口里的方法即可

 

 public interface ScrollViewListener {
        void onScrollChanged(MyScrollView1 ceshimy, int l, int t, int oldl, int oldt);
    }

 

MyScrollView1.java

 

 

package cn.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ScrollView;

/**
 * Created by cMusketeer on 17/11/21.
 *
 * @author 劉志通
 */
public class MyScrollView1 extends ScrollView {
    public ScrollViewListener scrollViewListener = null;

    public MyScrollView1(Context context) {
        super(context);
    }

    public MyScrollView1(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView1(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MyScrollView1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    //上面都是自動生成的,下面為正文,是實現接口方法。
    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    public interface ScrollViewListener {
        void onScrollChanged(MyScrollView1 ceshimy, int l, int t, int oldl, int oldt);
    }
}

 

2:布局文件創建ceshilayout.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <cn.views.MyScrollView1
        android:id="@+id/id_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <RelativeLayout
            android:id="@+id/id_zong"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RelativeLayout
                android:id="@+id/id_img"
                android:layout_width="match_parent"
                android:layout_height="300px"
                android:layout_centerHorizontal="true"
                android:background="@color/colorPrimary"
                android:gravity="center">

                <ImageView

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignWithParentIfMissing="false"
                    android:src="@mipmap/ic_launcher" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/wenzi"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/id_img">
                <include layout="@layout/bottomlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></include>
            </RelativeLayout>
            <TextView
                android:layout_below="@id/wenzi"
                android:textSize="20dp"
                android:text="      Android是一種基於Linux的自由及開放源代碼的操作系統,主要使用於移動設備,如智能手機和平板電腦,由Google公司和開放手機聯盟領導及開發。尚未有統一中文名稱,中國大陸地區較多人使用“安卓”或“安致”。Android操作系統最初由Andy Rubin開發,主要支持手機。2005年8月由Google收購注資。2007年11月,Google與84家硬件制造商、軟件開發商及電信營運商組建開放手機聯盟共同研發改良Android系統。隨后Google以Apache開源許可證的授權方式,發布了Android的源代碼。第一部Android智能手機發布於2008年10月。Android逐漸擴展到平板電腦及其他領域上,如電視、數碼相機、游戲機等。2011年第一季度,Android在全球的市場份額首次超過塞班系統,躍居全球第一。 2013年的第四季度,Android平台手機的全球市場份額已經達到78.1%。[1]  2013年09月24日谷歌開發的操作系統Android在迎來了5歲生日,全世界采用這款系統的設備數量已經達到10億台。"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />


        </RelativeLayout>


    </cn.views.MyScrollView1>



    <RelativeLayout
        android:id="@+id/id_xianshi"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <include


            layout="@layout/bottomlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></include>
    </RelativeLayout>



</RelativeLayout>

 

 

這里有個導航欄的布局,bottom layout.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="#f5f4f4"
    android:layout_height="51dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
        android:textSize="20dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:textColor="#3e66dc"
        android:text="簡介"/>

    <TextView
        android:textColor="#50dc3e"
        android:gravity="center"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:text="評論" />
    <TextView
        android:textColor="#50dc3e"
        android:gravity="center"
        android:textSize="20dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:text="相關" />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorPrimary"></View>

</LinearLayout>

 



 

 


3:關鍵代碼,然而也就那么幾行,不過需要注意的是,在xml文件中,藍色背景的部分為300px,如果是300dp,則要把dp轉成px單位。

 

scrollView.setScrollViewListener(new MyScrollView1.ScrollViewListener() {

            @Override
            public void onScrollChanged(MyScrollView1 ceshimy, int l, int t, int oldl, int oldt) {
                //需要注意的是這里比較是px單位,如果是dp還要轉成px。
                if(t>=300){
                    tv_show.setVisibility(View.VISIBLE);
                }else{
                    tv_show.setVisibility(View.GONE);
                }
            }
        });

 

 

 

 

CeshiActivity.java總代碼

 

package day1.cn.xiaohangjia;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;

import cn.views.MyScrollView1;

/**
 * Created by cMusketeer on 17/11/21.
 *
 * @author 劉志通
 */
public class CeShiActivity extends AppCompatActivity {

    private RelativeLayout tv_show;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        setContentView(R.layout.ceshilayout);

        MyScrollView1 scrollView= (MyScrollView1) findViewById(R.id.id_scrollview);
        tv_show = (RelativeLayout) findViewById(R.id.id_xianshi);
        scrollView.setScrollViewListener(new MyScrollView1.ScrollViewListener() {

            @Override
            public void onScrollChanged(MyScrollView1 ceshimy, int l, int t, int oldl, int oldt) {
                //需要注意的是這里比較是px單位,如果是dp還要轉成px。
                if(t>=300){
                    tv_show.setVisibility(View.VISIBLE);
                }else{
                    tv_show.setVisibility(View.GONE);
                }
            }
        });






    }
}

 


完。

 




 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM