2016-03-01 161 views
0

我在我的活動中有2 LinearLayout s。排名第一的有ImageView,另一個有ListView。我在ListView有很多項目。滾動ListView外的項目

我想要的是向下滾動整個頁面。所以當我向下滾動含有ImageViewLinearLayout應該會消失。

我將兩個版面高度設置爲wrap_content,但我只能向下滾動ListView,頂部LinearLayout保持原樣。

如果不將ImageView作爲項目放入ListView,是否可以實現我想要的功能?

這裏是佈局;

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

    <ImageView 
    android:id="@+id/imgMoviePoster" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="10dp" 
    android:scaleType="centerCrop"/> 
</LinearLayout> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

    <ListView 
     android:id="@+id/lstMovies" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 
+0

你可以使用滾動視圖作爲父窗口,但是你必須以編程方式設置你的列表視圖的高度。你可以給你最高的線性佈局作爲你的列表視圖的頭來實現你想要的。 – dgngulcan

回答

1

通常不是一個很好的做法,裏面另一個滾動視圖滾動視圖,在這種情況下,一個簡單的方法,將是一個頭添加到ListView。簡單,簡單,性能更好。

ListView listView = (ListView) findViewById(R.id.list); 
View header = getLayoutInflater().inflate(R.layout.header, null); 
listView.addHeaderView(header); 

如果您有任何疑問,您可以看看這個tutorial! (頭將與您的圖像的LinearLayout)

+0

這就是我想要的,謝謝。 – Mtok

0
<ScrollView android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <ImageView 
    android:id="@+id/imgMoviePoster" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginBottom="10dp" 
    android:scaleType="centerCrop"/> 
    </LinearLayout> 
    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

     <yourpackagename.ExpandableHeightListView 
     android:id="@+id/lstMovies" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 
</LinearLayout> 
    </ScrollView> 

高度可擴展列表視圖

 import android.content.Context; 
     import android.util.AttributeSet; 
     import android.view.ViewGroup; 
     import android.widget.ListView; 

    public class ExpandableHeightListView extends ListView 
    { 

    boolean expanded = false; 

    public ExpandableHeightListView(Context context) 
    { 
     super(context); 
    } 

    public ExpandableHeightListView(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
} 

public ExpandableHeightListView(Context context, AttributeSet attrs,int defStyle) 
{ 
    super(context, attrs, defStyle); 
} 

public boolean isExpanded() 
{ 
    return expanded; 
} 

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{ 
    // HACK! TAKE THAT ANDROID! 
    if (isExpanded()) 
    { 
     // Calculate entire height by providing a very large height hint. 
     // But do not use the highest 2 bits of this integer; those are 
     // reserved for the MeasureSpec mode. 
     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 

     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = getMeasuredHeight(); 
    } 
    else 
    { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
} 

public void setExpanded(boolean expanded) 
{ 
    this.expanded = expanded; 
} 
} 

和活動文件集list.setExpanded(真)

0

有可能要做到這一點。您需要創建一個從ListView擴展的類,然後重寫它的onTouch方法。當滾動時,您可以獲取手指的位置,然後控制LinearLayout的位置,它包含插入一個ImageView。

0

我曾經有過相同的場景,我解決了這個問題。請檢查我在你的佈局所做的更改:

 <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/imgMoviePoster" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_marginBottom="10dp" 
      android:scaleType="centerCrop" /> 
     </LinearLayout> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

     <ListView 
      android:id="@+id/lstMovies" 
      android:layout_width="match_parent" 
      android:nestedScrollingEnabled="true" 
      android:layout_height="200dp" /> 
    </LinearLayout> 

    </ScrollView> 
  1. 兩個LinearLayouts我把它放在一個滾動型。這樣整個視圖就可以基於設備進行滾動。

  2. 我做了一個高度listview爲200dp。 ListView只會伸展特定的高度。

3.Added屬性下面listviewas:

機器人:nestedScrollingEnabled = 「真」

這裏列表視圖可以滾動主ScrollView.We裏面只是想保持listview height作爲限制,一旦整個視圖在主視圖滾動後被listview覆蓋,那麼listview滾動只會起作用。所以爲了避免這種情況,我將listview的大小設爲200 dp。試試這個,等待你的反饋。

Happy Codding !!