我已經使用ListActivity來顯示一個視圖,從一個xml佈局文件膨脹。當我最初在列表中顯示這個視圖時,我通過將目標視圖的visible屬性設置爲View.GONE來隱藏視圖的一部分。在onListItemClick方法中,如果View.GONE可見,則將該'隱藏'視圖的可見性設置爲View.GONE。在下一次點擊時,我想再次隱藏視圖。爲此,我檢測視圖的可見性,並將其更改爲View.GONE(如果它是View.VISIBLE)。onListItemClick沒有(總是)被ListActivity調用
我的代碼執行方法onListItemClick,當隱藏視圖的可見性os View.GONE並正確顯示點擊隱藏視圖。但是,當視圖的可見性設置爲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
}
感謝。
歡呼