当编辑EditText时,Horizo​​ntal RecyclerView自动滚动

时间:2019-07-18 11:48:18

标签: android android-recyclerview android-edittext autoscroll

我正在使用内部的EditText实现水平RecyclerView。当我单击EditText或向EditText输入文本时,RecyclerView会自动滚动到开头。如何防止此滚动问题?

在此我的应用程序中,您可以在其中输入训练目标。

我尝试了EditText上的setOnKeyStroke,onClick,setOnFocusChange。

我将RecyclerView setFocusable设置为false。

我还尝试将带有RecyclerView的CoordinatorLayout设置为 android:descendantFocusability =“ blocksDescendants”

但是这些都不起作用。

这是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>

这是recycleView项的布局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>

这是主要的:

 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);

这是适配器:



    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);
    }

我希望在EditText中输入文本期间不会滚动RecyclerView。在editText上进行聚焦和击键之后,我滚动到了开始。

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

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);

它在这里: Android - How to disable RecyclerView auto scroll from clicking