android開發之SwipeListView的使用 -开发者知识库

android開發之SwipeListView的使用 -开发者知识库,第1张

實現一種類似於qq中滑動列表的功能:

android開發之SwipeListView的使用 -开发者知识库,這里寫圖片描述,第2张

向左或者向右滑動,然后執行相關操作。

這里用到的是GitHub上的開源控件SwipeListView,下載地址https://github.com/47deg/android-swipelistview,下載好了之后,我們可以把下載文件當作一個庫文件引用它,當然也可以直接把源代碼拷貝到我們的想木當中。SwipeListView還依賴一個Github上的第三方控件,叫做NineOldAndroids,下載地址https://github.com/JakeWharton/NineOldAndroids,nineoldandroids和swipelistview一樣,也是可以直接拷貝代碼進來或者當作一個庫文件來引用。本案例統一把他們都當作庫文件來引用。

先來看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 ;
}
}

本項目完整代碼下載http://pan.baidu.com/s/1o6knuUA

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复