自定義控件之仿QQ側(cè)滑欄

最終效果圖

2017-09-20-02mzZzzz.gif

思路:
打開QQ之后我個人感覺,QQ的側(cè)滑欄和系統(tǒng)的一個控件很像,但是由于好奇所以試了一下,感覺近乎完美

        1.有兩個FrameLayout,一個測量時是全屏幕(中間的),一個測量時是半邊屏根據(jù)QQ的滑動初步判斷為1/5,所以獲取當(dāng)前屏幕寬度在除以5[左邊控件寬 = 屏幕寬度 - 屏幕寬度 / 5],就能得到左邊控件所要的寬度,然后使用FragmeLayout的setTranslationX();來移動當(dāng)前控件的X位置

全部代碼

package com.example.downlist.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Scroller;

/**
 * Created by Administrator on 2017/9/15.
 */

//仿QQ側(cè)邊欄
public class QQLiftView extends FrameLayout {


    private int startX;
    private int mid = 0;
    private FrameLayout midF;
    private FrameLayout liftF;
    //記錄相加的值
    private int size;
    //防止越界的值
    private int right1;
    private Scroller scroller;

    private int weiht;
    private int liftIntWight;
    private int liftIntWight_;

    public QQLiftView(Context context) {
        super(context);
        initView(context);
    }

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

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


    public void initView(Context context) {
        scroller = new Scroller(context);
    }


    @Override
    public void computeScroll() {
        super.computeScroll();

        if (scroller.computeScrollOffset()) {
            liftF.setTranslationX(scroller.getCurrX());
            size = scroller.getCurrX();
            midF.setTranslationX(scroller.getCurrX() / 3);
            invalidate();
        }
    }


    private void scToIndex(int index) {
        scroller.startScroll(size, 0, index - size, 0);
        invalidate();

    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        //獲取中間的Fragment
        if (midF == null)
            midF = (FrameLayout) getChildAt(0);
        if (liftF == null)
            liftF = (FrameLayout) getChildAt(1);
        //獲取左邊的Fragment

        right1 = midF.getRight();
        //獲取屏幕寬
        int right = getRight();
        int right5 = right / 5;
        liftF.layout(i, i1, i2, i3);
        liftIntWight = i2 - right5;

        liftIntWight_ = liftIntWight / 3;
        midF.layout(-liftIntWight_, i1, (liftIntWight - liftIntWight_), i3);
        //  measureView(i2 - right5);
        Log.e("----", "onLayout: " + (right - right5));
        weiht = (i2 - right5);
        //獲取子類,重新測量位置

        //startLayout();
    }

    //開始量測位置

    public void startLayout() {

        for (int i = 0; i < midF.getChildCount(); i++) {
            midF.getChildAt(i).layout(
                    0,
                    midF.getChildAt(i).getTop(),
                    liftIntWight,
                    midF.getChildAt(i).getBottom()
            );
        }


        for (int i = 0; i < liftF.getChildCount(); i++) {
            liftF.getChildAt(i).layout(
                    liftF.getChildAt(i).getLeft(),
                    liftF.getChildAt(i).getTop(),
                    liftF.getChildAt(i).getRight(),
                    liftF.getChildAt(i).getBottom()
            );
        }

    }

    //獲取要測量的子類
    public void meIndex() {
        for (int i = 0; i < liftF.getChildCount(); i++) {
            View childAt = liftF.getChildAt(i);
            ViewGroup.LayoutParams layoutParams = childAt.getLayoutParams();
            if (layoutParams == null) {
                layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            }

            int childMeasureSpec = ViewGroup.getChildMeasureSpec(0, 0, layoutParams.width);

            int height;

            int tempHei = layoutParams.height;

            if (tempHei == 0) {
                height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                height = MeasureSpec.makeMeasureSpec(tempHei, MeasureSpec.EXACTLY);
            }
            childAt.measure(childMeasureSpec, height);
        }


        for (int i = 0; i < midF.getChildCount(); i++) {
            View childAt = midF.getChildAt(i);
            ViewGroup.LayoutParams layoutParams = childAt.getLayoutParams();
            if (layoutParams == null) {
                layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            }

            int childMeasureSpec = ViewGroup.getChildMeasureSpec(0, 0, layoutParams.width);

            int height;

            int tempHei = layoutParams.height;

            if (tempHei == 0) {
                height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            } else {
                height = MeasureSpec.makeMeasureSpec(tempHei, MeasureSpec.EXACTLY);
            }


            childAt.measure(childMeasureSpec, height);

        }

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (liftF == null) {
            liftF = (FrameLayout) getChildAt(1);
        }

        if (midF == null) {
            midF = (FrameLayout) getChildAt(0);
        }
        //開始測量
        meIndex();

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                int endX = (int) event.getX();
                mid = endX - startX;
                //scrollBy(-mid, 0);
                ScrollByView(mid);
                startX = endX;
                //invalidate();
                Log.e("----", "onTouchEvent: " + mid);

                break;
            case MotionEvent.ACTION_UP:
                isLiftRight();
                break;

        }
        return true;
    }


    //處理是要左劃還是要往右滑
    private void isLiftRight() {
        //中間值
        int mid = weiht / 2;


        if (size > weiht) {
            liftF.setTranslationX(weiht);
            size = weiht;
            return;
        }

        if (size < 0) {
            liftF.setTranslationX(0);
            size = 0;
            return;
        }


        if (size > mid) {
            //向右
            scToIndex(weiht);
            Log.e("大小", "isLiftRight: " + "向右滑");

        }
        if (size < mid) {
            //向左
            scToIndex(0);
            Log.e("大小", "isLiftRight: " + "向左滑");
        }

    }


    public void ScrollByView(int x) {
        size += x;


        if (size < weiht && size >= 0) {
            liftF.setTranslationX(size);
            midF.setTranslationX(size / 3);
        }


    }

}

//Layout界面代碼

<?xml version="1.0" encoding="utf-8"?>
<com.example.downlist.view.QQLiftView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mySheView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <FrameLayout
        android:id="@+id/mid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#123456">

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="fitXY"
                android:src="@drawable/hxh" />


        </LinearLayout>

    </FrameLayout>

    <FrameLayout
        android:id="@+id/lift"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#654321">

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="fitXY"
                android:src="@drawable/hxh" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="dshshfhdskjfhdsf" />

        </LinearLayout>


    </FrameLayout>


</com.example.downlist.view.QQLiftView>

希望以上代碼對你的自定義控件有所幫助 0.0
如果那塊有不懂得請聯(lián)我喲...0.0 哈哈
----XINHAO_HAN

Demo下載:
鏈接: https://pan.baidu.com/s/1i5P6WML 密碼: 3283

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末服傍,一起剝皮案震驚了整個濱河市米同,隨后出現(xiàn)的幾起案子官硝,更是在濱河造成了極大的恐慌矮固,老刑警劉巖辞嗡,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異崔拥,居然都是意外死亡悔详,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門宰睡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蒲凶,“玉大人,你說我怎么就攤上這事拆内⌒玻” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵矛纹,是天一觀的道長臂聋。 經(jīng)常有香客問我,道長或南,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任艾君,我火速辦了婚禮采够,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘冰垄。我一直安慰自己蹬癌,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著逝薪,像睡著了一般隅要。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上董济,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天步清,我揣著相機(jī)與錄音,去河邊找鬼虏肾。 笑死廓啊,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的封豪。 我是一名探鬼主播谴轮,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吹埠!你這毒婦竟也來了第步?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤缘琅,失蹤者是張志新(化名)和其女友劉穎粘都,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胯杭,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡驯杜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了做个。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸽心。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖居暖,靈堂內(nèi)的尸體忽然破棺而出顽频,到底是詐尸還是另有隱情,我是刑警寧澤太闺,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布糯景,位于F島的核電站,受9級特大地震影響省骂,放射性物質(zhì)發(fā)生泄漏蟀淮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一钞澳、第九天 我趴在偏房一處隱蔽的房頂上張望怠惶。 院中可真熱鬧,春花似錦轧粟、人聲如沸策治。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽通惫。三九已至茂翔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間履腋,已是汗流浹背珊燎。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留府树,地道東北人俐末。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像奄侠,于是被迫代替她去往敵國和親卓箫。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容