简体   繁体   English

onListItemClick不会(始终)被ListActivity调用

[英]onListItemClick does not (always) gets called with ListActivity

I have used a ListActivity to display a view, inflated from a xml layout file. 我已经使用ListActivity来显示从xml布局文件放大的视图。 When I initially display this view in the list, I hide a part of the view by setting the visible property of the target view to View.GONE. 最初在列表中显示此视图时,我通过将目标视图的visible属性设置为View.GONE来隐藏视图的一部分。 In the method onListItemClick, I set the visibility of this 'hidden' view to View.GONE, if it is visible. 在方法onListItemClick中,我将此“隐藏”视图的可见性设置为View.GONE(如果可见)。 On the next click, I want to hide the view again. 下次单击时,我要再次隐藏视图。 To do this, I detect the visibility of the view, and change it to View.GONE if it is View.VISIBLE. 为此,我检测视图的可见性,如果它是View.VISIBLE,则将其更改为View.GONE。

My code executes method onListItemClick, when the hidden view's visibility os View.GONE and correctly displays the hidden view on click. 当隐藏视图的可见性为View.GONE时,我的代码执行onListItemClick方法,并在单击时正确显示隐藏视图。 However, it does not executes the onListItemClick method, when the visiblity of the view is set to View.VISIBLE. 但是,当视图的可见性设置为View.VISIBLE时,它不会执行onListItemClick方法。

I understand that this situation is a good candidate for usage of ExpandableListActivity, but I am unable to use it due to other unresolvable issue! 我知道这种情况适合使用ExpandableListActivity,但由于其他无法解决的问题,我无法使用它!

Here's the (partial relevant) code: 这是(部分相关的)代码:

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
}

Thanks. 谢谢。

cheers 干杯

但以防万一,您必须将ListView的图像和其他项目设置为不可focusableonListItemClick将调用onListItemClick方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM