您好,登錄后才能下訂單哦!
我當時也是比較迷茫選哪個控件比較好,后面再github上找到一個這個SwipeMenuListView開源項目完成的,使用這個項目非常簡單,里面有兩個例子,復雜的跟簡單的,我當時自己寫了個Demos來學習,廢話不多說了,直接貼代碼。
public class MainActivity extends Activity {
private AppAdapter mAdapter;
private SwipeMenuListView mListView;
ArrayList<String> data=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 9; i++) {
data.add("position為"+i);
}
mListView = (SwipeMenuListView) findViewById(R.id.listView);
mAdapter = new AppAdapter();
mListView.setAdapter(mAdapter);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create "open" item
SwipeMenuItem openItem = new SwipeMenuItem(
getApplicationContext());
// set item background
openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
0xCE)));
// set item width
openItem.setWidth(dp2px(90));
// set item title
openItem.setTitle("Open");
// set item title fontsize
openItem.setTitleSize(18);
// set item title font color
openItem.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(openItem);
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(
getApplicationContext());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(dp2px(90));
// set a icon
deleteItem.setIcon(R.drawable.ic_delete);
// add to menu
menu.addMenuItem(deleteItem);
}
};
mListView.setMenuCreator(creator);
mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public void onMenuItemClick(int position, SwipeMenu menu, int index) {
//index=0 open index =1 刪除
switch (index) {
case 0:
Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
break;
case 1:
// Log.e("position", ""+position);
data.remove(position);
Log.e("onMenuItemClick 1", "position="+position+"##index="+index+"data.tos"+data.toString());
Toast.makeText(MainActivity.this, ""+position+"index="+index, Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mAdapter.notifyDataSetChanged();
}
}, 500);
break;
default:
break;
}
}
});
}
class AppAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.e("getview", data.toString());
return data.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// biao bb = null;
// if(convertView==null){
// convertView = getLayoutInflater().inflate(R.layout.item, null);
// bb = new biao();
// bb.text = (TextView) convertView.findViewById(R.id.textView1);
// convertView.setTag(bb);
// }else{
// bb = (biao) convertView.getTag();
// }
// bb.text.setText(data.get(position));
// Log.e("tv", data.get(position));
if (convertView == null) {
convertView = View.inflate(getApplicationContext(),
R.layout.item, null);
new ViewHolder(convertView);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.tv.setText(data.get(position));
return convertView;
}
class ViewHolder {
TextView tv;
public ViewHolder(View view) {
tv = (TextView) view.findViewById(R.id.textView1);
view.setTag(this);
}
}
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
}
}
主XML也是很簡單的。
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。