自定义listview列表来实现此效果
public class MainActivity extends AppCompatActivity {
private Context context = this;
private SwipeMenuListView listView;
private List<String> list = new ArrayList<>();
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
listView = ((SwipeMenuListView) findViewById(R.id.listView));
//虚拟数据源
for (int i = 0; i < 30; i++) {
list.add("item" + i);
}
adapter = new MyAdapter(context, list);
listView.setAdapter(adapter);
//点击事件一样写
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "你点击了" + list.get(position), Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "longClick" + list.get(position), Toast.LENGTH_SHORT).show();
return true;
}
});
//加入侧滑显示的菜单
//1.首先实例化SwipeMenuCreator对象
SwipeMenuCreator creater = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create置顶item
SwipeMenuItem item1 = new SwipeMenuItem(context);
// set item background
item1.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
// set item width
item1.setWidth(dp2px(90));
// set item title
item1.setTitle("置顶");
// set item title fontsize
item1.setTitleSize(18);
// set item title font color
item1.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(item1);
//同理create删除item
SwipeMenuItem deleteItem = new SwipeMenuItem(context);
// 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.mipmap.ic_delete);
// add to menu
menu.addMenuItem(deleteItem);
}
};
// set creator
listView.setMenuCreator(creater);
//2.菜单点击事件
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
//置顶的逻辑
if (position == 0) {
Toast.makeText(context, "此项已经置顶", Toast.LENGTH_SHORT).show();
return false;
}
String str = list.get(position);
for (int i = position; i > 0; i--) {
String s = list.get(i - 1);
list.remove(i);
list.add(i, s);
}
list.remove(0);
list.add(0, str);
adapter.notifyDataSetChanged();
break;
case 1:
//删除的逻辑
list.remove(position);
adapter.notifyDataSetChanged();
break;
}
return false;
}
});
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
}
下面是adpter
package com.text.sanbanhui.mydeletelist;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2016/2/23.
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List<String> list;
public MyAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item, null);
holder = new ViewHolder();
holder.txt = (TextView) convertView.findViewById(R.id.txt);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt.setText(list.get(position));
return convertView;
}
public class ViewHolder {
private TextView txt;
}
}
adpter布局文件
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:textSize="20sp" />
</LinearLayout>
主界面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.text.sanbanhui.mydeletelist.MainActivity">
<com.text.sanbanhui.mydeletelist.swipemenulistview.SwipeMenuListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.text.sanbanhui.mydeletelist.swipemenulistview.SwipeMenuListView>
</RelativeLayout>