本文介绍了计算一个ListView的每一行的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使一个ListView滚动与ListView的背景。我立足我的方法对这个类的<一个href="http://$c$c.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature/android/shelves/view/ShelvesView.java">Shelves,但同时在货架任何事物都有相同的高度,我不能做出同样的保证。

I am trying to make the background of a ListView scroll with the ListView. I am basing my approach on this class in Shelves, but while in Shelves everything has the same height, I can't make the same guarantee.

我有一个活动,像这样:

I have an activity like so:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listView);

        List<String> items = new ArrayList<String>();
        for(int i=0; i < 100; ++i) {
            items.add("Hello " + i);
        }
        CustomArrayAdapter adapter = new CustomArrayAdapter(this, items);

        listView.setAdapter(adapter);
    }
}

其中CustomArrayAdapter是:

Where CustomArrayAdapter is:

public class CustomArrayAdapter extends ArrayAdapter<String> {
    private List<Integer> mHeights;

    public View getView(int position, View convertView, ViewGroup parent) {
        //snip
    }
}

我想要做的就是填充mHeights与视图的行的高度适配器。

What I want to do is populate mHeights in the adapter with heights of the rows of the view.

我的主要企图是要做到这一点,在getView():

My main attempt was to do this, in getView():

if(row.getMeasuredHeight() == 0) {
    row.measure(
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY),
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY));
}
mHeights.set(position, row.getMeasuredHeight());

我已经尝试了几种方法可以做到这一点,但我不能得到任何工作。

I've tried several ways to do this, but I can't get anything to work.

getView ,如果我叫的getHeight()我得到的返回值为0,而如果我叫 getMeasuredHeight(),我得到的东西不为零,但不是真正的高度。如果我向下滚动,然后再次(取行之一的视图)的getHeight()== getMeasuredHeight()键,都具有正确的值。如果我只是更新mHeights在getView,因为我去(说 mHeights.set(位置,row.getHeight());它的工作原理 - 但只有在我已经滚动到在ListView的底部。我试着调用getView方法中row.measure(),但是这还是引起的getHeight()为0的第一次运行时

In getView, if I call getHeight() I get a return value of 0, while if I call getMeasuredHeight(), I get something non-zero but not the real height. If I scroll down and then up again (taking one of the rows out of view) getHeight() == getMeasuredHeight() and both have the correct value. If I just update mHeights in getView as I go (by saying mHeights.set(position, row.getHeight()); it works - but only after I've scrolled to the bottom of the ListView. I've tried calling row.measure() within the getView method, but this still causes getHeight() to be 0 the first time it is run.

我的问题是:如何计算并填充mHeights与ListView的行的正确的高度?我已经看到了一些类似的问题,但他们似乎并没有成为我在寻找什么。该 ViewTreeObserver 方法看起来有前途的,但我无法弄清楚如何强制调用getView(),从它(交替,遍历ListView中的行)。呼叫 adapter.notifyDataSetChanged()引起无限(虽然非阻塞)循环。

My question is this: how do I calculate and populate mHeights with the correct heights of the rows of the ListView? I've seen some similar questions, but they don't appear to be what I'm looking for. The ViewTreeObserver method seems promising, but I can't figure out how to force calls to getView() from it (alternately, to iterate the rows of the ListView). Calling adapter.notifyDataSetChanged() causes an infinite (although non-blocking) loop.

  1. 获取视图的实际高度
  2. 如何获得该项目的高度在列表视图?
  1. Get Actual Height of View
  2. how to get height of the item in a listview?

这是关系到关于这个问题,这似乎已经错过了点我的previous问题:的

This is related to my previous question on the subject, which seems to have missed the point: ListView distance from top of the list

推荐答案

我想通了,如何做到这一点。这是一个有点棘手,但它的作品。

I figured out how to do this. It's was a bit tricky, but it works.

精到越来越布局参数时知道他们。据我了解, getView()正在返回的视图的初始时间,所以当然就不能有一个高度,但 - 父母不知道的行但(因为 getView()的任务是吹吧),所以我们需要一个回调。

The subtlety was getting layout parameters when they are known. As I learned, getView() is returning the view for the initial time, so of course it can't have a height yet - the parent doesn't know about the row yet (since getView()'s job is to inflate it), so we need a callback.

什么可能的回调呢?事实证明,尽我可以告诉它的 OnLayoutChangeListener()该行的看法。因此,在 getView()在我的自定义 ArrayAdapter ,我添加了一个回调 onLayoutChanged 像这样:

What might that callback be? It turns out, as best I can tell it's the OnLayoutChangeListener() for the row's view. So in getView() in my custom ArrayAdapter, I added a callback for onLayoutChanged like so:

row.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    public void onLayoutChange(View v, int left, int top, int right,
            int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        ref.removeOnLayoutChangeListener(this);
        Log.d("CustomArrayAdapter", "onLayoutChange(" + position + "): height " + (bottom - top));
        mHeights.set(position, (bottom - top));
        if(position > 0) {
            mDistances.set(position, bottom - top + mDistances.get(position-1) + ((ListView)parent).getDividerHeight());
        }
        holderRef.distanceFromTop = mDistances.get(position);
        Log.d("CustomArrayAdapter", "New height for " + position + " is " + mHeights.get(position) + " Distance: " + mDistances.get(position));
    }
});

下面,被认为由返回getView()在适配器, mHeights 的ListView的每个元素的高度名单 mDistances 从像素列表视图顶部的距离列表 - 真实的东西,我想计算(也就是,如果你奠定了整个列表视图端到端,如何远离顶层元素,我将从一开始)。所有这些都存储在定制中的 ArrayAdapter 。这些列表初始化为0。

Here, row is the view to be returned by getView() in the adapter, mHeights is a list of the height of each element of the ListView, and mDistances is a list of the distance from the top of the list view in pixels - the real thing that I wanted to calculate (that is, if you laid out the entire list view end to end, how far from the top element i would be from the top). All of these are stored within the custom ArrayAdapter. These lists are initialized to zeroes.

添加此回调让我计算视图的高度。

Adding this callback allowed me to calculate the height of the view.

我的目标是有一个ListView,有一个背景下,与列表滚动。如果有人有兴趣,我把code可高达GitHub上,如果有人想回顾一下吧: https://github.com/mdkess / TexturedListView

My goal was to have a ListView that has a background that scrolls with the list. If anyone is interested, I put the code up on GitHub if anyone would like to review it: https://github.com/mdkess/TexturedListView

这篇关于计算一个ListView的每一行的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:35