Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView:在滚动条顶部显示固定标题(或使用dispatchDraw在另一个顶部绘制视图)_Android_Android Layout_Listview_Android Listview - Fatal编程技术网

Android ListView:在滚动条顶部显示固定标题(或使用dispatchDraw在另一个顶部绘制视图)

Android ListView:在滚动条顶部显示固定标题(或使用dispatchDraw在另一个顶部绘制视图),android,android-layout,listview,android-listview,Android,Android Layout,Listview,Android Listview,我正在尝试实现上面的示例应用程序中可以看到的效果: 基本上,我需要在ListView的顶部显示一个单一的、静态的、固定的标题视图,但要在滚动条下方显示。我不需要任何与章节或字母索引相关的东西 基于的源代码,我不知道如何做到这一点。我尝试子类化ListView并重写dispatchDraw(),如下所示: protected void dispatchDraw(Canvas canvas) { View view = LayoutInflater.from(getContext()).in

我正在尝试实现上面的示例应用程序中可以看到的效果:

基本上,我需要在ListView的顶部显示一个单一的、静态的、固定的标题视图,但要在滚动条下方显示。我不需要任何与章节或字母索引相关的东西

基于的源代码,我不知道如何做到这一点。我尝试子类化
ListView
并重写
dispatchDraw()
,如下所示:

protected void dispatchDraw(Canvas canvas)
{
    View view = LayoutInflater.from(getContext()).inflate(R.layout.header, this, false);
    drawChild(canvas, view, getDrawingTime());
    super.dispatchDraw(canvas);
}

但它不起作用,没有画标题。

回答我自己的问题。这个ListView子类可以做我想做的事情。列表的第一个元素可以成为固定的调用
showFixedHeader()


虽然没有经过严格的测试,但这是一个很好的起点。

感谢您的反驳=)
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

public class FixedHeaderListView extends ListView
{
    private View fixedHeader = null;
    private boolean fixedHeaderLayoutDone = false;
    private boolean showFixedHeader = true;

    @SuppressWarnings("unused")
    public FixedHeaderListView(Context context)
    {
        super(context);
    }

    @SuppressWarnings("unused")
    public FixedHeaderListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @SuppressWarnings("unused")
    public FixedHeaderListView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public void showFixedHeader(boolean show)
    {
        this.showFixedHeader = show;
        requestLayout(); // Will cause layoutChildren() and dispatchDraw() to be called
    }

    @Override
    protected void layoutChildren()
    {
        super.layoutChildren();

        if (!fixedHeaderLayoutDone)
        {
            ListAdapter adapter = getAdapter();
            if (adapter != null && adapter.getCount() > 0)
            {
                // Layout the first item in the adapter's data set as the fixed header
                fixedHeader = adapter.getView(0, null, this);
                if (fixedHeader != null)
                {
                    // Measure and layout

                    LayoutParams layoutParams = (LayoutParams)fixedHeader.getLayoutParams();
                    if (layoutParams == null)
                    {
                        layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    }

                    int heightMode = MeasureSpec.getMode(layoutParams.height);
                    if (heightMode == MeasureSpec.UNSPECIFIED)
                    {
                        heightMode = MeasureSpec.EXACTLY;
                    }

                    int heightSize = MeasureSpec.getSize(layoutParams.height);
                    int maxHeight = getHeight() - getListPaddingTop() - getListPaddingBottom();
                    if (heightSize > maxHeight)
                    {
                        heightSize = maxHeight;
                    }

                    int widthSpec = MeasureSpec.makeMeasureSpec(getWidth() - getListPaddingLeft() - getListPaddingRight(), MeasureSpec.EXACTLY);
                    int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
                    fixedHeader.measure(widthSpec, heightSpec);
                    fixedHeader.layout(0, 0, fixedHeader.getMeasuredWidth(), fixedHeader.getMeasuredHeight());

                    // Flag as layout done
                    fixedHeaderLayoutDone = true;
                }
            }
        }
    }


    @Override @SuppressWarnings("NullableProblems")
    protected void dispatchDraw(Canvas canvas)
    {
        super.dispatchDraw(canvas);

        if (fixedHeader != null && showFixedHeader)
        {
            drawChild(canvas, fixedHeader, getDrawingTime());
        }
    }

}