horizo​​ntalScrollView 的 scrollTo() 不起作用

scrollTo() for horizontalScrollView not working

我膨胀另一个布局以显示在我当前布局中的某个视图下方。

这样做是这样的:

       LayoutInflater vi = (LayoutInflater) getActivity().getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rootView = vi.inflate(R.layout.horizontal_scroll_view, null);

    horizontalScrollView = (HorizontalScrollView) rootView.findViewById(R.id.hsv_suggestions_scroll_view);
    LinearLayout suggestionsContainer = (LinearLayout) horizontalScrollView.findViewById(R.id.ll_suggestions_container);

而且我可以确认它出现在正确的位置,因为一段时间后我在其中添加了一些视图并且它们都出现了。

我膨胀的布局是:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hsv_suggestions_scroll_view"
android:scrollbars="none" android:layout_gravity="center_horizontal"
android:paddingTop="16dp" android:fillViewport="true"
android:layout_width="match_parent" android:layout_height="wrap_content">

    <LinearLayout
      android:id="@+id/ll_suggestions_container"
      android:gravity="center_horizontal"    android:orientation="horizontal"
      android:layout_width="wrap_content" android:layout_height="wrap_content">

</LinearLayout>

只是一个 HorizontalScrollView 和一个 LinearLayout 作为一个 child。 我后面加的View都是TextView个。

现在,在用户操作(在 editText 上写一些文本)之后,我试图滚动到该视图并突出显示它。突出作品。不起作用的是滚动。

我试过了:

 horizontalScrollView.postDelayed(new Runnable() {
   @Override
   public void run() {

   horizontalScrollView.smoothScrollTo(scrollTo, 0);
   }
}, 300);

其中变量 scrollTo 是我将 getLeft() 应用于我想要滚动到的视图时得到的。我可以确认它采用各种值。

有人可以帮我吗?

尝试使用 scrollTo(),而不是 smoothScrollTo()。
确保您的 getLeft() 确实返回了一个 > 0 的值;

切换到将 RecyclerView 与方向设置为水平的 LinearLayoutManager 一起使用。像这样:

scroll_view.xml

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView"
    android:scrollbars="none"
    android:layout_gravity="center_horizontal"
    android:paddingTop="16dp"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    android:orientation="horizontal">
    <LinearLayout
        android:id="@+id/scrollContainer"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</android.support.v7.widget.RecyclerView>

注意:这里需要layoutManager标签来让布局膨胀。在它膨胀之后,我们也将以编程方式设置它,否则我们会得到一个异常,因为 RecyclerView 基本上会在它完成之前处理掉它。

OptionAdapter.java

public class OptionAdapter extends RecyclerView.Adapter<OptionAdapter.OptionHolder> {

    private Context context;
    private LayoutInflater inflater;

    private ArrayList<String> options;

    public OptionAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);

        options = new ArrayList<>();
    }

    @Override
    public OptionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OptionHolder(inflater.inflate(R.layout.textview, parent, false));
    }

    @Override
    public void onBindViewHolder(OptionHolder holder, int position) {
        String option = options.get(position);

        ((TextView) holder.itemView).setText(option);
    }

    @Override
    public int getItemCount() {
        return options != null ? options.size() : 0;
    }

    public ArrayList<String> getOptions() {
        return options;
    }

    public void addOption(String option, Integer index) {
        if (index != null && index <= options.size()) {
            options.add(index, option);
        } else {
            options.add(option);
        }

        notifyDataSetChanged();
    }

    public class OptionHolder extends RecyclerView.ViewHolder {

        public OptionHolder(View itemView) {
            super(itemView);
        }
    }
}

text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />

正在设置一切

final LayoutInflater inflater = LayoutInflater.from(this);
final RecyclerView scrollView = (RecyclerView) inflater.inflate(R.layout.scroll_view, container, false);
scrollView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

final OptionAdapter adapter = new OptionAdapter(this);
scrollView.setAdapter(adapter);

container 这里是将持有 RecyclerView 的任何视图。在我的代码中,我在 LinearLayout.

中得到了它

正在向列表添加视图

adapter.addOption("The Added One", null);

或者如果你想将它添加到列表中的特定位置。

adapter.addOption("The Added One", position);

滚动到特定位置

scrollView.smoothScrollToPosition(position);

滚动到列表中的特定项目

scrollView.smoothScrollToPosition(adapter.getOptions().indexOf("ItemText"));

希望对你有用!

遇到了类似的问题。

我猜根本原因是UI那个时候还没有渲染,导致滚动不能正常工作

我只需要将对 smoothScrollTo 的调用包装到 .post 中,如下所示:

mScrollView.post(new Runnable() {
    @Override
    public void run() {
        mScrollView.smoothScrollTo(xxx, yyy);
    }
});

请注意,我直接在 scrollView 上执行 post 以确保它在呈现后执行。例如,getActivity().runOnUIThread() 对我来说是行不通的。