1

I've created a ListView that has an EditText in each row. The ListView also allows the user to delete an item in the list by swiping horizontally using SwipeDismissListViewTouchListener.java from https://github.com/romannurik/Android-SwipeToDismiss.

Both functionality work well, except when the user tries to do the swiping gesture with the initial ACTION_DOWN event occurring over the EditText. It looks like the EditText is consuming the ACTION_DOWN event before it can be passed to the SwipeDismissListViewTouchListener, which needs the ACTION_DOWN event to initialize the gesture.

Question:

How can I get the SwipeDismissListViewTouchListener to work when the swipe gesture is initiated on the EditText?

SampleListFragment.java:

import android.app.Activity;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class SampleListFragment extends ListFragment {

    protected AddGroupAdapter adapter;
    protected ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new AddGroupAdapter(getActivity());
        setListAdapter(adapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sample, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        lv = getListView();
        SwipeDismissListViewTouchListener listener =
                new SwipeDismissListViewTouchListener(
                        lv,
                        new SwipeDismissListViewTouchListener.DismissCallbacks() {
                            @Override
                            public boolean canDismiss(int position) {
                                return true;
                            }

                            @Override
                            public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                                for (int position : reverseSortedPositions) {
                                    adapter.removeRecord(position);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        });
        lv.setOnTouchListener(listener);
        lv.setOnScrollListener(listener.makeScrollListener());

        super.onActivityCreated(savedInstanceState);
    }

    public class AddGroupAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private ArrayList<String> data;

        public AddGroupAdapter(Activity activity) {
            mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            data = new ArrayList<String>();
            for (int i = 0; i < 10; i++) {
                data.add("Sample Text " + String.valueOf(i));
            }
            notifyDataSetChanged();
        }

        public void removeRecord(int position) {
            data.remove(position);
        }

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.row, null);
                holder.edit = (EditText) convertView.findViewById(R.id.edit_text);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.edit.setText(data.get(position));
            holder.edit.setId(position);
            holder.edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if (!hasFocus) {
                        final int position = view.getId();
                        final EditText edit = (EditText) view;
                        if (edit.getText() != null && !edit.getText().toString().isEmpty())
                            if (position < getCount())
                                data.set(position, edit.getText().toString());
                    }
                }
            });

            return convertView;
        }
    }

    static class ViewHolder {
        EditText edit;
    }
}

row.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@+id/edit_text"
        android:hint="Enter Text"
        android:gravity="center_horizontal"
        android:singleLine="true" />
</LinearLayout>

*fragment_sample.xml:*

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@android:id/list"
        android:name="com.example.app.SampleListFragment"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        android:descendantFocusability="beforeDescendants"
        tools:context=".SampleActivity"
        tools:layout="@android:layout/list_content"/>

</LinearLayout>
Adam
  • 11
  • 3

1 Answers1

0

Trying adding this to your LinearLayout in the row.xml:

android:descendantFocusability="blocksDescendants"

Edit:

So apparently a EditText is trickier than buttons to have inside a listview. Maybe this answer can put you in the right direction if you must have them in yours: https://stackoverflow.com/a/2680077/362298

Community
  • 1
  • 1
Ricardo
  • 7,785
  • 8
  • 40
  • 60
  • That does not seem to work. The SwipeDismissListViewTouchListener still does not receive the ACTION_DOWN event. – Adam Nov 24 '13 at 22:02
  • I think this answer might be closer to what I need: [http://stackoverflow.com/a/3542124/3027659](http://stackoverflow.com/a/3542124/3027659). I just can't seem to figure out how to implement that into my code (switching from a GestureDetector to an OnTouchListener). – Adam Nov 25 '13 at 02:03