Android --- ListView之高度由 item 的个数决定,wrap_content有效

需要计算list中显示条目的个数,然后在在代码中设置listview的高度。

这里看看我怎么实现的
我们的布局代码:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/search_title_layout"
        android:orientation="vertical">

        <com.xis.read.view.ListViewForScrollView
            android:id="@+id/lv_history"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tv_clear_history"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_common_gray_background"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/claer_history"
            android:textColor="@color/readpage_rose" />
    </LinearLayout>

上面的重要的只有一条ListView的高度用wrap_content

接下来我们只要自定义ListView就好了。这个自定义ListView也可以用于ScrollView中嵌套使用

package cn.xs.reader.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * Created by Saud on 16/2/15.
 */
public class ListViewForScrollView extends ListView {
    public ListViewForScrollView(Context context) {
        super(context);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs,
                                 int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    /**
     * 重写该方法,达到使ListView适应ScrollView的效果
     */
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }


}
为了防止ListView高度设置为`wrap_content`时遮挡底部按钮,你可以采取以下几种策略: 1. **给ListView设置固定高度**:如果需要确保底部始终可见,可以为ListView设置一个固定的最小高度,使其不再随内容自动调整。 ```xml <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content" <!-- 或者设置为一个具体的像素值如 "android:layout_height="200dp"` --> android:minHeight="?android:attr/listPreferredItemHeight" // 自动获取适配器项的高度作为最小高度 ...其他配置...> </ListView> ``` 2. **使用FrameLayout**:将ListView放在一个FrameLayout内,并设置布局权重,这样当ListView的内容不够填充整个空间时,Button会显示出来。 ```xml <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/list_view" android:layout_weight="1" ...其他配置.../> <Button android:id="@+id/bottom_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" /> </FrameLayout> ``` 3. **监听列表滚动事件**:在ListView的滚动监听器里,检查是否到达底部,然后手动调整Button的位置,让它浮上来。 ```java listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) {} @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem + visibleItemCount == totalItemCount && !isLastItemOnScreen(view)) { // 如果已到底部并且不是最后一个item,提升按钮 button.setTranslationY(-button.getHeight()); } else { // 否则,恢复按钮位置 button.setTranslationY(0); } } }); ``` 这里假设`isLastItemOnScreen`是一个检查当前可视区域最后一个元素是否为最后一项的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值