滚动ListView外的项目
我在我的活动中有2 LinearLayout
s。排名第一的有ImageView
,另一个有ListView
。我在ListView
有很多项目。滚动ListView外的项目
我想要的是向下滚动整个页面。所以当我向下滚动含有ImageView
的LinearLayout
应该会消失。
我将两个版面高度设置为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>
<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>
两个LinearLayouts我把它放在一个滚动型。这样整个视图就可以基于设备进行滚动。
我做了一个高度listview为200dp。 ListView只会伸展特定的高度。
3.Added属性下面listviewas:
机器人:nestedScrollingEnabled = “真”
这里列表视图可以滚动主ScrollView.We里面只是想保持listview height作为限制,一旦整个视图在主视图滚动后被listview覆盖,那么listview滚动只会起作用。所以为了避免这种情况,我将listview的大小设为200 dp。试试这个,等待你的反馈。
Happy Codding !!
你可以使用滚动视图作为父窗口,但是你必须以编程方式设置你的列表视图的高度。你可以给你最高的线性布局作为你的列表视图的头来实现你想要的。 – dgngulcan