Multiline EditText in RecyclerView scrolling issues
Asked Answered
J

2

8

I have a RecyclerView that contains a multiline EditText as a list item, the EditText expands each time a line is added. The RecyclerView has a standard vertical LinearLayoutManager and a standard RecyclerView.Adapter<RecyclerView.ViewHolder> adapter with 15 of the EditText list items. I'll add the code at the end of the question.

The issue is that after you have added a number of lines to the EditText, it begins to scroll the list down each time a line is added. Eventually, after you have added enough new lines, the EditText will be scrolled off the top of the screen and loses focus. If you try to scroll back up and select the last line of the EditText, it will scroll the list back down until the EditText is off the top of the screen and it loses focus.

This isn't an issue for ListView, only for RecyclerView.

Does anyone know how to fix or get around this issue?

The line item:

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="textMultiLine|textCapSentences"
      android:gravity="top"
      android:hint="Enter text"
      android:minLines="2"
      android:layout_margin="16dp"/>

The RecyclerViewFragment and Adapter

public class RecyclerViewFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.recycler_view_fragment, container, false);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setAdapter(new MyAdapter());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;
    }

    private class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
    {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view = getActivity().getLayoutInflater().inflate(R.layout.edit_text_list_item, parent, false);

            MyViewHolder holder = new MyViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {

        }

        @Override
        public int getItemCount()
        {
            return 15;
        }

        private class MyViewHolder extends RecyclerView.ViewHolder
        {

            public MyViewHolder(View itemView)
            {
                super(itemView);
            }
        }
    }
}
Jabalpur answered 25/5, 2015 at 7:11 Comment(0)
J
4

This issue was fixed by the latest version of the RecyclerView: 22.2.0

Jabalpur answered 12/7, 2015 at 9:35 Comment(0)
R
0
EditTextView.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
        // Disallow the touch request for parent scroll on touch of child view
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
    }
});

While Edittext on recycler view, request to not deliver touch event to its parent. This way editText will have touch event and will behave as expected

Retrogressive answered 17/1, 2020 at 17:23 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Neighborhood

© 2022 - 2024 — McMap. All rights reserved.