【问题标题】:Scrollable Fragments within Activity (no viewpager)Activity 中的可滚动片段(无 viewpager)
【发布时间】:2015-02-07 11:21:23
【问题描述】:

我想制作一个具有以下基本布局的 Android 应用:

Activity 应该包含几个片段(它们都有不同的布局),可以通过向左或向右滚动(在图像中的黑色边框区域)或按上一个/下一个按钮来访问这些片段。

片段的布局包含输入字段,当按下下一个按钮或执行向右滑动时,应验证这些输入字段。如果输入字段为空,则不应显示下一个片段。

我尝试使用 viewpager 进行此操作,但这仅适用于一些不太好的解决方法,因此我正在尝试寻找不同的方法。

所以我的问题是:在不使用 viewpager 的情况下实现上述功能的最佳方法是什么?也许在片段上放置一个手势监听器并手动滑动它们?

我们将不胜感激!

【问题讨论】:

  • ViewPager 出了什么问题?
  • 您可以根据需要使用手势监听器和替换片段。
  • @Blackbelt 原来验证非常棘手。
  • @SunitKumarGupta “替换碎片”是什么意思?我需要将这些片段的内容存储在某个地方。

标签: android android-fragments scrollable


【解决方案1】:

拥有一个带有按钮的向导式 UI 很好,至少有一个库可以做到这一点。我通常不同意您关于滑动事件的方法。

原来验证非常棘手

我不知道你这是什么意思。

恕我直言,最简单的解决方案是使用ViewPager,在满足验证条件之前不要添加“下一页”页面,如果未满足验证条件,则删除“下一页”页面(例如,用户清除该字段)。您将需要为此使用自定义 PagerAdapter,或者可能是 my ArrayPagerAdapter,因为从库存适配器添加/删除页面并不能很好地工作(尽管它们可能只是在一端...)。

但是,我再次认为您的 UX 不是一个好的用户体验。假设您在向导中总共有四个可能的页面,这里命名为 A、B、C 和 D。用户从 A 开始,填写您的字段,前进到 B,填写您的字段,前进到 C,填写您的字段,前进到 D,返回到 B,并清除 B 的字段(或以其他方式使其无效)。根据您的流程,在填写 B 的字段之前,用户不能再次进入 C。从通过“下一步”按钮推进的角度来看,在 B 的表单验证之前将其禁用是完全合理的,并且用户之前可能已经看到过这种行为(例如,桌面操作系统向导)。但是用户不一定会理解 Next 按钮和滑动在逻辑上是同一件事,因为用户习惯于滑动到他们想要的任何地方,一旦他们意识到他们可以在那里滑动。因此,如果我这样做,我会使用直接的向导 UI,没有滑动事件。

【讨论】:

    【解决方案2】:

    ViewPager 至少加载两个片段,一个是您正在查看的片段,另一个是下一个/上一个片段。因此,您将永远无法使用 ViewPager 顺利完成您愿意做的事情。

    相反,您可以使用上一个和下一个按钮来分离当前片段并在验证您的输入后附加新片段。如果你想让它看起来像一个 ViewPager,你可以为当前片段和新片段添加幻灯片动画。

    【讨论】:

      【解决方案3】:

      这是我的代码,可以帮到你

         <?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:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".view.FormKorbanActivity">
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_above="@+id/layoutBottom">
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                  android:id="@+id/parent_linear_layout_korban" >
      
                  <LinearLayout 
                      android:layout_width="match_parent"
                      android:layout_height="50dp"
                      android:orientation="horizontal" >
                      <EditText
                          android:id="@+id/kerugian_text"
                          android:layout_width="0dp"
                          android:layout_height="match_parent"
                          android:layout_weight="5"
                          android:inputType="textLongMessage"/>
                      <Spinner
                          android:id="@+id/type_spiner_kerugian"
                          android:layout_width="0dp"
                          android:layout_height="40dp"
                          android:layout_weight="3"
                          android:entries="@array/tipe_bangunan"
                          android:gravity="right" />
                      <Button
                          android:id="@+id/delete_kerugian"
                          android:layout_width="0dp"
                          android:layout_height="40dp"
                          android:layout_weight="1"
                          android:background="@drawable/ic_remove"
                          android:onClick="onDelete"/>
                  </LinearLayout>
      
              </LinearLayout>
      
          </ScrollView>
      
          <FrameLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/layoutBottom"
              android:layout_alignParentBottom="true">
      
              <LinearLayout
      
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
      
                  <Button
                      android:id="@+id/btnCancel"
                      style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_gravity="bottom"
                      android:layout_weight="1"
                      android:onClick="onCancel"
                      android:text="Batal"
                      android:textColor="@color/darkDeepOrange"/>
      
                  <Button
                      android:id="@+id/btnNext"
                      style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_gravity="bottom"
                      android:layout_weight="1"
                      android:onClick="onNext"
                      android:text="Berikutnya" />
              </LinearLayout>
          </FrameLayout>
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-13
        • 2023-04-08
        • 1970-01-01
        相关资源
        最近更新 更多