Nice programing

imagebutton으로 listview 행을 클릭 할 수 없습니다.

nicepro 2020. 12. 27. 20:46
반응형

imagebutton으로 listview 행을 클릭 할 수 없습니다.


listview에 문제가 있습니다. 항목 (행)에는 이미지 버튼이 있습니다. imagebutton에는 "android : onClick"이 있으므로이 onclick 이벤트는 작동하지만 행 클릭은 작동하지 않습니다. 행 항목에서 imagebutton을 제거하면 행 작동을 클릭하십시오 (listview에는 올바른 onclick listner가 있음). 어떻게 고칠 수 있습니까? 사용자가 이미지 버튼을 클릭하고 표준 클릭 이벤트를 클릭 할 때 onclick 이벤트가 필요합니다. 사용자가 행을 선택할 때 (이미지 버튼을 클릭하지 않고 행을 클릭합니다)

내 목록보기 :

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/restaurants_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@color/list_devider"
        android:dividerHeight="1dp"
        android:cacheColorHint="@color/list_background" /> 

운수 나쁘게,

android:focusable="false"
android:focusableInTouchMode="false"

에서 작동하지 않습니다 ImageButton.

마침내 여기 에서 해결책을 찾았습니다 . 해당 항목에 대한 레이아웃 xml에서

android:descendantFocusability="blocksDescendants" 

루트 뷰로.

s ListView있는 경우에 완벽하게 작동합니다 ImageButton. 따르면 공식 기준 , blocksDescendants(가)을 의미 ViewGroup포커스를 수신하는 자손을 차단한다.


listView에 사용자 지정 어댑터를 사용할 수 있습니다 (아직 사용하지 않은 경우). 그리고 getView(int position, View inView, ViewGroup parent)어댑터 방법에서 다음과 같이하십시오.

@Override
public View getView(int position, View inView, ViewGroup parent) {

    View v = inView;
    ViewHolder viewHolder; //Use a viewholder for sufficent use of the listview

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) adaptersContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.image = (ImageView) v.findViewById(R.id.ImageView);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

        .....

    viewHolder.image.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            //Click on imageView
        }i
    });

    v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            //Click on listView row
        }
    });

        .....

    return (v);
}

사용자 지정 어댑터를 만드는 데 도움이 필요한 경우 여기를 참조 하십시오 .


listView의 행에 Button, Image..etc .. 와 같은 클릭 가능한 요소가 있으면 onItemClick작동하지 않습니다. 따라서 getView목록 어댑터 에 클릭 리스너를 작성해야합니다 .

자세한 내용은 이것을 읽으십시오 .


버튼에 대해 다음 속성을 설정합니다.

      android:focusable="false"
      android:focusableInTouchMode="false"

또는 어댑터 클래스에서 동적으로 설정할 수 있습니다.

        yourButton.setFocusable(false);
    yourButton.setFocusableInTouchMode(false);

And make sure that you set the choice mode as single for the listview:

       listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

In my case, android:descendantFocusability="blocksDescendants" for main layer did not work, neither in the ListView. I also tried android:focusable="false" android:focusableInTouchMode="false" which I heard that it is working for Buttons, but I had ImageButton so it didn't.

But setting the properties of the button in the CS file of the Layout worked.

var imageButton = view.FindViewById<ImageButton>(Resource.Id.imageButton1);
imageButton.Focusable = false;
imageButton.FocusableInTouchMode = false;
imageButton.Clickable = true;

If a row has multiple clickable elements, onItemClick() will not work. You will need to set the OnClickListener in the getView() method. Store the listeners the the View's tag so that they can be recycled, add methods to your listeners so they can be specialized for different rows.

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        RowClickListeners listeners = (RowClickListeners) view.getTag();
        if (listeners == null) {
            listeners = new RowClickListeners();
        }

        // Row click listener:
        RowClickListener onClickListener = listeners.rowClickListener;
        if (onClickListener == null) {
            onClickListener = new RowClickListener();
            listeners.rowClickListener = onClickListener;
        }
                    onClickListener.setToPosition(pos);
        view.setOnClickListener(onClickListener);

        // Overflow listener:
        View btn = view.findViewById(R.id.ic_row_btn);
        ButtonListener btnListener = listeners.buttonClickListener;
        if (rowListener == null) {
            btnListener = new ButtonListener(activity);
            listeners.rowClickListener = btnListener;
        }
                    btnListener.setToPosition(pos);
        btnListener.setCollection(collectionId);
        btn.setOnClickListener(btnListener);
    }


    public static class RowClickListeners {
        public RowClickListener rowClickListener;
        public ButtonListener buttonClickListener;
    }

no single answer above worked for me, but a combination did.

I now set android:descendantFocusability="blocksDescendants" on the ListView and android:focusable="false" android:focusableInTouchMode="false" on the ImageButtons in the XML AND in Java I also set descendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS) on the ListView and focusable(false), focusableInTouchMode(false), clickable(true) on the ImageButtons.

ReferenceURL : https://stackoverflow.com/questions/11428303/cant-click-on-listview-row-with-imagebutton

반응형