我使用ListActivity来显示从xml布局文件中膨胀的视图。当我最初在列表中显示此视图时,我通过将目标视图的visible属性设置为View.GONE来隐藏视图的一部分。在onListItemClick方法中,我将此“隐藏”视图的可见性设置为View.GONE(如果可见)。在下一次单击时,我想再次隐藏视图。为此,我检测视图的可见性,如果它是View.VISIBLE,则将其更改为View.GONE。
当隐藏视图的可见性为View.GONE并且在点击时正确显示隐藏视图时,我的代码执行onListItemClick方法。但是,当视图的可见性设置为View.VISIBLE时,它不会执行onListItemClick方法。
我知道这种情况适合使用ExpandableListActivity,但由于其他无法解决的问题,我无法使用它!
这是(部分相关的)代码:
public class MyListActivity extends ListActivity implements OnClickListener {
// images that depict whether part of the view is visible or not
private Drawable imgUp, imgDown;
private class MyOrderAdapter extends ArrayAdapter<Order> {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// infalte this view from xml
v = vi.inflate(R.layout.brief_order, null);
// display down arrow key depicting that more is available to be
// displayed when a user clicks on it.
ImageView arrow = (ImageView) v.findViewById(R.id.imgArrow);
// get a reference to the table layout for details
// which should not be displayed initially
TableLayout tb = (TableLayout) v.findViewById(R.id.view_detail);
// DO NOT DISPLAY PART OF THE VIEW INITIALLY
tb.setVisibility(View.GONE);
}
// code to initialize the text filed values in the view v
return v;
}
// onListItemClick
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d("MyList", "item click called");
// RETRIEVE THE detail view
TableLayout tb = (TableLayout) v.findViewById(R.id.view_detail);
if(tb.getVisibility() == View.GONE) {
tb.setVisibility(View.VISIBLE);
img.setImageDrawable(imgUp);
}
else {
img.setImageDrawable(imgDown);
tb.setVisibility(View.GONE);
}
}
// rest of the code
}
感谢。
欢呼声
答案 0 :(得分:0)
但为了以防万一,您必须将ListView
中的图片和其他项目设置为focusable
,而不会调用onListItemClick
方法。