4

I'm noticing that by adding the HorizontalScrollView it's disabling my ability to select items on my ListView. This seems like a pretty basic/common feature to want to add (I want to be able to swipe my item to the left to offer a delete option) but HorizontalScrollView appears to be incompatible with items that you would like to also touch to select. Note that if I change HorizontalScrollView to a LinearLayout or otherwise the items become selectable.

I was suspecting that touch listeners may be ineffective due to the fact that the swiping capability overrides any other kind of touch, but I can't really think of why it doesn't work otherwise. Can anyone help to resolve this issue? I would like to be able to use HorizontalScrollView alongside a click compatibility (unless there is another way to achieve the same functionality).

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout4"
            android:layout_width="384dp"
            android:layout_height="110dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/task_view_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="17dp"
                android:layout_marginTop="28dp"
                android:fontFamily="@font/robotolight"
                android:text="@string/task_name"
                android:textColor="@android:color/black"
                android:textSize="24sp"
                app:layout_constraintStart_toEndOf="@+id/task_view_icon"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/task_view_icon"
                android:layout_width="77dp"
                android:layout_height="77dp"
                android:layout_marginStart="17dp"
                android:layout_marginTop="17dp"
                android:contentDescription="@+string/icon"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/main_page_icon_switch_x4" />

            <TextView
                android:id="@+id/task_view_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="50dp"
                android:layout_marginTop="36dp"
                android:fontFamily="@font/robotolight"
                android:text="@string/duration"
                android:textColor="@android:color/black"
                android:textSize="14sp"
                app:layout_constraintStart_toEndOf="@+id/task_view_name"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/task_view_detail"
                android:layout_width="228dp"
                android:layout_height="31dp"
                android:layout_marginStart="17dp"
                android:fontFamily="@font/robotolight"
                android:text="@string/task_view_detail_literal"
                android:textColor="@android:color/black"
                android:textSize="12sp"
                app:layout_constraintStart_toEndOf="@+id/task_view_icon"
                app:layout_constraintTop_toBottomOf="@+id/task_view_name" />



        </android.support.constraint.ConstraintLayout>
        <android.support.constraint.ConstraintLayout
            android:id="@+id/task_delete"
            android:layout_width="116dp"
            android:layout_height="110dp"
            android:background="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="@id/constraintLayout4"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/task_delete_literal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="13dp"
                android:layout_marginTop="39dp"
                android:fontFamily="@font/roboto_regular"
                android:text="@string/delete"
                android:textColor="@android:color/background_light"
                android:textSize="24sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </android.support.constraint.ConstraintLayout>
    </LinearLayout>
</HorizontalScrollView>
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
Jermaine Oliver
  • 115
  • 1
  • 7
  • Try How to build a Horizontal ListView with RecyclerView? https://stackoverflow.com/questions/28460300/how-to-build-a-horizontal-listview-with-recyclerview – Ramesh sambu Dec 20 '17 at 04:02
  • Forgive me if I misunderstand but I'm looking to make a horizontal scroll to a vertical ListView row, not a horizontal ListView. – Jermaine Oliver Dec 20 '17 at 04:23
  • Can you share an example of the screenshot? – Ramesh sambu Dec 20 '17 at 04:27
  • This isn't answering your question but may I ask the purpose of the 2 `ConstraintLayout`s – n_r Dec 20 '17 at 09:01
  • @Kiya I was trying a few combinations. After some trial and error I may have a solution (that I haven’t seen online yet) that I will post here later today. – Jermaine Oliver Dec 20 '17 at 10:21

1 Answers1

0

For anyone having this problem, I managed to find a way to workaround this. My problem was that I could not click on the ListView item because of the HorizontalScrollView setup in the XML, however in my case I was using an adapter along with my ListView (that allows you link your listView with your actual list row view) and in that adapter I simply defined and linked the container of the view that I want to press and set an onClickListener.

This goes in your custom adapter:

ConstraintLayout myLayout = convertView.findViewById(R.id.constraintLayout1);
    ConstraintLayout anotherLayout = convertView.findViewById(R.id.constraintLayout2); //lets' say I'm using this to delete the item when clicked



    myLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent exampleIntent = new Intent(viewParent.getContext(), SomeActivity.class); 
        }
    });
    anotherLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(view.getContext());

            builder.setMessage("Are You Sure?").setNegativeButton("No", null);
            builder.setPositiveButton("yes", new android.support.v7.app.AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Database database = new Database(viewParent.getContext());
                    database.deleteItem(item);

                    List<Item> items = database.getItems();
                    ListView list = (ListView) viewParent.findViewById(R.id.my_linear_list);

                    //List<Item> items = new Database(viewParent.getContext()).getItems();

                    TaskAdapter adapterNew = new ItemAdapter(viewParent.getContext(), tasks);

                    list.setAdapter(adapterNew);
                }
            });
            builder.show();
        }
    });
Jermaine Oliver
  • 115
  • 1
  • 7