最簡單的左右滑動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,不過有一些方法,可以查看一下)。有的手機是可以顯示出來的。
五、其他思路
幀布局、控制項的隱藏