滚动ListView外的项目

问题描述:

我在我的活动中有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

通常不是一个很好的做法,里面另一个滚动视图滚动视图,在这种情况下,一个简单的方法,将是一个头添加到ListView。简单,简单,性能更好。

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

如果您有任何疑问,您可以看看这个tutorial! (头将与您的图像的LinearLayout)

+0

这就是我想要的,谢谢。 – Mtok

<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(真)

有可能要做到这一点。您需要创建一个从ListView扩展的类,然后重写它的onTouch方法。当滚动时,您可以获取手指的位置,然后控制LinearLayout的位置,它包含插入一个ImageView。

我曾经有过相同的场景,我解决了这个问题。请检查我在你的布局所做的更改:

 <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 !!