When I add a view to a ListView Item, this Item reacts no longer properly

南楼画角 提交于 2020-01-05 02:44:48

问题


I want,a new View appears in my ListView Item, if I click this Item like this, where (1) and (2) are clicks

and I can do it, but when I want to click again this Item, i.e. to hide that new View, then Itemclick reacts no longer in the way it should do.

I perform the add View Operation in my onItemClickListener right to the properly position

My code is the following

...
list_view.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i("TAG", "onItemClick:: BEGIN");

            switch (position) {
                case 0 : 
                    Log.i("TAG", "onItemClick:: before addTitle");                      
                    showFieldList(view);
                    Log.i("TAG", "onItemClick:: after  addTitle");
                break;              
                ...                 
            }

            Log.i("TAG", "onItemClick:: END");
        }
});
...

I tried to show the behaviour in my Log and after the first Click I can see my Log Entry "TAG" with the values

onItemClick:: BEGIN

onItemClick:: before addTitle

onItemClick:: after addTitle

onItemClick:: BEGIN

but the next clicks show nothing and as said, that Item doesn't react anymore but I can see instead my client_adapter LOG and just the entries of getGroupView Method are shown when I click on Item.

I just wanted to remark too, that it also behaves like this, if I attach the OnGroupExpand/OnGroupCollapse/OnGroupClick Listeners of ExpandableListView.

Sum up: I have a ListView and if I click the first Item, this one expand to show an ExpandableListView and if I'click again, this should be collapsed but this doesn't behave like this, so that the ExpandableListView assumes the control over entire Item.

Can anyone tell me, why the item loses control of clicks and this is won by the ExpandableListView FieldList? Thank you in advance!

Rest of code

    private View showFieldList(View view) {

        // Retrieving Item Layout. (= view Layout)
        RelativeLayout item_layout = (RelativeLayout) view.findViewById(R.id.relLayoutDrawer);

        // Retrieving "title" Element
        TextView title_tv = (TextView)view.findViewById(R.id.title);

        View row = View.inflate(context, R.layout.field_list, null);

        // Getting Field names
        Resources res = getResources();
        String fields = res.getString(R.string.fields);         

        client = (ExpandableListView)row.findViewById(R.id.field_expandable_list);

        client.setFocusable(false); //<< ADDED TO WORK  

        client_adapter = new ClientAdapter(context, fields);



        // When I comment this Line, `onItemClick` works
        // if not, then client_adapter assummes control

        client.setAdapter(client_adapter); 

        // Setting "Subtitle" under "title" aligning both left borders
        params.addRule(RelativeLayout.ALIGN_LEFT, title_tv.getId());
        params.addRule(RelativeLayout.BELOW, title_tv.getId());     
        item_ayout.addView(row, params);    
    }

Here my drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relLayoutDrawer"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@android:color/black">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/title"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/icon"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/metallic_silver"
        android:gravity="center_vertical"
        android:paddingRight="40dp"/>

    <TextView android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@layout/counter_bg"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"/>

</RelativeLayout>

回答1:


The problem exists because you have ExpandableListView nested in your ListView. When ExpandableListView is hidden, only ListView receives onClick events, but when it appears it consumes all the onClick events, therefore your ListView doesn't react on clicking anymore.

You can either follow this topic: ListView OnItemClickListener Not Responding?

Or just go for approach offered by momo in one of comments - using only ExpandableListView with just more levels of depth.




回答2:


Solved!

I properly wrote again onCollapsedGroupListener, onExpandedGroupListener and onClickedGroupListener and I added the next line to the last code

client = (ExpandableListView)row.findViewById(R.id.field_expandable_list);
...
client.setFocusable(false);


来源:https://stackoverflow.com/questions/26276264/when-i-add-a-view-to-a-listview-item-this-item-reacts-no-longer-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!