Separate Item clickable in android Layout

Asked

Viewed 273 times

2

I have a Listview, and a layout of the Adapter item that follows its basic image

inserir a descrição da imagem aqui

Problem is that when I click on any area of the item, in the list, every component is "selected"

inserir a descrição da imagem aqui

I would like to get the Action 1, Action 2 and Description effect separately.

For example:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

XML layout:

...
<LinearLayout 
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/linearLayout"
    android:layout_marginLeft="3dp">

        <!--descrição-->
        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:id="@+id/txtDescricao"
                android:text="Descricao" />

        <!--este é o "separador" horizontal-->
        <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray"/>

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="100"
                android:orientation="horizontal">

            <!--Tentei colocar um Linear Layout "fora" e marcar clickable = true -->
            <LinearLayout android:layout_width="wrap_content"
                          android:layout_weight="50"
                          android:clickable="true"
                          android:layout_height="wrap_content">

                <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:id="@+id/txtAcao1"
                        android:text="Acao 1" />

            </LinearLayout>

            <!--este é o "separador" vertical entre ação 1 e ação 2-->
            <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:background="@android:color/darker_gray" />

            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:gravity="center"
                    android:id="@+id/txtAcao2"
                    android:text="Acao 2" />

        </LinearLayout>

</LinearLayout>

I also tried to add a setOnClickListener in Action 1 and Action2, but it didn’t work

Follows the class . java

//ViewHolder
    static class ViewHolderItem {
            public TextView txtAcao1;
            public TextView txtAcao2;
            public View txtDescricao;
        }

//Adapter
 class RoteiroAdapter extends BaseAdapter{

            private Context mContext = null;
            private List<ItemRoteiro> mListaRoteiro = null;

            public RoteiroAdapter(Context context, List<ItemRoteiro> lista){
                this.mContext = context;
                this.mListaRoteiro = lista;
            }

            @Override
            public int getCount() {
                return mListaRoteiro.size();
            }

            @Override
            public Object getItem(int position) {
                return mListaRoteiro.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

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

                ViewHolderItem viewHolder;

                if(convertView == null){

                    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                    convertView = inflater.inflate(R.layout.item_roteiro, parent, false);

                    viewHolder = new ViewHolderItem();
                    viewHolder.txtAcao1= (TextView) convertView.findViewById(R.id.txtAcao1);
                    viewHolder.txtAcao2= (TextView) convertView.findViewById(R.id.txtAcao2);
                    convertView.setTag(viewHolder);
                }
                else{
                    viewHolder = (ViewHolderItem) convertView.getTag();
                }

                ItemRoteiro item = mListaRoteiro.get(position);

                if(item != null) {

                    viewHolder.txtAcao1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    });
                }
                 return convertView;
            }
        }

Does anyone know how to get this effect??

Thank you!!

  • How do you treat it in Activity?

  • instance each layout(are 2 actions; therefore 2 different layouts) and gives clickListener in layout.

  • @Reginaldorigo edited and posted the Activity code

  • @Mr_anderson, thanks for your help, I’ll test here and let you know the result..

  • @Mr_anderson didn’t work either.. I’ve done many tests I haven’t had any success...

  • Strange, because if it’s a custom list, then it should work, just like a cardview that contains an image with some buttons inside...

Show 1 more comment

1 answer

2


Give a ID pro to your Linearlayout and use the Java click on it.

Example:

XML

<LinearLayout 
     android:id="@+id/acao1"
     android:layout_width="wrap_content"
     android:layout_weight="50"
     android:layout_height="wrap_content">

JAVA

LinearLayout acao1 = (LinearLayout) findViewById(R.id.acao1);
acao1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

On all items in XML insert:

 android:clickable="false"

And the layout where you want to have the effect of "click"

 android:clickable="true"
 android:background="?android:attr/selectableItemBackground"
  • Thanks @Leonardo Dias for the help, I tested here, what happens now, is that when I press Acao1 or Acao2 nothing happens on the screen (onClickListener is called normally), but press the description every item is marked. But it was a breakthrough once..

  • Try to put the android:clickable="false" in the Textview of the description

  • I tried to put the android:duplicateParentState="true", that I found, but it’s not working. I’m running tests...

  • 1

    Thanks @Leonardodias, just add the android:background="?android:attr/selectableItemBackground", in linearLayout and android:clickable=true in linearLayout and ran!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.