我正在创建一个包含很多项的ListView,ListView的每一行都包含一个TextView和一个“删除”按钮,当单击“删除”按钮(而不是单击该行)时,该行应显示toast,运行动画并从列表中删除但它只显示吐司(当我点击该行时效果很好)
这是我的代码
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String[] items = new String[20];
ListView lv;
MyAdapter mAdapter;
ArrayList<String> data = new ArrayList<String>();
private int position;
Animation ani;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 20; i++) {
items[i] = String.valueOf(i + 1);
data.add(items[i]);
}
lv = (ListView) findViewById(R.id.listView1);
mAdapter = new MyAdapter(getApplication(), R.layout.custom_row, data);
lv.setAdapter(mAdapter);
ani = AnimationUtils.loadAnimation(getApplicationContext(), R.animator.fadeout);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "You have clicked on item " + arg2, Toast.LENGTH_SHORT).show();
arg1.startAnimation(ani);
ani.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mAdapter.remove(arg2);
}
});
}
});
}
class MyAdapter extends ArrayAdapter<String> {
ArrayList<String> data;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(layout, parent, false);
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText(data.get(position));
Button bt = (Button) v.findViewById(R.id.button1);
bt.setOnClickListener(new MyClick(this, position));
return v;
}
class MyClick implements View.OnClickListener {
MyAdapter p;
int i;
public MyClick(MyAdapter parent, int pos) {
p = parent;
i = pos;
}
@Override
public void onClick(View v) {
lv.performItemClick(lv.getAdapter().getView(i, null, null), i, i);
}
}
public void remove(int pos) {
data.remove(pos);
notifyDataSetChanged();
}
int layout;
public MyAdapter(Context c, int l, ArrayList<String> data_name) {
super(c, l, data_name);
layout = l;
data = data_name;
}
}
}
提前感谢您的帮助。
答案 0 :(得分:0)
修改onClick
类中的MyClick
回调方法,如下所示:
@Override
public void onClick(View v) {
lv.performItemClick(lv.getChildAt(i), i, i);
}
由于p.getView(i, null, null)
会使item
中不属于ListView
的新视图曝光,因此您应使用lv.getChildAt(i)
获取实际item
。