所以我的listview完美运行然后我决定添加一个上下文菜单。每当我正常点击列表视图中的项目时,我就会立即执行该操作,第一次单击时整个列表都会反转。后续单击对订单不执行任何操作,但是当再次取消选择第一个项目时,列表将恢复正常。当我取出我添加的上下文菜单逻辑时,列表视图问题不会消失。
我附加了一个调试器,我的列表适配器中的元素永远不会重新排序,并且ListView本身永远不会设置为与.setStackFromBottom()
这是我的onClick侦听器,用于处理列表视图项的单击事件:
public void onClick(View v) {
ViewHolder holder = (ViewHolder) v.getTag();
CheckBox b = holder.box;
Boolean check = b.isChecked();
b.setChecked(!check);
if (!check) {
mChecked.add(holder.fu);
if (mChecked.size() == 1) {
buttonLayout.setVisibility(View.VISIBLE);
}
} else {
mChecked.remove(holder.fu);
if (mChecked.size() == 0) {
buttonLayout.setVisibility(View.GONE);
}
}
}
viewholder类只保存对我在listview中用于优化的对象的引用。我无法弄清楚为什么这会导致我的列表在显示时反转,我已经尝试将侦听器移动到布局中的另一个视图,我已经尝试重写了侦听器,似乎没有任何工作!任何建议将不胜感激。
编辑:这是视图持有者的代码
/** Class to provide a holder for ListViews. Used for optimization */
private class ViewHolder {
TextView date;
TextView gallons;
TextView cost;
TextView cpg;
TextView mpg;
CheckBox box;
FillUp fu;
}
以及适配器:
public class FillUpAdapter extends BaseAdapter {
ArrayList<FillUp> mElements;
ArrayList<FillUp> mChecked;
Context mContext;
public FillUpAdapter(Context c, ArrayList<FillUp> data) {
mContext = c;
mElements = data;
mChecked = new ArrayList<FillUp>();
}
public void clearChecked() {
mChecked.clear();
}
public ArrayList<FillUp> getChecked() {
return mChecked;
}
public boolean remove(FillUp f) {
mChecked.remove(f);
return mElements.remove(f);
}
@Override
public int getCount() {
return mElements.size();
}
@Override
public Object getItem(int arg0) {
return mElements.get(arg0);
}
@Override
public long getItemId(int arg0) {
return mElements.get(arg0).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout;
ViewHolder holder;
if (convertView != null) {
layout = (LinearLayout) convertView;
holder = (ViewHolder) layout.getTag();
} else {
layout = (LinearLayout) LayoutInflater.from(mContext).inflate(
R.layout.fillup_list_item, parent, false);
holder = new ViewHolder();
holder.cost = (TextView) layout
.findViewById(R.id.fillUpListTotalValue);
holder.cpg = (TextView) layout
.findViewById(R.id.fillUpListCostPerGal);
holder.gallons = (TextView) layout
.findViewById(R.id.fillUpListGalValue);
holder.mpg = (TextView) layout
.findViewById(R.id.fillUpMPGText);
holder.date = (TextView) layout
.findViewById(R.id.fillUpListDate);
holder.box = (CheckBox) layout
.findViewById(R.id.fillUpListCheckBox);
holder.fu = (FillUp) getItem(position);
layout.setTag(holder);
}
holder.date.setText(holder.fu.getDate());
holder.gallons.setText(holder.fu.getGallonsText());
holder.cpg.setText(holder.fu.getCostText());
holder.cost.setText(holder.fu.getTotalText());
holder.mpg.setText(String.format("%03.1f MPG",holder.fu.getMPG()));
if (convertView != null) {
holder.box.setChecked(mChecked.contains(holder.fu));
}
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder holder = (ViewHolder) v.getTag();
CheckBox b = holder.box;
Boolean check = b.isChecked();
b.setChecked(!check);
if (!check) {
mChecked.add(holder.fu);
if (mChecked.size() == 1) {
buttonLayout.setVisibility(View.VISIBLE);
}
} else {
mChecked.remove(holder.fu);
if (mChecked.size() == 0) {
buttonLayout.setVisibility(View.GONE);
}
}
}
});
return layout;
}
}
更新:
好的,所以我把它缩小到了buttonLayout视图的可见性变化,这是ListView下面Activity活动布局底部按钮的线性布局。每当我将该视图的可见性更改为View.VISIBLE
时(在检查第一个项目时发生),列表的顺序就会反转。当视图的可见性设置为View.GONE
我不知道会导致什么:(
答案 0 :(得分:1)
在缩小范围之后,我发现问题不在于改变我的按钮栏的可见性,而是在FillUp
holder.fu
ViewHolder
中传递了getItem(position)
个对象。 } .class。通过将其更改为引用适配器的{{1}}方法,一切似乎都有效。相当奇怪的错误,因为适配器本身没有更改元素的顺序,但是传递对象的引用使它非常不满。
答案 1 :(得分:-1)
如果您点击它时列表视图背景颜色发生了变化,我认为这与您的主题有关。只需使用listview的缓存颜色参数,这是一个例子:
<ListView
android:layout_width="fill_parent"
android:id="@android:id/list"
android:scrollingCache="true"
android:persistentDrawingCache="all"
android:cacheColorHint="#0000"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:stackFromBottom="true"
android:smoothScrollbar="true"
android:paddingTop="115dip">
</ListView>