如何制作包含列表视图+图像视图的可滚动布局
How to make a scrollable layout that includes a listview + imageview
所以,我在布局文件中得到了一个带有列表视图的片段。在此列表视图上方是带有徽标的图像视图。
我面临的问题是无法将滚动条设置为整个布局。
我在其他帖子上写了红色,你不应该这样做,但如果我必须以另一种方式来做,我必须重做这么多代码。
我还从 API 获取数据,元素的数量可能会发生变化。这就是我使用列表视图的原因。
我对布局类型没有偏好。目前我使用的是ScrollView布局。
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:src="@drawable/logo"/>
<ListView
android:id="@+id/listFemArrangementer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@color/white"
android:scrollbars="none">
</ListView>
</LinearLayout>
有没有人有解决这个问题的好方法?
尝试将布局 xml 更改为:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment"
android:fillViewport="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:src="@drawable/logo"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listFemArrangementer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@color/white"
android:scrollbars="none"/>
</ScrollView>
</LinearLayout>
可以使用 NestedScrollView
,但首先最好使用 RecyclerView
而不是 ListView
。
下面是一个简单的xml演示此布局
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:src="@drawable/logo"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/listFemArrangementer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
找到了解决我的问题的方法。
我制作了一个列表视图class 并禁用了滚动等等。
public class ScrollDisabledListView extends ListView {
private int mPosition;
public ScrollDisabledListView(Context context) {
super(context);
}
public ScrollDisabledListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
super.dispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
setPressed(false);
invalidate();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
然后我在布局(滚动视图)中使用了这个class
<no.packagename.ScrollDisabledListView
android:id="@+id/listFemArrangementer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@color/white"
android:scrollbars="none"
android:padding="0px"
>
</no.packagename.ScrollDisabledListView>
然后我从适配器中找到并使用了列表的高度
listView.setAdapter(adapter);
if(adapter != null){
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
所以,我在布局文件中得到了一个带有列表视图的片段。在此列表视图上方是带有徽标的图像视图。
我面临的问题是无法将滚动条设置为整个布局。 我在其他帖子上写了红色,你不应该这样做,但如果我必须以另一种方式来做,我必须重做这么多代码。
我还从 API 获取数据,元素的数量可能会发生变化。这就是我使用列表视图的原因。
我对布局类型没有偏好。目前我使用的是ScrollView布局。
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:src="@drawable/logo"/>
<ListView
android:id="@+id/listFemArrangementer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@color/white"
android:scrollbars="none">
</ListView>
</LinearLayout>
有没有人有解决这个问题的好方法?
尝试将布局 xml 更改为:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment"
android:fillViewport="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:src="@drawable/logo"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/listFemArrangementer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@color/white"
android:scrollbars="none"/>
</ScrollView>
</LinearLayout>
可以使用 NestedScrollView
,但首先最好使用 RecyclerView
而不是 ListView
。
下面是一个简单的xml演示此布局
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:src="@drawable/logo"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/listFemArrangementer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
找到了解决我的问题的方法。
我制作了一个列表视图class 并禁用了滚动等等。
public class ScrollDisabledListView extends ListView {
private int mPosition;
public ScrollDisabledListView(Context context) {
super(context);
}
public ScrollDisabledListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
super.dispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
setPressed(false);
invalidate();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
然后我在布局(滚动视图)中使用了这个class
<no.packagename.ScrollDisabledListView
android:id="@+id/listFemArrangementer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@color/white"
android:scrollbars="none"
android:padding="0px"
>
</no.packagename.ScrollDisabledListView>
然后我从适配器中找到并使用了列表的高度
listView.setAdapter(adapter);
if(adapter != null){
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}