【问题标题】:Make NEXT button in HorizontalView to scroll through different ImageView在水平视图中制作 NEXT 按钮以滚动不同的 ImageView
【发布时间】:2017-10-26 00:50:21
【问题描述】:

我有一个水平视图。在里面,有一个LinearLayout,我通过代码添加了多个ImageView。我想实现 NEXT 和 PREVIOUS 按钮,以便它自动滚动。当我到达 Horizo​​ntalView 的末尾时,我还想隐藏按钮。

http://imgur.com/a/313af

我不知道该怎么做。

编辑:此 CardView 位于 recyclerView 内。

这里是 XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    android:layout_gravity="center"
    android:layout_margin="8dp"
    card_view:cardCornerRadius="4dp"
    >

    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="1"
        android:paddingTop="5dp">

        <TextView
            android:id="@+id/nombrecito"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"

            android:maxLines="1"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textSize="20sp"
            android:fontFamily="fonts/rugs_font.otf"/>

        <RelativeLayout>

            <HorizontalScrollView
                android:id="@+id/horizontal_scroll"
                android:layout_width="wrap_content"
                android:layout_height="254dp"                >

                <LinearLayout
                    android:id="@+id/linear"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"

                    android:fitsSystemWindows="false">

                    <ImageView

                        android:id="@+id/foto"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="-10dp"
                        android:contentDescription="fotografía"
                        android:scaleType="fitEnd"
                        android:src="@android:drawable/ic_menu_camera" />
                </LinearLayout>

            </HorizontalScrollView>
            <ImageButton
                android:shape="ring"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="#ceb06a"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_keyboard_arrow_left_black_48dp"/>
            <ImageButton
                android:shape="ring"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="#ceb06a"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_keyboard_arrow_right_black_48dp"/>
        </RelativeLayout>


        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:rowCount="1">

            <ImageButton
                android:id="@+id/btnLike"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@android:color/transparent"
                android:inAnimation="@anim/slide_in_likes_counter"
                android:outAnimation="@anim/slide_out_likes_counter"
                android:src="@drawable/ic_heart_outline_grey" />

            <ImageButton
                android:id="@+id/btnShare"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@android:color/transparent"
                android:onClick="share"
                android:src="@android:drawable/ic_menu_share" />

        </GridLayout>
    </GridLayout>
</android.support.v7.widget.CardView>

【问题讨论】:

  • 为什么不使用视图寻呼机呢?
  • 谢谢。我会试一试,看看它是如何工作的。我会和你保持联系
  • 你可以试试recycler view
  • 我可以试试,但我已经有了回收站的视图。里面都是那些cardViews。这会是个问题吗?

标签: android scroll imageview android-linearlayout next


【解决方案1】:

我认为实现这种情况的最佳方法是使用RecyclerView,因为 ViewPager 不会在不列出特定图片的情况下向您显示两张图片

使其水平
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true)

在RelativeLayout中添加回收器和两个按钮,将按钮放在回收器上的正确位置

例如,用于左键

android:layout_alignParent=true & android:layout_centerHorizontal="true"

要使按钮正常工作,请使用 smth like

recyclerView.scrollToPosition(position);

【讨论】:

  • 谢谢。我会试试看。但我已经有了回收者的看法。里面都是那些cardView。会不会有问题?
  • 我认为你应该在垂直回收视图中制作水平回收视图,现在你在回收视图中只有一个水平滚动视图。喜欢这里stackoverflow.com/questions/41714993/…
  • 我改变了它,现在有点工作。我有这个: holder.btnPrev.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { holder.recyclerView.scrollToPosition(1); } });对于 0 不起作用。不知道为什么
  • 没关系。有用。只是它不是 0、1、2。它是 2、1、0
【解决方案2】:

我查看了图片,我建议为此使用ViewPager

【讨论】:

  • 谢谢。我会试一试,看看它是如何工作的。我会和你保持联系
猜你喜欢
  • 2019-10-16
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
相关资源
最近更新 更多