使用自定义适配器和滚动视图获取列表视图的所有高度
Get all height at listview with custom adapter and scrollview
我想将 listview
与自定义适配器一起使用,并且它会动态变化。
有一个是我想要的(ListView
上面的元素必须滚动):
我找到了两种主要的方法:
- 使用
ScrollView
和代码 listView.measure(0,0);
来动态设置 listview
高度(但它不起作用,列表视图被裁剪);
例如:listView 有 3 个项目,但它的高度是 2 个项目(隐藏 1 个项目);
- 不要使用
ScrollView
,使用setHeaderView
(但它也不起作用,ListView
没有滚动)
有什么想法吗?
我的一个应用程序中有一个片段看起来几乎一样..
我和你有同样的问题,我在我的案例中所做的是将我的每个列表视图单元格的大小设置为固定大小。并按该逻辑测量高度。
不确定这是否被认为是实现该目标的最佳方式,但它对我有用。
filterListView.getLayoutParams().height = (searchLabelsList.size() * (int) (43 * getScale() + 0.5f)) + (filterListView.getDividerHeight() * (searchLabelsList.size() - 1));
filterListView.setAdapter(searchLabelsAdapter);
并且在公式中,43 是每个单元格的高度,当然是以 dp 为单位。
getScale是我写的获取当前用户屏幕尺寸scale的方法phone:
private float scale;
private float getScale() {
if (scale == 0) {
if (getActivity() != null) {
scale = getActivity().getResources().getDisplayMetrics().density;
}
}
return scale;
}
希望这会有所帮助,如有任何问题,请随时提出:)
祝你好运
在此处使用自定义 ListView ExpandableHeightListView
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(true);
在 onCreate() 方法中。
你可以试试这个:
首先:在您的 xml 中将所有其他视图放入 ScrollView 中,包括 ListView
<ScrollView
android:id="@+id/scrollViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
// Add your other views over here....
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
第二个:在您的 java 文件中,
Just use this custom method setListViewHeightBasedOnChildren(listview)
如何 ??
list = (ListView) findViewById(R.id.listview);
list.setAdapter(YOUR CUSTOM ADAPTER);
setListViewHeightBasedOnChildren(list);
这是您的自定义方法。
public static void setListViewHeightBasedOnChildren(ListView listView)
{
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight=0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LayoutParams.MATCH_PARENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
希望这对你有所帮助。
我想将 listview
与自定义适配器一起使用,并且它会动态变化。
有一个是我想要的(ListView
上面的元素必须滚动):
我找到了两种主要的方法:
- 使用
ScrollView
和代码listView.measure(0,0);
来动态设置listview
高度(但它不起作用,列表视图被裁剪); 例如:listView 有 3 个项目,但它的高度是 2 个项目(隐藏 1 个项目);
- 不要使用
ScrollView
,使用setHeaderView
(但它也不起作用,ListView
没有滚动)
有什么想法吗?
我的一个应用程序中有一个片段看起来几乎一样.. 我和你有同样的问题,我在我的案例中所做的是将我的每个列表视图单元格的大小设置为固定大小。并按该逻辑测量高度。
不确定这是否被认为是实现该目标的最佳方式,但它对我有用。
filterListView.getLayoutParams().height = (searchLabelsList.size() * (int) (43 * getScale() + 0.5f)) + (filterListView.getDividerHeight() * (searchLabelsList.size() - 1));
filterListView.setAdapter(searchLabelsAdapter);
并且在公式中,43 是每个单元格的高度,当然是以 dp 为单位。
getScale是我写的获取当前用户屏幕尺寸scale的方法phone:
private float scale;
private float getScale() {
if (scale == 0) {
if (getActivity() != null) {
scale = getActivity().getResources().getDisplayMetrics().density;
}
}
return scale;
}
希望这会有所帮助,如有任何问题,请随时提出:)
祝你好运
在此处使用自定义 ListView ExpandableHeightListView
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(true);
在 onCreate() 方法中。
你可以试试这个:
首先:在您的 xml 中将所有其他视图放入 ScrollView 中,包括 ListView
<ScrollView
android:id="@+id/scrollViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
// Add your other views over here....
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
第二个:在您的 java 文件中,
Just use this custom method setListViewHeightBasedOnChildren(listview)
如何 ??
list = (ListView) findViewById(R.id.listview);
list.setAdapter(YOUR CUSTOM ADAPTER);
setListViewHeightBasedOnChildren(list);
这是您的自定义方法。
public static void setListViewHeightBasedOnChildren(ListView listView)
{
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight=0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LayoutParams.MATCH_PARENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
希望这对你有所帮助。