Android ListView 左右滑动显示删除


public class AddressAdapter extends BaseAdapter {


Context context;

JSONArray arry;

Button btn;

private float downX;  //点下时候获取的x坐标

private float upX;   //手指离开时候的x坐标

/**

*/

public AddressAdapter(Context context, JSONArray arry) {

this.context = context;

this.arry = arry;

}


/* (non-Javadoc)

* @see android.widget.Adapter#getCount()

*/

@Override

public int getCount() {

return arry.length();

}


/* (non-Javadoc)

* @see android.widget.Adapter#getItem(int)

*/

@Override

public Object getItem(int position) {

try {

return arry.getJSONObject(position);

} catch (JSONException e) {

e.printStackTrace();

}

return null;

}


/* (non-Javadoc)

* @see android.widget.Adapter#getItemId(int)

*/

@Override

public long getItemId(int position) {

return position;

}


/* (non-Javadoc)

* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)

*/

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

TextView txt1, txt2;

final Button delBtn;

// 获得布局文件more_info_my.xml,里面定义了TextView和ImageView两个组件

convertView = LayoutInflater.from(context).inflate(

R.layout.address_lv_item, null);

txt1 = (TextView) convertView.findViewById(R.id.txt1);

txt2 = (TextView) convertView.findViewById(R.id.txt2);

delBtn = (Button) convertView.findViewById(R.id.btn1);

try{

txt1.setText(arry.getJSONObject(position).getString("address_title"));

txt2.setText(arry.getJSONObject(position).getString("address_create_time"));

delBtn.setTag(arry.getJSONObject(position).getString("address_id"));

}catch(JSONException e){

e.printStackTrace();

}

// 返回的convertView对象将作为ListView的列表项

convertView.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

downX = event.getX();

upX = 0;

if (btn != null) {   

btn.setVisibility(View.GONE);  //影藏显示出来的button

}

break;

case MotionEvent.ACTION_UP:

upX = event.getX();

if (btn != null) {   

btn.setVisibility(View.GONE);  //影藏显示出来的button

}

break;

}

if(delBtn != null){

if(upX == 0.0){

return true;

}

if(Math.abs(downX - upX) > 35){

delBtn.setVisibility(View.VISIBLE);

btn = delBtn;

return true;

}

return false;

}

return false;

}

});

delBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(context, v.getTag().toString(), Toast.LENGTH_SHORT).show();

arry = RemoveJSONArray(arry,position);

notifyDataSetChanged();

}

});

return convertView;

}

/**

* 删除JSONArry内的JSONObject

* @param jarray

* @param pos

* @return

*/

public static JSONArray RemoveJSONArray( JSONArray jarray,int pos) {

JSONArray Njarray=new JSONArray();

try{

for(int i=0;i<jarray.length();i++){  

if(i!=pos)

Njarray.put(jarray.get(i));  

}

}catch (Exception e){

e.printStackTrace();

}

return Njarray;

}


}


转载于:https://my.oschina.net/u/2293530/blog/417662

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值