主要是使用了对ListView的长按和点击,长按的时候显示确认视图,在里面有删除和取消选项。
效果:
长按的时候,显示删除和取消界面,点击删除就删除该项,取消就隐藏显示的删除和取消视图。
显示了删除和取消视图,点击其他选项就隐藏显示的视图。
活动:
public class MainActivity extends Activity {
private MainActivity mContext;
private int lastPress = 0;
private boolean delState = false;
private List<String> curList = new ArrayList<>();
private ListView curListView;
private CurAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mContext = this;
String[] strings = {"aa", "bb", "cc", "dd", "ee"};
for (String s : strings) {
curList.add(s);
}
curListView = (ListView) findViewById(R.id.lv_contents);
adapter = new CurAdapter();
curListView.setAdapter(adapter);
curListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (delState) {
if (lastPress < parent.getCount()) {
View delview = parent.getChildAt(lastPress).findViewById(R.id.linear_del);
if (null != delview) {
delview.setVisibility(View.GONE);
}
}
delState = false;
return;
} else {
Log.d("click:", position + "");
}
// lastPress = position;
}
});
curListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
private View delview;
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
if (lastPress < parent.getCount()) {
delview = parent.getChildAt(lastPress).findViewById(R.id.linear_del);
if (null != delview) {
delview.setVisibility(View.GONE);
}
}
delview = view.findViewById(R.id.linear_del);
delview.setVisibility(View.VISIBLE);
delview.findViewById(R.id.tv_del).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delview.setVisibility(View.GONE);
curList.remove(position);
adapter.notifyDataSetChanged();
}
});
delview.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delview.setVisibility(View.GONE);
}
});
lastPress = position;
delState = true;
return true;
}
});
}
private class CurAdapter extends BaseAdapter {
@Override
public int getCount() {
return curList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_content_delete, null);
}
((TextView) convertView.findViewById(R.id.tv_content)).setText(curList.get(position));
return convertView;
}
}
}
Listivw行布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_height="40dp">
<RelativeLayout
android:id="@+id/rela_content"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_height="40dp">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="content"
android:textColor="#001122"
android:gravity="center" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linear_del"
android:layout_width="match_parent"
android:layout_height="40dp"
android:visibility="gone"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_del"
android:layout_width="0dp"
android:layout_weight="1"
android:text="删除"
android:textColor="#000000"
android:background="#00ff00"
android:gravity="center"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_cancel"
android:layout_width="0dp"
android:text="取消"
android:textColor="#ffffff"
android:background="#0000ff"
android:gravity="center"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
显示效果: