0

In my listview's item, there is a two components.

<TextView
        android:id="@+id/owner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="방장"
        android:layout_centerInParent="true"
        android:textColor="@color/colorBlack"
        android:textSize="10dp"/>

    <ImageButton
        android:id="@+id/kickOutBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:visibility="invisible"
        android:scaleType="fitCenter"
        android:background="@color/colorTransparent"
        android:tint="@color/colorBlack"
        android:src="@drawable/ic_baseline_close_24"/>

These are in a same location in a view.

I mean, the Textview is located in the center of the ImageButton.

And if the ListView item's target is a manager, I want to make its list item's ImageButton invisible and open the Textview that says manager, and if not, I want to make its list item's ImageButton show the opposite, and I want to make the Textview that says manager invisible. (default is that imagebutton is invisible and textview is visible)

To do that, I tried to this code.

// getView in ListAdapter

@Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        View view;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.navi_list_menu_item, null);
        }
        else {
            view = convertView;
        }

        TextView supervisor_tv = (TextView) view.findViewById(R.id.owner);
        ImageButton kick_btn = (ImageButton) view.findViewById(R.id.kickOutBtn);

        if(!mNavItems.get(i).fb_uid.equals(roomSupervisor)) {
            kick_btn.setVisibility(View.VISIBLE);
            kick_btn.setOnClickListener(v -> {
                Log.d("ChatRoomActivity", "click imagebutton");
            });
            supervisor_tv.setVisibility(View.INVISIBLE);
        }

        return view;
    }

// Activity

mDrawerList = (ListView) findViewById(R.id.nav_list);
mDrawerList.addHeaderView(listHeaderView);
NaviDrawerListAdapter adapter = new NaviDrawerListAdapter(this, mNavItems, roomSupervisor);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener((adapterView, view, position, id) -> {
    Log.d(TAG, String.valueOf(position));
});

It is ok to click manager's list item and ImageButton of others.

But the problem is that I cannot click non-manager user's list item.

Could you tell me what is the problem of my code?

Jun
  • 451
  • 4
  • 16
  • https://stackoverflow.com/questions/11610023/click-is-not-working-on-the-listitem-listview-android Refer to this post, I tried to add focusable = "false" to the ImageButton, But it is not worked... – Jun Apr 08 '21 at 13:23

1 Answers1

0

By googling, I found the answer.

It is setFocusable and setFocusableInTouchMode

I revised my Adapter code like this.

if(!mNavItems.get(i).fb_uid.equals(roomSupervisor)) {
            kick_btn.setVisibility(View.VISIBLE);
            kick_btn.setOnClickListener(v -> {
                Log.d("ChatRoomActivity", "click a kick btn");
            });
            kick_btn.setFocusable(false);
            kick_btn.setFocusableInTouchMode(false);
            supervisor_tv.setVisibility(View.INVISIBLE);
        }

Then it works well!

Jun
  • 451
  • 4
  • 16