我有一个列表视图并为其设置 BaseAdapter,行布局只有文本视图。
我通过拥有视图对象来跟踪以前的 textview,现在当我滚动 listview 视图时,previoustextview obj 不起作用。
如何知道之前选择了哪个子视图。
public View getView(final int position, final View convertView, final ViewGroup parent) {
TextView txtview = (TextView) convertView;
boolean flag = false;
final Resources res = getResources();
final Drawable actbtnDoneDraw = res.getDrawable(R.drawable.action_done_button);
final Drawable actbtnSelectedDraw = res.getDrawable(R.drawable.action_button_selected);
final Drawable actbtnDraw = res.getDrawable(R.drawable.action_button);
if (null == convertView) {
if (DeviceDetail.isGalaxyTabOriginal()) {
txtview = (TextView) inflater.inflate(R.layout.list_txtview_gtab0, parent,
true);
} else {
txtview = (TextView) inflater.inflate(R.layout.list_txtview, parent, false);
}
} else {
txtview = (TextView) convertView;
if (selectedposition != position) {
txtview.setBackgroundDrawable(actbtnDraw);
txtview.setSelected(false);
} else {
txtview.setBackgroundDrawable(actbtnSelectedDraw);
txtview.setSelected(true);
}
}
final Activity activity = activities.get(position);
txtview.setText(activity.name);
txtview.setTag(Integer.valueOf(activity.id));
txtview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
selectedposition = position;
Log.i("Onclick position = " + position);
modifyButtonView(v, position);
}
});
}
public void modifyButtonView(final View view, final int position) {
final TextView v = (TextView) view;
final TextView pv = (TextView) previousview;
final Resources res = getResources();
final Drawable actbtnSelectedDraw = res.getDrawable(R.drawable.action_button_selected);
final Drawable actbtnDraw = res.getDrawable(R.drawable.action_button);
if (NoSelection) {
v.setSelected(true);
v.setBackgroundDrawable(actbtnSelectedDraw);
} else {
if (previousposition != position) {
v.setSelected(true);
pv.setBackgroundDrawable(actbtnDraw);
v.setBackgroundDrawable(actbtnSelectedDraw);
pv.setSelected(false);
}
}
previousview = v;
previousposition = position;
NoSelection = false;
mListener.onActivityButtonClicked(v);
}
}