【问题标题】:How to make certain widgets in scroll view unscrollable or sticky at the top of screen like a header如何使滚动视图中的某些小部件不可滚动或像标题一样粘在屏幕顶部
【发布时间】:2011-10-01 08:40:53
【问题描述】:

我正在创建一个 android 应用程序,用户可以在其中在同一屏幕上查看频道列表和节目列表。 例如:-

频道的图像将在左侧,已安排的节目将在右侧,用户可以水平滚动节目列表,由于频道很多,用户还可以垂直滚动整个布局。 所有这一切都已实现,但我无法完成的是标题应该粘在顶部。

标题有 2 个部分:- 1. 图片频道列表上方左侧的简单图片视图,应保持粘性 在顶部且不可滚动 2. 时间列表标题,如上图右侧的 00:00 、 01:00 、 02:00 和 在节目列表的上面,不应该垂直滚动,但应该 可以水平滚动。

以下是我用来显示布局的代码,我无法完成 就是以上两点。它们只是不会保持粘性,它们还会随着布局的其余部分上下滚动

        <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">

                        <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
                                        android:layout_alignParentLeft="true" android:id="@+id/image_layout">
                                        <ImageView android:layout_gravity="center_vertical|center_horizontal" android:layout_width="wrap_content"
                                                   android:layout_height="wrap_content"  android:src="@drawable/tv_guide_channels" 
                                                   android:scaleType="fitCenter" android:id="@+id/channel_img_header"
                                                  android:layout_alignParentTop="true"/>
                                        <LinearLayout android:id="@+id/layout_to_add_channel_image" android:layout_width="wrap_content"
                                                      android:layout_height="wrap_content" android:orientation="vertical"
                                                      android:background="@color/White" android:layout_below="@id/channel_img_header" 
                                                      android:layout_alignParentBottom="true"/>
                       </RelativeLayout>

                       <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
                                       android:layout_alignParentRight="true" android:layout_toRightOf="@id/image_layout"
                                       android:id="@+id/programme_layout">
                                        <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
                                                        <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
                                                                    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
                                                                                    android:orientation="horizontal" android:id="@+id/table_header"
                                                                                    android:layout_alignParentTop="true">
                                                                    </LinearLayout>
                                                                    <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"
                                                                                android:layout_below="@id/table_header">
                                                                    <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
                                                                                    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
                                                                                                    android:gravity="center_vertical" android:background="@color/greyblue"
                                                                                                    android:orientation="vertical" android:id="@+id/table_programme"
                                                                                                    android:layout_alignParentBottom="true">
                                                                                     </LinearLayout>    

                                                                        </RelativeLayout>
                                                                        </ScrollView>
                                                            </RelativeLayout>
                                         </HorizontalScrollView>
                       </RelativeLayout>

        </RelativeLayout>

<LinearLayout android:layout_height="2dip" android:layout_width="fill_parent" android:background="@color/Black"></LinearLayout>

【问题讨论】:

    标签: android xml android-layout horizontalscrollview android-scrollview


    【解决方案1】:

    我知道这是一个老问题,发布我的答案,因为它会帮助其他正在寻找相同的人。
    我出于同样的目的使用StickyScrollViewItems 库。 scrollView 内的任何视图都可以标记为“粘性”以使其在顶部保持粘性。

    【讨论】:

      【解决方案2】:

      让您的 Header View 远离任何 ScrollView。

      编辑

      嗨 Abhishek 受您问题的启发,我试图解决它。 你可以查看here

      【讨论】:

      • 我做不到...页眉有 2 个部分,一个不会水平移动,另一个会水平移动并有计时。因此该部分应与程序列表布局处于相同的水平滚动视图中。并且由于频道多,所以节目列表和频道列表必须在一个滚动视图下,这样您就不需要单独滚动频道
      【解决方案3】:

      感谢您的链接,但我找到了解决方案:-

      人们总是可以使用自定义小部件,而且它也不那么可怕:) 在我的情况下,我曾经自定义滚动视图,并在 xml 中提到了他们的包名+类名

      例如:

      <org.vision_asia.utils.MyScrollView1
      android:id="@+id/sv1"
      android:layout_below="@id/channel_img_header"
      android:layout_height="fill_parent"
      android:layout_width="wrap_content"
      android:scrollbars="none">
      
      <LinearLayout
          android:background="@color/White"
          android:id="@+id/layout_to_add_channel_image"
          android:layout_alignParentBottom="true"
          android:layout_below="@id/channel_img_header"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:orientation="vertical" />
      

      并且您希望将此 ScrollView 与其他滚动视图同步,这样如果用户滚动此滚动视图,则必须同时滚动其他滚动视图,就好像所有元素都在一个滚动视图下一样

      public class MyScrollView1 extends ScrollView {
      
          public MyScrollView2 sv2;
      
          public MyScrollView1(Context context) {
              super(context);
              // TODO Auto-generated constructor stub
          }
      
          public MyScrollView1(Context context, AttributeSet attrs) {
              super(context, attrs);
              // TODO Auto-generated constructor stub
          }
      
          @Override
          protected void onScrollChanged(int l, int t, int oldl, int oldt) {
              // TODO Auto-generated method stub
              sv2.scrollTo(oldt, t);
              super.onScrollChanged(l, t, oldl, oldt);
          }
      }
      

      在您的主要活动中,您必须像这样调用自定义滚动视图:-

      sv1 = (MyScrollView1)channelsList.findViewById(R.id.sv1);
      sv1.sv2 = sv2;
      

      其中 sv2 是应该同步的滚动视图

      要完全同步,您需要 2 个自定义滚动视图

      享受

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-11
        • 2010-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多