ScrollView android里面的Horizo​​ntalScrollView

时间:2017-02-14 15:58:27

标签: android android-recyclerview android-scrollview

我在VerticalScrollView中有一个Horizo​​ntalScrollView。如果我水平滚动,滚动是平滑的。但是如果我垂直滚动,滚动不顺畅,有时会水平滚动。如何使垂直滚动平滑?

这是我的xml,

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sasank.calendarview.MainActivity"
    android:background="@android:color/white">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_marginLeft="200dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/chapter_list"
                    android:layout_width="150dp"
                    android:layout_height="match_parent">

                </android.support.v7.widget.RecyclerView>

                <HorizontalScrollView
                    android:id="@+id/horizontal_scroll"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_toRightOf="@id/chapter_list"
                    android:scrollbars="horizontal">
                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/calendar"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent">

                    </android.support.v7.widget.RecyclerView>
                </HorizontalScrollView>

            </RelativeLayout>
        </ScrollView>

    </RelativeLayout>
</RelativeLayout>

我使用VerticalLayoutManager for Chapter_list Recyclerview和GridLayoutManager for Calendar RecyclerView

2 个答案:

答案 0 :(得分:2)

android:nestedScrollingEnabled="true"

您可以尝试这一点,但是仍然存在与嵌套滚动等相关的问题,这些问题实际上很常见。人们在StackOverflow上遇到了同样的问题,看一看,我希望它可以帮到你:Problems with Nested Scrolling

答案 1 :(得分:0)

我的一个项目中有垂直滚动视图和水平循环视图照片库,它工作正常。这是代码:

片段/活动布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

       ...

    <android.support.v7.widget.RecyclerView
        android:id="@+id/horizontal_recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:horizontalSpacing="10dp"
        android:isScrollContainer="false"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp"
        />
            ...

        </LinearLayout>
</ScrollView>

<强>项:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
    android:id="@+id/horizontal_item_view_image"
    android:layout_marginRight="10dp"
    android:layout_width="109dp"
    android:layout_height="109dp" />

</LinearLayout>

<强>适配器:

public class HorizontalPhotosAdapter extends RecyclerView.Adapter<HorizontalPhotosAdapter.MyViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private ArrayList<Bitmap> bitmapList;

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private ImageView riv;

        public MyViewHolder(View view) {
            super(view);

            riv = (ImageView) view.findViewById(R.id.horizontal_item_view_image);

        }
    }


    public HorizontalPhotosAdapter(Context context, ArrayList<Bitmap> bitmapList, String[] imageUrls) {
        this.context = context;
        this.bitmapList = bitmapList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_item_view, parent, false);

        if (itemView.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT)
            itemView.getLayoutParams ().width = RecyclerView.LayoutParams.WRAP_CONTENT;

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        holder.riv.setImageBitmap(bitmapList.get(position));

    }


    @Override
    public int getItemCount() {
        return bitmapList.size();
    }
}
片段/活动中的

实施:

            horizontalAdapter=new HorizontalPhotosAdapter(getContext(), bitmapList);
            horizontal_recycler_view.setAdapter(horizontalAdapter);
            horizontalAdapter.notifyDataSetChanged();

            LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
            horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
            horizontal_recycler_view.setAdapter(horizontalAdapter);