ListView显示行重用的不同类型的行

时间:2015-08-04 20:52:40

标签: java android listview android-listview

我正在尝试显示两种不同类型的行(因为我的listView中有节标题),并确保重用行以提高性能。但是,我无法知道如何重用两种类型的单元而不会崩溃。这是我的适配器:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;

        // Check to see if this is a header or not
        Object item = items.get(position);
        if (item.getClass().equals(PUCObjects.PUCListSectionHeader.class)) {

            ViewHolderHeader holder = new ViewHolderHeader();
            if (row == null) {
                row = inflater.inflate(R.layout.section_header, parent, false);
                row.setTag(holder);
            } else {
                holder = (ViewHolderHeader) row.getTag();
            }

            holder.title = (TextView) row.findViewById(R.id.section_title);

            PUCObjects.PUCListSectionHeader header = (PUCObjects.PUCListSectionHeader)item;
            holder.title.setText(header.title);

        } else {

            ViewHolder holder = new ViewHolder();
            if (row == null) {
                row = inflater.inflate(R.layout.audio_list_cell, parent, false);
                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }

            holder.title = (TextView) row.findViewById(R.id.audioTitle);
            holder.subTitle = (TextView) row.findViewById(R.id.audioSubtitle);

            PUCObjects.PUCAudio audioItem = (PUCObjects.PUCAudio) item;
            holder.title.setText(audioItem.artist);

        }

        return row;
    }

    public class ViewHolder {
        TextView title;
        TextView subTitle;
    }

    public class ViewHolderHeader {
        TextView title;
    }

它在78行上崩溃,即holder = (ViewHolder) row.getTag();

视图将加载,但是当我滚动时,我收到错误:

FATAL EXCEPTION: main
    java.lang.ClassCastException: com.puc.mobile.audio.PUCAudioListSectionAdapter$ViewHolderHeader cannot be cast to com.puc.mobile.audio.PUCAudioListSectionAdapter$ViewHolder
            at com.puc.mobile.audio.PUCAudioListSectionAdapter.getView(PUCAudioListSectionAdapter.java:78)
            at android.widget.AbsListView.obtainView(AbsListView.java:2437)
            at android.widget.ListView.makeAndAddView(ListView.java:1775)
            at android.widget.ListView.fillDown(ListView.java:678)
            at android.widget.ListView.fillGap(ListView.java:642)
            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5491)
            at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3405)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3888)
            at android.view.View.dispatchTouchEvent(View.java:7246)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2179)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1914)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2185)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1928)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2116)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1469)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2477)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2064)
            at android.view.View.dispatchPointerEvent(View.java:7430)
            at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3457)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3389)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4483)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4461)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4565)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
            at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
            at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
            at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4533)
            at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4584)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:523)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

您应该实施getItemViewTypegetViewTypeCount方法。然后在getItemViewType中使用getItem来了解getView中当前的视图类型。以下是一个示例:http://learnncode.co/2014/01/21/listview-getviewtype-and-getviewtypecount-in-action/

答案 1 :(得分:1)

仅使用一种视图布局(以及显示/隐藏不需要的子视图)可以更快更简单的解决方案