项目中遇到一个要求 在listview 列表中左划item 显示删除按钮(仿QQ获取微信左划删除)
Item布局
<?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:background="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/left_listview_item_leftlayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<TextView
android:id="@+id/left_listview_item_textview"
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center|left"
android:text="【1】姓名:张三" />
</LinearLayout>
<LinearLayout
android:id="@+id/left_listview_item_rightlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FF0000">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@mipmap/right_del"
android:contentDescription="@null"
android:scaleType="fitCenter" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Activity布局
<?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">
<com.wjn.myview.view.LeftListView
android:id="@+id/activity_leftlistview_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:divider="#66CCCCCC"
android:dividerHeight="0.5dp">
</com.wjn.myview.view.LeftListView>
</LinearLayout>
JavaBean
public class MyJaveBean {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
values样式
<!-- listview左划删除 -->
<declare-styleable name="swipelistviewstyle">
<attr name="right_width" format="dimension"></attr>
</declare-styleable>
接口
/**
* 监听listview 左划删除监听
* */
public interface onRightItemClickListener {
void onRightItemClick(View v, int position);
}
Activity调用
/**
* 自定义 ListView左划删除
* */
public class LeftListViewActivity extends AppCompatActivity {
private LeftListView leftListView;
private LeftAdapter leftAdapter;
private List<MyJaveBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leftlistview);
leftListView=findViewById(R.id.activity_leftlistview_listview);
list=new ArrayList<MyJaveBean>();
MyJaveBean myJaveBean1=new MyJaveBean();
myJaveBean1.setName("张三");
MyJaveBean myJaveBean2=new MyJaveBean();
myJaveBean2.setName("李四");
MyJaveBean myJaveBean3=new MyJaveBean();
myJaveBean3.setName("王五");
MyJaveBean myJaveBean4=new MyJaveBean();
myJaveBean4.setName("韦德");
MyJaveBean myJaveBean5=new MyJaveBean();
myJaveBean5.setName("詹姆斯");
MyJaveBean myJaveBean6=new MyJaveBean();
myJaveBean6.setName("蒿俊闵");
for(int i=0;i<10;i++){
list.add(myJaveBean1);
list.add(myJaveBean2);
list.add(myJaveBean3);
list.add(myJaveBean4);
list.add(myJaveBean5);
list.add(myJaveBean6);
}
leftAdapter=new LeftAdapter(LeftListViewActivity.this,list,leftListView.getRightViewWidth());
leftListView.setAdapter(leftAdapter);
//左划删除
leftAdapter.setOnRightItemClickListener(new onRightItemClickListener() {
@Override
public void onRightItemClick(View v, int position) {
String result=list.get(position).getName()+"\n"+position;
Toast.makeText(LeftListViewActivity.this,result,Toast.LENGTH_LONG).show();
}
});
}
}
效果: