自定义一个swipe to delete listview

时间:2022-07-04 21:59:33

也就是侧滑删除的listview

第一部分 item的布局

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="none"
>
<LinearLayout

android:layout_width="wrap_content"
android:layout_height="match_parent"

android:orientation="horizontal">
<LinearLayout


android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/coll_background"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:ellipsize="end"
/>
<TextView
android:id="@+id/content"
android:maxLines="4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:ellipsize="end"
/>
</LinearLayout>


</LinearLayout>

<TextView
android:id="@+id/delete"
android:maxLines="4"
android:gravity="center"
android:layout_width="70dp"
android:layout_height="match_parent"
android:text="删除"
android:textSize="26sp"

android:textColor="#ccffff"
android:background="@drawable/delete_background"

/>
</LinearLayout>
</HorizontalScrollView>

第二部分,可能是因为内部嵌套了 scrollview的原因,外面的listview取不到焦点,事件拦截比较麻烦,因为即使是事件拦截,最终还要判断点在子view的什么位置,如果是点在删除按钮,那么才删除,否则进入该项,要判断子view的位置还要判断scrollview的滚动位置~~~~~~~~~~~~~

所以,简单的办法是将焦点直接给子view 在adapter 的 getview里,将每一个view settag,然后点击的时候就会得到是哪一项

之后adaper.notifydatasetchanged

删除的时候,adpter取得数据的list与 view 的tag就不会是一一对应的关系了,所以,要将list复制一份,如此就可以保持对应关系


public class collect extends Activity{
PullToRefreshListView pullToRefreshListView;
MyOpenHelper myOpenHelper;
SQLiteDatabase sqLiteDatabase;
DisplayMetrics displayMetrics;
int position=0;
ArrayList<dataitem>aList;
ArrayList<dataitem>copy;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
displayMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
setContentView(R.layout.collect_layout);

pullToRefreshListView=(PullToRefreshListView)findViewById(R.id.pull_refresh_list);
pullToRefreshListView.setMode(Mode.DISABLED);
aList=getdata();
copy=aList;
pullToRefreshListView.setAdapter(new collect.jinri_adapter(this,aList));


}

ArrayList<dataitem> getdata(){
ArrayList<dataitem>arrayList=new ArrayList<dataitem>();
myOpenHelper=new MyOpenHelper(collect.this, database.datebasenameString, null, 1);
sqLiteDatabase=myOpenHelper.getWritableDatabase();
dataitem dataitem;
Cursor c = sqLiteDatabase.query(database.table, null, null, null, null, null, null);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){

dataitem=new dataitem();
dataitem.setLink(c.getString(c.getColumnIndex("website")));
dataitem.setTitle(c.getString(c.getColumnIndex("title")));
dataitem.setDiscrip(c.getString(c.getColumnIndex("discrip")));
dataitem.setDate(c.getString(c.getColumnIndex("date")));
arrayList.add(dataitem);
}


sqLiteDatabase.close();
c.close();
return arrayList;
}

public class jinri_adapter extends BaseAdapter{

public Context context;
public ArrayList<dataitem> arrayList;
LayoutInflater layoutInflater;
Typeface face;
public jinri_adapter(Context context,ArrayList<dataitem> arrayList) {
// TODO Auto-generated constructor stub
this.context=context;
this.arrayList=arrayList;
layoutInflater=LayoutInflater.from(context);
face = Typeface.createFromAsset (context.getAssets() , "fonts/lanting.TTF" );
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if (arrayList!=null) {
return arrayList.size();
}else {
return 0;
}

}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
//TextView textView;
//TextView textView1;
// TextView delete;
@SuppressLint({ "ViewHolder", "InflateParams" })
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
position=arg0;
arg1=layoutInflater.inflate(R.layout.collect_item, null);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(displayMetrics.widthPixels, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView textView=(TextView)arg1.findViewById(R.id.title);
textView.setLayoutParams(layoutParams);
final TextViewtextView1=(TextView)arg1.findViewById(R.id.content);
textView1.setLayoutParams(layoutParams);
final TextViewdelete=(TextView)arg1.findViewById(R.id.delete);
if (arrayList!=null) {
if (arrayList.size()!=0) {
//textView.setText(arrayList.get(arg0*3));
//textView1.setText(arrayList.get(arg0*3+1));
textView.setText(arrayList.get(arg0).getTitle());

textView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), position+"position", 1000).show();
Intent intent=new Intent(collect.this, pageactivity.class);
//原有的alist改变了,对应关系也改变了,所以要用一个没改变的集合
intent.putExtra("web", copy.get((Integer)(textView.getTag())).getLink());
intent.putExtra("data", copy.get((Integer)(textView.getTag())));
startActivity(intent);
}
});


textView.setTextColor(Color.BLUE);
textView.setTag(Integer.valueOf(position));
textView1.setText(arrayList.get(arg0).getDiscrip());

textView1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), position+"position", 1000).show();
Intent intent=new Intent(collect.this, pageactivity.class);
//原有的alist改变了,对应关系也改变了,所以要用一个没改变的集合
intent.putExtra("web", copy.get((Integer)(textView1.getTag())).getLink());
intent.putExtra("data", copy.get((Integer)(textView1.getTag())));
startActivity(intent);
}
});


textView1.setTypeface(face);
textView1.setTag(Integer.valueOf(position));
delete.setTag(Integer.valueOf(position));

delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyOpenHelper myOpenHelper=new MyOpenHelper(collect.this, database.datebasenameString, null, 1);;
SQLiteDatabase sqLiteDatabase=myOpenHelper.getWritableDatabase();
//Toast.makeText(getApplicationContext(), ((Integer)(delete.getTag())).intValue()+"", 1000).show();
//sqLiteDatabase.delete("ftchineserssgreatz", "_id=?", new String[]{String.valueOf((((Integer)(delete.getTag())).intValue()+1))});
sqLiteDatabase.delete(database.table, "website=?", new String[]{aList.get((Integer)delete.getTag()).getLink()});
sqLiteDatabase.close();

aList.remove(((Integer)(delete.getTag())).intValue());
collect.jinri_adapter.this.notifyDataSetChanged();
pullToRefreshListView.onRefreshComplete();
}
});

}
}
return arg1;
}

}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
//sqLiteDatabase.close();
super.onBackPressed();
}
}