Поместите пользовательский вид в другой

1

Мне нужно расширить ListView для создания настраиваемого компонента. Я хотел бы положить кнопку поиска в левом нижнем слое. Когда я запускаю ошибку генерации в onMessure.

Этот код является фрагментом, исходный код имеет много других вещей.

Я пытаюсь изменить родительский контур ViewGroup на null, но получаю ту же ошибку.

mPainelView = inflater.inflate(R.layout.longlist_painel, null);

Пользовательский вид:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageButton
    android:id="@+id/longlist_painel_seek"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@android:drawable/ic_menu_search" /></FrameLayout>

Расширенный ListView:

public class List2 extends ListView {
    private View mPainelView;

    private int mPainelViewWidth;
    private int mPainelViewHeight;

    public List2(Context context) {
        super(context);
        createPainel();
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mPainelView != null) {
            measureChild(mPainelView, widthMeasureSpec, heightMeasureSpec);
            mPainelViewWidth = mPainelView.getMeasuredWidth();
            mPainelViewHeight = mPainelView.getMeasuredHeight();
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (mPainelView != null) {
            mPainelView.layout(0, this.getBottom()-mPainelViewHeight,  mPainelViewWidth, mPainelViewHeight);
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        drawChild(canvas, mPainelView, getDrawingTime());
    }


    private void createPainel(){
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mPainelView = inflater.inflate(R.layout.longlist_painel, this);
    }
}

Ошибка:

java.lang.NullPointerException
at android.view.ViewGroup.measureChild(ViewGroup.java:3098)
at com.rodrigo.comp.LongList2.onMeasure(List2.java:40)
at android.view.View.measure(View.java:8173)
Теги:
android-listview

2 ответа

0

сначала мой английский не является моим родным языком, я одновременно столкнулся с одной и той же проблемой, поэтому я искал это и нашел u, исключение nullpointer выбрасывается, когда оно вызывает "measurChild", я обнаружил, что ваш mPainelView layoutParams имеет значение null, поэтому исключение выбрано.

если u измените метод надувания на "inflater.inflate(R.layout.longlist_painel, yourlistview, false)";

u может решить эту проблему

0

Вам не нужно расширять ListView, вы можете помещать взгляды друг на друга с RelativeLayout:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    >
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        />        
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:padding="10dp"
        android:text="Search"
        />
</RelativeLayout>
  • 0
    Этот код является примером. У меня есть расширенный ListView с множеством переопределений для других вещей.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню