我有一个listview并为它设置BaseAdapter,行布局只有textview。
我通过查看对象跟踪以前的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);
}
}
答案 0 :(得分:0)
如何知道之前选择了哪个子视图。
将两个字段变量添加到Adapter类中,将其称为public int previousChoice
和currentChoice
。在onItemClick()
或onListItemClick()
方法中,使用第三个参数position
更新此值。
mAdapter.previousChoice = mAdapter.currentChoice;
mAdapter.currentChoice = position;
答案 1 :(得分:0)
//获取上一个视图,具体取决于位置,....方法(视图,位置)
final int wantedPosition = previousposition;
final int firstPosition = listview.getFirstVisiblePosition()
- listview.getHeaderViewsCount();
final int wantedChild = wantedPosition - firstPosition;
if (!(wantedChild > firstPosition && wantedPosition < listview
.getLastVisiblePosition())) {
//如果您只将文本视图作为行视图而不是
pv = (TextView) listview.getChildAt(wantedChild);
if (pv == null) {
pv = (TextView) previousview;
}
}
if (previousposition != position) {
// perform the operation on the pv view or current v view
}
}
previousview = v;
previousposition = position;