有一个重大的bug,喜欢大牛能够帮忙解决一下
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
/*
* Arrays.asList返回的List的类型是 Arrays.asList,不支持下面的方法。
* 得到list<String> 不支持remove操作
*
* 报错:*/
@SuppressLint("ShowToast")
public class MainActivity extends Activity{
ScaleAnimation leftAnimation;
ScaleAnimation rightAnimation;
ScaleAnimation itemDelAnimation;
private String[] names ={"张飞","关羽","刘备","曹操","赵云","智多星","曹冲","诸葛亮","哈士奇","詹姆斯","科比"};
private DelAdapter delAdapter;
private ListView listView;
float downX =0;
float lastMoveX = 0;
float moveX=0;
private List<String> list = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)this.findViewById(R.id.listview);
// list = Arrays.asList(names);
for (int i = 0; i < names.length; i++) {
list.add(names[i]);
}
delAdapter=new DelAdapter();
delAdapter.setList(list);
listView.setAdapter(delAdapter);
}
private void leftAnim(ViewHolder holder){
leftAnimation = new ScaleAnimation(0.0f, 1.0f, 1.0f, 1.0f
,Animation.RELATIVE_TO_SELF ,1.0f,Animation.RELATIVE_TO_SELF ,1.0f);
leftAnimation.setDuration(300);
leftAnimation.setFillAfter(true);
holder.button.setAnimation(leftAnimation);
}
@SuppressWarnings("unused")
private void rightAnim(ViewHolder holder){
rightAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 1.0f
,Animation.RELATIVE_TO_SELF ,1.0f,Animation.RELATIVE_TO_SELF ,1.0f);
rightAnimation.setDuration(300);
rightAnimation.setFillAfter(true);
holder.button.setAnimation(rightAnimation);
}
/**
* item删除后动画*/
@SuppressWarnings("unused")
private void itemDelAnim(ViewHolder holder){
itemDelAnimation = new ScaleAnimation(1.0f, 1.0f, 1.0f, 0.0f
,Animation.RELATIVE_TO_SELF ,0.0f,Animation.RELATIVE_TO_SELF ,0.0f);
itemDelAnimation.setDuration(300);
itemDelAnimation.setFillAfter(true);
// holder.rLayout.setAnimation(itemDelAnimation);
}
@SuppressWarnings("unused")
private class DelAdapter extends BaseAdapter{
private List<String> nameList;
public DelAdapter(){
nameList=new ArrayList<String>();
}
public void setList(List<String> nameList){
this.nameList=nameList;
}
public void refreshData(){
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return nameList.size();
}
@Override
public Object getItem(int position) {
return nameList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView==null) {
holder=new ViewHolder();
convertView=(View)LayoutInflater.from(MainActivity.this).inflate(R.layout.itemlayout,null);
holder.textView=(TextView)convertView.findViewById(R.id.text);
holder.button=(Button)convertView.findViewById(R.id.button);
convertView.setTag(holder);
}else {
holder=(ViewHolder)convertView.getTag();
}
final String name = nameList.get(position);
holder.button.setTag(position+"");
holder.textView.setText(name);
// holder.textView.setTag(position+"");
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nameList.remove(position);
listView.setTag(null);
holder.button.clearAnimation();
holder.button.setVisibility(View.GONE);
refreshData();
}
});
// holder.textView =(TextView)convertView.findViewWithTag(position+"");
holder.textView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// if (holder.textView.getTag()!=null && holder.textView.getTag().equals(""+position)) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX=event.getX();
break;
case MotionEvent.ACTION_MOVE:
lastMoveX=event.getX();
moveX=lastMoveX-downX;//移动的距离(可正可负)
break;
case MotionEvent.ACTION_UP:
if (moveX>10) {//向右滑动,隐藏删除按钮
if ( holder.button.getTag()!=null && holder.button.getTag().equals(position+"")) {
if (holder.button.getVisibility()==View.VISIBLE && listView.getTag()!=null) {
holder.button.clearAnimation();
holder.button.setVisibility(View.GONE);
rightAnim(holder);
listView.setTag(null);
}
}
}else if (moveX<-10) {//向左滑动,显示删除按钮
if ( holder.button.getTag()!=null && holder.button.getTag().equals(position+"")) {
if (holder.button.getVisibility()==View.GONE && listView.getTag()==null) {
holder.button.clearAnimation();
holder.button.setVisibility(View.VISIBLE);
listView.setTag(position);//标记控件已经有显示的删除按钮
leftAnim(holder);
}
}
}else{
if ((listView.getTag()==null)) {
holder.button.clearAnimation();
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("name", name);
startActivity(intent);
}
}
break;
case MotionEvent.ACTION_CANCEL:
Log.i("----------cancel--------", "滚动结束了");//滚动结束会调用此方法
break;
default:
break;
}
// }
return true;
}
});
return convertView;
}
}
class ViewHolder{
TextView textView;
Button button;
}
}