您的位置:首页 > 移动开发 > Android开发

Android ListView 左右滑动显示删除

2015-05-21 00:00 399 查看
摘要: 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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐