SwipeListView滑動刪除Android

來源:互聯網
上載者:User

標籤:

先來看activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:swipe="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <com.fortysevendeg.swipelistview.SwipeListView        android:id="@+id/example_lv_list"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:listSelector="#00000000"        swipe:swipeActionLeft="dismiss"        swipe:swipeActionRight="reveal"        swipe:swipeAnimationTime="0"        swipe:swipeBackView="@+id/back"        swipe:swipeCloseAllItemsWhenMoveList="true"        swipe:swipeFrontView="@+id/front"        swipe:swipeMode="both"        swipe:swipeOffsetLeft="0dp"        swipe:swipeOffsetRight="0dp"        swipe:swipeOpenOnLongPress="false" /></RelativeLayout>

這裡就一個swipelistview控制項,我說幾個不易理解的屬性

表示滑動時的操作,dismiss表示滑動時刪除,如果設定為reveal表示滑動時會顯示出item後面的選項 
swipe:swipeActionLeft=”dismiss” 
swipe:swipeActionRight=”reveal” 
這個是背面布局的id(我們把直接看到的布局叫做前面的,滑動之後才能看到的布局叫做背面的),必須與背面布局id對應 
swipe:swipeBackView=”@+id/back” 
這個是滾動時候是否關閉背面的布局,true表示關閉,false表示不關閉,一般設定為true 
swipe:swipeCloseAllItemsWhenMoveList=”true” 
這個是前面布局的id,要與布局的id對應 
swipe:swipeFrontView=”@+id/front” 
both表示可以向左滑也可以向右滑,right和left分別表示只能向有或者向左滑動。 
swipe:swipeMode=”both” 
下面兩個表示向左或者向右滑動時的位移量,一般不在xml檔案中設定,而是在代碼中根據設定的大小來設定位移量。 
swipe:swipeOffsetLeft=”0dp” 
swipe:swipeOffsetRight=”0dp” 
再來看看Item布局檔案,這裡包括前面的和後面的,兩個重疊在一起:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" >    <!-- linearlayout中的布局是每一項後面隱藏的布局 -->    <LinearLayout  android:id="@+id/back" android:layout_width="match_parent" android:layout_height="80dp" android:background="#eee" android:tag="back" >        <Button  android:id="@+id/example_row_b_action_1" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginRight="10dp" android:layout_weight="1" android:text="測試" />        <Button  android:id="@+id/example_row_b_action_2" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_weight="1" android:text="刪除" />        <Button  android:id="@+id/example_row_b_action_3" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_weight="1" android:text="編輯" />    </LinearLayout>    <!-- 這裡是前台顯示的布局 -->    <RelativeLayout  android:id="@+id/front" android:layout_width="match_parent" android:layout_height="80dp" android:background="#ffffff" android:orientation="vertical" android:tag="front" >        <TextView  android:id="@+id/example_row_tv_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="18sp" />    </RelativeLayout></FrameLayout>

這個布局是一個常規布局,我就不解釋了。

MainActivity.java,關鍵地方都有注釋

public class MainActivity extends Activity {    private SwipeListView mSwipeListView ;    private SwipeAdapter mAdapter ;    public static int deviceWidth ;    private List<String> testData ;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mSwipeListView = (SwipeListView) findViewById(R.id.example_lv_list);        testData = getTestData();        //資料適配        mAdapter = new SwipeAdapter(this, R.layout.package_row, testData,mSwipeListView);        //拿到裝置寬度        deviceWidth = getDeviceWidth();        mSwipeListView.setAdapter(mAdapter);        //設定事件監聽        mSwipeListView.setSwipeListViewListener( new TestBaseSwipeListViewListener());        reload();    }    private List<String> getTestData() {        String [] obj = new String[]{"紅樓夢","西遊記","水滸傳","管錐編","宋詩選注","三國演義","android開發進階編程","紅樓夢","西遊記","水滸傳","管錐編","宋詩選注","三國演義","android開發進階編程"};        List<String> list = new ArrayList<String>(Arrays.asList(obj));        return list;    }    private int getDeviceWidth() {        return getResources().getDisplayMetrics().widthPixels;    }    private void reload() {// mSwipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_LEFT);// mSwipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_REVEAL);// mSwipeListView.setSwipeActionRight(settings.getSwipeActionRight());        //滑動時向左位移量,根據裝置的大小來決定位移量的大小        mSwipeListView.setOffsetLeft(deviceWidth * 1 / 3);        mSwipeListView.setOffsetRight(deviceWidth * 1 / 3);// mSwipeListView.setOffsetRight(convertDpToPixel(settings.getSwipeOffsetRight()));        //設定動畫時間        mSwipeListView.setAnimationTime(30);        mSwipeListView.setSwipeOpenOnLongPress(false);    }    class TestBaseSwipeListViewListener extends BaseSwipeListViewListener{        //點擊每一項的響應事件        @Override        public void onClickFrontView(int position) {            super.onClickFrontView(position);            Toast.makeText(getApplicationContext(), testData.get(position), Toast.LENGTH_SHORT).show();        }        //關閉事件        @Override        public void onDismiss(int[] reverseSortedPositions) {            for (int position : reverseSortedPositions) {                Log.i("lenve", "position--:"+position);                testData.remove(position);            }            mAdapter.notifyDataSetChanged();        }    }}

資料配接器:

public class SwipeAdapter extends ArrayAdapter<String> {    private LayoutInflater mInflater ;    private List<String> objects ;    private SwipeListView mSwipeListView ;    public SwipeAdapter(Context context, int textViewResourceId,List<String> objects, SwipeListView mSwipeListView) {        super(context, textViewResourceId, objects);        this.objects = objects ;        this.mSwipeListView = mSwipeListView ;        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    }    @Override    public View getView(final int position, View convertView, ViewGroup parent) {        ViewHolder holder = null ;        if(convertView == null){            convertView = mInflater.inflate(R.layout.package_row, parent, false);            holder = new ViewHolder();            holder.mFrontText = (TextView) convertView.findViewById(R.id.example_row_tv_title);            holder.mBackEdit = (Button) convertView.findViewById(R.id.example_row_b_action_3);            holder.mBackDelete = (Button) convertView.findViewById(R.id.example_row_b_action_2);            convertView.setTag(holder);        }else{            holder = (ViewHolder) convertView.getTag();        }        holder.mBackDelete.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                //關閉動畫                mSwipeListView.closeAnimate(position);                //調用dismiss方法刪除該項(這個方法在MainActivity中)                mSwipeListView.dismiss(position);            }        });        String item = getItem(position);        holder.mFrontText.setText(item);        return convertView;    }    class ViewHolder{        TextView mFrontText ;        Button mBackEdit,mBackDelete ;    }}

SwipeListView滑動刪除Android

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.