在ListView外滚动项目

时间:2016-03-01 08:47:26

标签: android listview

我的活动中有2 LinearLayout个。前一个有ImageView,另一个有ListView。我在ListView中有很多项目。

我想要的是向下滚动整个页面。因此,当我向下滚动时,包含LinearLayout的{​​{1}}应该会消失。

我将布局高度设置为ImageView,但我只能向下滚动wrap_content,顶部ListView会停留在原来的位置。

是否有可能实现我想要的而不将LinearLayout放入ImageView作为项目?

以下是布局;

ListView

4 个答案:

答案 0 :(得分:1)

在另一个可滚动视图中包含可滚动视图通常不是一个好习惯,在这种情况下,直接的方法是向listView添加一个标题。简单,轻松,性能更佳。

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

如果您有任何疑问,可以查看此tutorial! (标题将是与您的图像一起的LinearLayout)

答案 1 :(得分: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;
}
} 

并在Activity文件中设置list.setExpanded(true)

答案 2 :(得分:0)

可以这样做。你需要创建一个从ListView扩展的类,然后覆盖它的onTouch方法。滚动时你可以获得手指的位置,然后控制包含ImageView的LinearLayout的位置

答案 3 :(得分: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都放在ScrollView中。因此整个视图可以基于设备滚动。

  2. 我将listview的高度设为200dp。 Listview只会伸展特定的高度。

  3. 3.添加属性到下面的listviewas:

      

    android:nestedScrollingEnabled =“true”

    这里列表视图也在主ScrollView内滚动。我们只想将listview高度保持为限制,一旦在主视图滚动后整个视图被listview覆盖,那么listview滚动只能工作。所以要避免我将列表视图大小设为200 dp。试试这个,等待你的反馈。

    快乐的Codding !!

相关问题