最簡單的左右滑動item實現不同效果,滑動item實現

來源:互聯網
上載者:User

最簡單的左右滑動item實現不同效果,滑動item實現
一、效果

二、item布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="fill_parent"              android:layout_height="fill_parent"        >    <LinearLayout            android:id="@+id/item_long"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:background="#FFF"            >        <ImageView                android:id="@+id/img"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/ic_launcher"                />        <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="china cool"                android:textSize="30sp"                android:textColor="#000"                android:layout_gravity="center_vertical"                />        <Button                android:id="@+id/btn_delete"                android:layout_width="80dp"                android:layout_height="wrap_content"                android:background="#CFD8DC"                android:textSize="20sp"                android:gravity="center"                android:text="刪除"                />        <Button                android:id="@+id/btn_menu"                android:layout_width="80dp"                android:layout_height="wrap_content"                android:background="#FF3D00"                android:textSize="20sp"                android:gravity="center"                android:text="菜單"                />    </LinearLayout></LinearLayout>
三、activity
package com.qianfeng.gudao.SwipeDemo;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;public class MainActivity extends Activity implements View.OnTouchListener {    /**     * 需要拉動的內容     */    private View itemLong;    private Button btnDelete,btnMenu;    /**     * Called when the activity is first created.     */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        itemLong = findViewById(R.id.item_long);        itemLong.setOnTouchListener(this);        btnDelete = (Button) findViewById(R.id.btn_delete);        btnMenu = (Button) findViewById(R.id.btn_menu);    }    //上一次拖拽的X座標    private float lastX;    /**     * 這個事件也是處理TouchEvent的事件的     * @param v     * @param event     * @return boolean true 代表事件處理了,false 代表當前沒處理     */    @Override    public boolean onTouch(View v, MotionEvent event) {        boolean b = false;        if (v == itemLong){            int action = event.getAction();            switch (action){                case MotionEvent.ACTION_DOWN:                    //當使用onTouchListener時,Down這個方法,返回true                    //後面的move 才會繼續傳給這個介面                    b = true;                    lastX = 0;                    break;                case MotionEvent.ACTION_MOVE:                    //進行移動                    //凡是要實現控制項的拖拽,事件的 x,y                    //需要使用物理手機螢幕的rawX,rawY才可以進行運算                    float rawX = event.getRawX();                    if(lastX==0){                        lastX = rawX;                    }else{                        float step = rawX-lastX;                        //行動控制項                        //1.擷取原有的位置                        int left = itemLong.getLeft();                        int btnDeleteWidth = btnDelete.getWidth();                        int btnMenuWidth = btnMenu.getWidth();                        //2.計算新值                        float nl = left+step;                        int newLeft = (int) nl;                        int maxMove = btnDeleteWidth + btnMenuWidth;                        //目的是為了體現出動態效果                        if(newLeft <=0 && newLeft>=-maxMove){                            //3.代碼動態設定控制項的位置                            itemLong.setLeft(newLeft);                        }                        //更新上一次x,確保平滑滾動                        lastX = rawX;                    }                    break;            }        }        return b;    }}
四、一個問題

如果把布局改成相對布局,就無法顯示button,且獲得button的寬度也是0,說明button並沒有被畫出來(此說法錯誤,只能在控制項內部獲得該空間自身的尺寸,在外部得到的預設都是0,不過有一些方法,可以查看一下)。有的手機是可以顯示出來的。

五、其他思路

幀布局、控制項的隱藏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.