真正实现仿微信左滑删除该Item,Item点击事件无影响,开源强大啊!!! 先看图:
1.最新项目需求,在Listview上左滑删除该条信息,各种坑走过啊:用GestureDetector和OnTouch配合添加删除按钮,结果实现了左滑可以添加删除,但该Item的删除事件无法执行,,才疏学浅啊,于是作废,网上各种搜各种坑啊,,,,,,
2.直接上代码:
01.先添加依赖:
dependencies {
compile 'com.baoyz.swipemenulistview:library:1.3.0'
}
02.布局文件:
<?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"> <include layout="@layout/base_toolbar" /> <com.baoyz.swipemenulistview.SwipeMenuListView android:id="@+id/lv_modification_vehicle_information" android:layout_width="match_parent" android:layout_height="match_parent"></com.baoyz.swipemenulistview.SwipeMenuListView> </LinearLayout>
<include 是我的toolbar,大家可以不用添加直接添加下面的ListView布局即可>
ListView的Item布局tem_vehicle_information_detail
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:gravity="center_vertical" > <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:src="@drawable/tab_icon_home_my"/> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="vertical" > <TextView android:id="@+id/ll_tv_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:text="陕AF666" /> <TextView android:id="@+id/ll_tv_color" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:layout_marginTop="10dp" android:text="黑色" /> <TextView android:id="@+id/ll_tv_version" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:layout_marginTop="10dp" android:text="奥迪A18" /> </LinearLayout> <ImageView android:id="@+id/iv_vehicle_information" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/tab_icon_my_back" android:layout_marginRight="10dp" /> </LinearLayout>
<里面的图片大家自己用自己替换即可>
03.构建Adapter:老生常谈了已经
package com.wxkj.ycw.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.wxkj.ycw.R; import com.wxkj.ycw.domain.Vehicle; import java.util.List; /** * Created by Okamiy on 2017/4/15. */ public class DeleteAdapter extends BaseAdapter{ private List<Vehicle> datas;//数据集合 private LayoutInflater layoutinflater;//视图容器,用来导入布局 static class ViewHolder { private ImageView phone; private ImageView back; private TextView num; private TextView color; private TextView version; } /* * 实例化Adapter */ public DeleteAdapter(Context context, List<Vehicle> dataSet) { this.datas = dataSet; this.layoutinflater = LayoutInflater.from(context); } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.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) { holder= new ViewHolder(); //获取listitem布局文件 convertView = layoutinflater.inflate(R.layout.item_vehicle_information_detail, null); //获取控件对象 holder.phone = (ImageView) convertView.findViewById(R.id.imageView2); holder.back = (ImageView) convertView.findViewById(R.id.iv_vehicle_information); holder.num = (TextView) convertView.findViewById(R.id.ll_tv_num); holder.color = (TextView) convertView.findViewById(R.id.ll_tv_color); holder.version = (TextView) convertView.findViewById(R.id.ll_tv_version); convertView.setTag(holder); } holder = (ViewHolder) convertView.getTag(); Vehicle vehicle = datas.get(position); //设置图片和文字 holder.num.setText(vehicle.getMsg().getNum()); holder.color.setText(vehicle.getMsg().getColor()); holder.version.setText(vehicle.getMsg().getVersion()); holder.phone.setImageResource(R.drawable.tab_icon_home_my); holder.back.setImageResource(R.drawable.tab_icon_my_back); return convertView; } }
04.适配Adapter,关键点时listView的监听事件(开源魅力):
private SwipeMenuListView lvMsg; List<Vehicle> msg = new ArrayList<>(); private DeleteAdapter adapter; @Override protected void initDatas() { Msg msg1 = new Msg("陕A666", "黑色", "奥迪A6"); Msg msg2 = new Msg("陕A888", "黑色", "奥迪A11"); Msg msg3 = new Msg("陕A999", "黑色", "奥迪A0"); Msg msg4 = new Msg("陕A0000", "黑色", "奥迪A9"); Vehicle vh01 = new Vehicle(R.drawable.tab_icon_home_my, msg1, R.drawable.tab_icon_my_back); Vehicle vh02 = new Vehicle(R.drawable.tab_icon_home_my, msg2, R.drawable.tab_icon_my_back); Vehicle vh03 = new Vehicle(R.drawable.tab_icon_home_my, msg3, R.drawable.tab_icon_my_back); Vehicle vh04 = new Vehicle(R.drawable.tab_icon_home_my, msg4, R.drawable.tab_icon_my_back); msg.add(vh01); msg.add(vh02); msg.add(vh03); msg.add(vh04); } @Override protected void initViews() { lvMsg = (SwipeMenuListView) findViewById(R.id.lv_modification_vehicle_information); adapter = new DeleteAdapter(this, msg); lvMsg.setAdapter(adapter); SwipeMenuCreator creator = new SwipeMenuCreator() { @Override public void create(SwipeMenu menu) { // create "open" 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 item title deleteItem.setTitle("删除"); // set item title fontsize deleteItem.setTitleSize(18); // set item title font color deleteItem.setTitleColor(Color.WHITE); // add to menu menu.addMenuItem(deleteItem); } }; // set creator lvMsg.setMenuCreator(creator); // step 2. listener item click event lvMsg.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { Vehicle device = msg.get(position); delete(device); msg.remove(position); adapter.notifyDataSetChanged(); return false; } }); // set SwipeListener lvMsg.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() { @Override public void onSwipeStart(int position) { // swipe start } @Override public void onSwipeEnd(int position) { // swipe end } }); } private int dp2px(int dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); } /** * 删除 * * @param item */ private void delete(Vehicle item) { }
3.感谢Android开源无私的贡献者:参考 Github地址