ListView控件getFirstVisiblePosition getFirstVisiblePosition getCount将值 [英] ListView getFirstVisiblePosition getFirstVisiblePosition getCount values

查看:940
本文介绍了ListView控件getFirstVisiblePosition getFirstVisiblePosition getCount将值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的ListView类(从ListView的子类),我需要它的一个小填充添加到最终视图元素的底部,以便它不是由我在屏幕底部的小酒吧重叠。我只是想这样做,当孩子长大意见过去列表视图的可见区域。我试图用这个code来实现这一点:

I have a custom ListView class (subclassed from ListView) and I need it to add a small padding to the bottom of the final view element so that it is not overlapped by a small bar that I have across the bottom of the screen. I only want to do this when the child views grow past the visible region of the listview. I am trying to use this code to achieve this:

@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
     super.onLayout(changed, left, top, right, bottom);

     // If there are hidden listview items we need to add a small padding to the last one so that it
     // partially hidden by the bottom sliding drawer handle
     if (this.getLastVisiblePosition() - this.getFirstVisiblePosition() + 1 > this.getCount()) {
         LinearLayout v2 = (LinearLayout) this.getChildAt(this.getCount() - 1);
         v2.setPadding(v2.getPaddingLeft(), v2.getPaddingTop(), v2.getPaddingRight(), 
                 v2.getPaddingBottom() + 5);
     }
 }

不过,getLastVisiblePostion(),getFirstVisiblePostion()和getCount将()返回的值反映不出什么适配器还持有。我假定这是因为该适配器还没有通知的数据的ListView的,但我不能揣摩出ListView控件实际上了解的数据,因此有正确的价值观。这code正在被加载活动时运行。

However, the values returned by getLastVisiblePostion(), getFirstVisiblePostion() and getCount() do not reflect what the adapter holds yet. I am assuming this is because the Adapter has not yet notified the ListView of the data, but I cannot figure out where the ListView would actually know about the data and thus have the correct values. This code is being run when the Activity is being loaded.

在渲染过程中什么时候我将有机会获得这些数据?我还要说,我使用的AsyncTask从数据库加载数据,然后在那里创建适配器并将其添加到列表视图。是否有一个事件,我可以在ListView中使用时,该适配器将数据添加,将火/原因呈现新项目的列表视图?

At what point in the rendering process will I have access to this data? I should also say that I use an AsyncTask to load the data from a database and then create the Adapter in there and add it to the list view. Is there an event I can use within the ListView that will fire when the adapter adds data/causes the listview to render the new items?

推荐答案

我不知道这是否是最好的解决办法,但我知道,在一个拉来刷新执行针对Android的分支之一,其高度列表的对所有列表项的总高度相比。从你说的话,这听起来像这就是你以确定是否应用微胖或不找相同的信息。

I'm not sure if it's the best solution, but I know that in one of the branches of a pull-to-refresh implementation for Android, the height of the list is compared against the total height of all the list items. From what you're saying, it sounds like that's the same information you're looking for in order to determine whether to apply extra padding or not.

执行的有关部分是在以下三种方法:

The relevant parts of the implementation are in the following three methods:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mHeight == -1) {  // do it only once
        mHeight = getHeight(); // getHeight only returns useful data after first onDraw()
        adaptFooterHeight();
    }
}

/**
 * Adapts the height of the footer view.
 */
private void adaptFooterHeight() {
    int itemHeight = getTotalItemHeight();
    int footerAndHeaderSize = mFooterView.getHeight()
        + (mRefreshViewHeight - mRefreshOriginalTopPadding);
    int actualItemsSize = itemHeight - footerAndHeaderSize;
    if (mHeight < actualItemsSize) {
        mFooterView.setHeight(0);
    } else {
        int h = mHeight - actualItemsSize;
        mFooterView.setHeight(h);
        setSelection(1);
    }
}

/**
 * Calculates the combined height of all items in the adapter.
 * 
 * Modified from http://iserveandroid.blogspot.com/2011/06/how-to-calculate-lsitviews-total.html
 * 
 * @return 
 */
private int getTotalItemHeight() {
    ListAdapter adapter = getAdapter();
    int listviewElementsheight = 0;
    for(int i =0; i < adapter.getCount(); i++) {
        View mView  = adapter.getView(i, null, this);
        mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        listviewElementsheight+= mView.getMeasuredHeight();
    }
    return listviewElementsheight;
}

完整的源$ C ​​$ C可以发现<一个href=\"https://github.com/johannilsson/android-pulltorefresh/blob/scrollfix_for_short_list/pulltorefresh/src/com/markupartist/android/widget/PullToRefreshListView.java\"相对=nofollow>这里在GitHub上。

这篇关于ListView控件getFirstVisiblePosition getFirstVisiblePosition getCount将值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆