android - 水平 RecyclerView 在编辑 EditText 时自动滚动

标签 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上聚焦和击键后,我滚动到开头。

最佳答案

我找到了解决方案:

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

关于android - 水平 RecyclerView 在编辑 EditText 时自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57093763/

相关文章:

带有 Webview 的 Android JSON 自定义 ListView

Android 旋转重定向到 webview 的根 URL

android - 如何从android保存新行?

java - Android MVVM RecyclerView ClickListener Kotlin 到 Java 转换

android - EditText setEnabled()不起作用

android - 如何知道EditText的高度?

android - 从新的照片应用程序中挑选图像

android - 如何接收蓝牙设备工作的 Intent ?

android - 如何在 Android 中禁用 RecyclerView 项目?

java - 单击时使 RecyclerView 图像全屏显示