ListView仅在附加HierarchyViewer时出现

roc*_*k3r 1 android android-ui hierarchyviewer

我有一个扩展ListView的自定义.列表以这样的布局定义:AdapterCursorAdapter

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <ProgressBar
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/ProgressBar.Itasa.Indeterminate.Large"/>

    <net.italiansubs.droitasa.widget.HookedListView
            android:id="@android:id/list"
            style="@style/NewsList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

HookedListView如下所示:

package net.italiansubs.droitasa.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ListView;
import net.italiansubs.droitasa.util.OnLayoutChangedListener;

/**
 * An extended ListView that has an hook for getting layout changes.
 *
 * @author Sebastiano Poggi, Francesco Pontillo
 */
public class HookedListView extends ListView {

    private OnLayoutChangedListener mLayoutListener;
    private final Handler mHandler;
    private boolean mFirstLayout = true;

    public HookedListView(Context context) {
        super(context);
        mHandler = new Handler(context.getMainLooper());
    }

    public HookedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHandler = new Handler(context.getMainLooper());
    }

    public HookedListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mHandler = new Handler(context.getMainLooper());
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        if (mFirstLayout) {
            // In this case we want to avoid posting, because this could
            // have the two layout passes happen one after the other with
            // a pause between each other, with the effects, for example,
            // of having the number of columns change
            mFirstLayout = false;

            new LayoutChangedRunnable().run();
        }
        else if (changed) {
            mHandler.postAtFrontOfQueue(new LayoutChangedRunnable());
        }
    }

    /**
     * Sets the OnLayoutChangedListener for this instance, that will
     * receive callbacks after layout changes for the View.
     *
     * @param l The new OnLayoutChangedListener, or null to remove
     *          the current listener.
     */
    public void setOnLayoutChangedListener(OnLayoutChangedListener l) {
        mLayoutListener = l;
    }

    /**
     * Gets the OnLayoutChangedListener set for this instance.
     *
     * @return Returns the instance's OnLayoutChangedListener, if any.
     */
    public OnLayoutChangedListener getOnLayoutChangedListener() {
        return mLayoutListener;
    }

    private class LayoutChangedRunnable implements Runnable {

        @Override
        public void run() {
            // We don't need to synchronize on the listener because the handler
            // is hooked up to the main thread's looper so we shouldn't be
            // called from other threads.

            if (mLayoutListener != null) {
                mLayoutListener.onLayoutChanged(HookedListView.this);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是ListView内容没有出现在设备上,即使Cursor有项目,Adapter有项目,ListView本身也有项目.将HierarchyViewer附加到窗口确实使ListView项目显示而无需任何进一步的干预,并且同样请求从webservice更新数据,这反过来更新了web服务的内容ContentProvider.我已经尝试为the Cursor和the 设置内容观察器Adapter,并且记录以确保设置了正确的项目数Adapter,并且它们都按预期工作.由于我们的自定义MultiCoumnAdapter,我们已经在List上执行了一个额外的布局轮次,它计算第一个之后的列数ListView布局通过(因此需要a HookedListView).

我尝试过使用标准CursorAdapter,而不是我自己的标准MultiColumnAdapter(模仿一个GridView,没有它的所有限制和错误),但这并没有改变任何东西.

Fragment是唯一的内容Activity; 它Activity本身基本上什么都不做,只是从Intent用于启动它的附加项中获取项ID ,并将其传递给它Fragment,它在Activity布局XML中定义为唯一元素.

这样Fragment做只是处理ContentProvider使用标准从应用程序获取数据CursorLoader.我们使用DataDroid来管理从Web服务获取新数据.

有没有人对这里发生的事情有丝毫的线索?看起来似乎陷在ListView自己的代码,直到我们更新底层Cursor...

Chr*_*nes 5

你在ListView上搞乱onLayout的事实会敲响警钟.为什么不使用带有OnGlobalLayoutListener的标准ListView ?

或者,如果您需要自定义ListView,请尝试将您更改onLayout()为:

final LayoutChangedRunnable mLayoutRunnable = new LayoutChangedRunnable()

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    if (changed) {
        post(mLayoutRunnable);
    }
}
Run Code Online (Sandbox Code Playgroud)