0

I have a listview, and have this item style

enter image description here

when I click on any part of it is all selected

enter image description here

but I would like to get the separate effect of Description and Action 1 and Action 2

this style:

enter image description here

enter image description here

My XML item

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

        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:id="@+id/txtDescription"
                android:text="description" />

        <!--this is one separator 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="50dp"
                android:weightSum="100"
                android:orientation="horizontal">

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

                <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:id="@+id/txtAction1"
                        android:text="Action 1" />

            </LinearLayout>

            <!--this is one separator vertical-->
            <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:background="@android:color/darker_gray" />


            <LinearLayout android:layout_width="wrap_content"
                          android:id="@+id/test2"
                          android:layout_height="match_parent">

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

            </LinearLayout>
        </LinearLayout>
</LinearLayout>
...

My class Java

//ViewHolder
static class ViewHolderItem {
    public LinearLayout test1;
    public LinearLayout test2;
}


//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 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.test1= (LinearLayout) convertView.findViewById(R.id.txtTest1);
            viewHolder.test2= (LinearLayout) convertView.findViewById(R.id.txtTest2);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolderItem) convertView.getTag();
        }

        ItemRoteiro item = mListaRoteiro.get(position);

        if(item != null) {

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

                }
            });

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

                }
            });
        }

        return convertView;
    }
}

How to solve this problem? Thanks!!

Marco Giovanni
  • 297
  • 7
  • 18
  • I've tried using the **android:clickable = "false"**, but did not succeed, I think I used the wrong way. – Marco Giovanni Sep 22 '16 at 18:41
  • 1
    Maybe there is a solution here, but I can't think of any. Is there any reason this layout needs to be in a ListView instead of a ScrollView? If it were in a ScrollView, I think this becomes a much easier problem to solve. – joebro Sep 22 '16 at 18:46
  • Thanks @joebro, I looked this [link](http://stackoverflow.com/questions/6763876/onclick-not-triggered-on-linearlayout-with-child) but it did not work for me. I thought of using the listview by the fact that the information will be loaded into the database, it will not be an amount "fixed" – Marco Giovanni Sep 22 '16 at 18:52
  • 1
    I don't know a whole lot about the specifics here, but it would seem doable by creating layouts on the fly and adding them programmatically to a ScrollView. Some very basic pseudo code, but something like this: `RelativeLayout relativeLayout = new RelativeLayout(this); //add action1/2 buttons and description here RelativeLayout scrollViewContainer = findViewById(R.id.scrollViewContainer); //get reference to the direct and only child of the scrollview //append layout with buttons and description to scrollViewContainer` – joebro Sep 22 '16 at 18:56
  • Thanks @joebro I will do this test. – Marco Giovanni Sep 22 '16 at 19:03
  • A ListView is so much better than a scroll view with dynamically added components. It scales so much better with lots of rows. That is the wrong direction IMO. – Gary Bak Sep 22 '16 at 19:20
  • possible duplicate of - http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons – karthik prasad Sep 22 '16 at 19:37
  • @karthikprasad is not duplicated, because this link refers to how to get the button1 and button2 event within the same layout, I get that, but it is all selected listview element – Marco Giovanni Sep 22 '16 at 19:53

1 Answers1

1

Try adding this to your adapters item layout.

android:descendantFocusability="blocksDescendants"

Update

Since I didn't notice that the onClickListener was on the layout instead of the TextView, you can either add the click listener to the TextViews or apply this attribute to the LinearLayouts

android:clickable="true" 

And this to all the TextViews within the layouts:

android:clickable="false"
Gary Bak
  • 4,746
  • 4
  • 22
  • 39
  • Thanks @GaryBak for your answer..but it did not work :( – Marco Giovanni Sep 22 '16 at 19:29
  • Do you have an onItemClickListener defined on the listview? If so, remove it. – Gary Bak Sep 22 '16 at 19:31
  • There were OnClickListener in LinearLayout, removed, but still did not work :( – Marco Giovanni Sep 22 '16 at 19:42
  • Thanks @GaryBak, I tried to add `android: clickable` I let `= true` only in the listener layout... If I press the "action 1" or "action 2" nothing happens if I remove the listener test1 and test2 back fill every item.. – Marco Giovanni Sep 22 '16 at 20:09
  • Thank you very much!! It worked, it was just enough to add in LinearLayout `android:background="?android:attr/selectableItemBackground"` that worked.., lost almost a day with this ..... Thankssssss!!! – Marco Giovanni Sep 22 '16 at 20:21