整合了重新整理、加載更多、滑動删除功能的XListview
轉載請說明出處:http://blog.csdn.net/bz419927089
如果想做帶有重新整理、加載更多功能的listview,我們可以使用XListview。(已經停止維護,github位址: https://github.com/Maxwin-z/XListView-Android )
如果想做帶有滑動删除功能的listview,我們可以使用SwipeListview。(github位址: https://github.com/47deg/android-swipelistview )
如果想做重新整理、加載更多、滑動删除的listview呢?抱歉,我暫時還沒有發現同時擁有這些功能的listview。不過,我們可以整合多個開源項目,做出這樣的效果。
如果要實作這個功能,我們需要兩個開源項目的配合,一個就是XListview,來實作重新整理加載功能,而滑動删除功能,我們則需要另外一個大神的開源項目,AndroidSwipeLayout(github位址: https://github.com/daimajia/AndroidSwipeLayout )。
話不多說,我們先看一下做出來的效果。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5SRyplWJlnbvwVbvNmLs92bjlWd05iMn1Wavw1LcpDc0RHaiojIsJye.gif)
我感覺效果很不錯,對于一般的開發需求來說,已經夠了,下面,我們看一下項目的內建過程。
下面是整個項目的結構。
daimajia.swipe是AndroidSwipeLayout的代碼,me.maxwin.view是XListview的代碼,不需要再導入其他的lib包了。
layout檔案夾下面的xlistview_footer,xlistview_header是XListview項目需要的布局檔案,values檔案夾下面的attrs是
SwipeLayout一些預置參數,colors是用到的兩個顔色值,strings是XListview用到的字元串常量。
介紹完一些重要的檔案夾,我們看一下 ListViewAdapter的代碼實作。
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.daimajia.swipe.SimpleSwipeListener;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
/**
*
* @author zhaokaiqiang
*
*/
public class ListViewAdapter extends BaseSwipeAdapter {
// 上下文對象
private Context mContext;
// 構造函數
public ListViewAdapter(Context mContext) {
this.mContext = mContext;
}
// SwipeLayout的布局id
@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
@Override
public View generateView(int position, ViewGroup parent) {
View v = LayoutInflater.from(mContext).inflate(R.layout.listview_item,
parent, false);
final SwipeLayout swipeLayout = (SwipeLayout) v
.findViewById(getSwipeLayoutResourceId(position));
// 當隐藏的删除menu被打開的時候的回調函數
swipeLayout.addSwipeListener(new SimpleSwipeListener() {
@Override
public void onOpen(SwipeLayout layout) {
Toast.makeText(mContext, "Open", Toast.LENGTH_SHORT).show();
}
});
// 輕按兩下的回調函數
swipeLayout
.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
@Override
public void onDoubleClick(SwipeLayout layout,
boolean surface) {
Toast.makeText(mContext, "DoubleClick",
Toast.LENGTH_SHORT).show();
}
});
// 添加删除布局的點選事件
v.findViewById(R.id.ll_menu).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(mContext, "delete", Toast.LENGTH_SHORT).show();
//點選完成之後,關閉删除menu
swipeLayout.close();
}
});
return v;
}
//對控件的填值操作獨立出來了,我們可以在這個方法裡面進行item的資料指派
@Override
public void fillValues(int position, View convertView) {
TextView t = (TextView) convertView.findViewById(R.id.position);
t.setText((position + 1) + "."<p class="p1"><span class="s1"> + </span>"我就是一行很長很長很長很長很長很長很長很長很長很長很長很長很長的測試文本"</p>);
}
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
}
如果我們想使用帶有滑動布局的listvew,我們需要繼承 BaseSwipeAdapter,然後對上面代碼中提到的重要方法進行實作。
除了adapter需要改變之外,我們還需要修改listview的item的布局檔案。
下面是listview_item.xml的代碼實作。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp" >
<com.daimajia.swipe.SwipeLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/ll_menu"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light"
android:gravity="center" >
<ImageView
android:id="@+id/trash"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/trash" />
<TextView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/item_selector"
android:padding="6dp" >
<TextView
android:id="@+id/position"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</com.daimajia.swipe.SwipeLayout>
</LinearLayout>
整個item的布局内容需要用SwipeLayout包裹起來,然後添加id,這個id我們需要在adapter裡面作為
getSwipeLayoutResourceId()的傳回值。
設定好adapter之後,我們就可以在MainActivity裡面為我們的xlistview添加擴充卡了,下面是一個簡單的例子。
package com.example.swiperefreshloadlistview;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;
/**
*
* @author zhaokaiqiang
*
*/
public class MainActivity extends Activity {
private XListView mListView;
// 隻是用來模拟異步擷取資料
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
mListView = (XListView) findViewById(R.id.xListView);
// 設定xlistview可以加載、重新整理
mListView.setPullLoadEnable(true);
mListView.setPullRefreshEnable(true);
// 設定回調函數
mListView.setXListViewListener(new IXListViewListener() {
@Override
public void onRefresh() {
// 模拟重新整理資料,1s之後停止重新整理
handler.postDelayed(new Runnable() {
@Override
public void run() {
mListView.stopRefresh();
Toast.makeText(MainActivity.this, "refresh",
Toast.LENGTH_SHORT).show();
}
}, 1000);
}
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
// 模拟加載資料,1s之後停止加載
@Override
public void run() {
mListView.stopLoadMore();
Toast.makeText(MainActivity.this, "loadMore",
Toast.LENGTH_SHORT).show();
}
}, 1000);
}
});
// 設定擴充卡
mListView.setAdapter(new ListViewAdapter(this));
}
}
好了,完成這些之後,我們就實作了重新整理、加載、滑動删除功能的多功能lixtview了。
項目的源代碼,請到我的github下載下傳。
https://github.com/ZhaoKaiQiang/SwipeRefreshLoadListview