2016-09-30 136 views
1

我有一個滾動視圖,滾動視圖內我有項目,我想將它們中的一些釘在屏幕頂部(如stickyheaderListview)取決於scrollView scrollY。我嘗試了幾乎所有的東西。如何更改scrollView內的視圖位置,將其固定在屏幕頂部

如果我將我的標題項目Y位置設置爲零,它將更改位置,但在此之後它是scrollView中的第一個項目。

header.animate().y(0).setDuration(0).start(); // first attempt 
header.setTop(0); // second attempt 
header.animate().y(mScrollView.getLastItem().getBottom).setDuration(0).start();// third attempt 

PS

我的目標是讓stickyheader列表項中滾動視圖 我想這library內滾動視圖,但它不工作,所有項目均通常滾動

回答

1

好,我認爲可能有多種方式在Android中實現相同的UI。

1)第一種方式,換你的滾動視圖成相對佈局是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:text="top here" 
      android:id="@+id/text" 
      /> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/text" 
      > 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="sample" 
       /> 
     </ScrollView> 
    </RelativeLayout> 

2)或另一種方式是,你可以把你的主要佈局成2個片段,一個片段顯示認爲總是在最上面,另一個會加載scrollview的內容。

3)本帖可能對您很有幫助 Android: pin TabLayout to top of Scrollview

相關問題