4

I am implementing horizontal RecyclerView with EditText which is inside. When I click on EditText or enter text to EditText, RecyclerView automaticly scrolls to the beginning. How to prevent this scroll issue?

In this my application where you can enter goals for your training.

I have tried setOnKeyStroke, onClick, setOnFocusChange on EditText.

I set RecyclerView setFocusable to false.

I also tried to set CoordinatorLayout with RecyclerView to android:descendantFocusability="blocksDescendants".

But none of this works.

Here is main_layout.xml


        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="0dp"
            android:orientation="horizontal"
            app:layout_constraintStart_toStartOf="parent"
            android:id="@+id/goal_rv"
            android:background="@drawable/recycler_view_background"
            />
    </android.support.constraint.ConstraintLayout>

Here is recycleView item layout goal_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  >
        <EditText
            android:inputType="text"
            android:id="@+id/generalEditText"
            android:layout_width="300dp"
            android:layout_height="63dp"
            android:background="@drawable/edit_text_general_background"
            android:foregroundGravity="fill_vertical"
            android:gravity="center|center_vertical"
            android:text="@string/add_goal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0"
            />
</android.support.constraint.ConstraintLayout>

Here is main:

 goalRecyclerView = findViewById(R.id.goal_rv);

        goalRecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        goalRecyclerView.setFocusable(false);
        goalRecyclerView.setOnClickListener(this);
        generalAdapter = new GoalListViewAdapter(this);
        generalAdapter.setOnItemClickListener(this);
        goalRecyclerView.setAdapter(generalAdapter);
        goalRecyclerView.setGoals(null);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);

Here is adapter:



    void setGoals(ArrayList<Goal> goals){
        if(goals != null && this.goals != null && goals.size() != this.goals.size()) {
            this.goals.clear();
            this.goals = goals;
        }
        if(this.goals !=null) {
            this.goals.add(EMPTY_GOAL);
            this.goals.add(EMPTY_GOAL);/*problem appears in second edittext*/

        }
    }

    ArrayList<Goal> getGoals(){
        return goals;
    }
    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.goal_layout, null);
        viewHolder = new CustomViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder customViewHolder, int i) {
        Goal textAtPosition = goals.get(i);
        customViewHolder.fillView(textAtPosition);
    }

I expect no scrolling of RecyclerView during entering text in EditText. I got scrolling to the beggining after focusing and key stroke on editText.

Charles Annic
  • 865
  • 1
  • 8
  • 20
dobrowol
  • 63
  • 7

1 Answers1

1

I found the solution:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false) {
            @Override
            public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {

                if (((ViewGroup) child).getFocusedChild() instanceof EditText) {
                    return false;
                }

                return super.requestChildRectangleOnScreen(parent, child, rect, immediate, focusedChildVisible);
            }
        };
        goalRecyclerView.setLayoutManager(linearLayoutManager);

It was in here: Android - How to disable RecyclerView auto scroll from clicking

dobrowol
  • 63
  • 7
  • I have tried it but nothing changes, can you go to my post to review it? [here](https://stackoverflow.com/questions/64802989/keep-state-recyclerview-when-keyboard-open) – Aldan Nov 24 '20 at 06:59