如何使用ExpandableRecyclerView库具有多个(孩子)viewTypes? [英] How to use ExpandableRecyclerView library with multiple (child) viewTypes?

查看:1234
本文介绍了如何使用ExpandableRecyclerView库具有多个(孩子)viewTypes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像在这张照片可扩展RecyclerView活动:

在这里输入的形象描述

所以我使用 这个第三方库项目 那部分作品。

问题出现:当我做了我所描述在接下来的两个段落:

我的下一个要求是,我想不同的儿童行针对不同的父行。我复制(如果这是正确的字) 这个例子来创建多个儿童viewholders

举例说明:的想法基本上是有延伸的一个共同的childview持有人不同的儿童viewholders(对应于每个子行样式),然后我们ExpandableRecyclerAdapter在那里我们传递(在SSCCE parentItemList )的构造显示父行的数据的ArrayList里面,我们声明常数重新presenting所有子行类型(TYPE_EDITTEXT和TYPE_SPINNER在下面的SSCCE);然后在 getItemViewType(INT位置)我们比较从 parentItemList 数据项使用通过 INT位置为指标,对父母行,并在每一种情况下每个文本字符串,分配 viewType int变量一个类型常量,而 viewType 。然后这个 INT viewType 传递给 onCreateChildViewHolder onBindChildViewHolder ,因此,我们在这些方法的定义执行另一个开关块,然后在onCreateChildViewHolder我们返回一个对应ChildViewHolder(从相应的布局资源充气)(或者 R.layout.custom_row_child_with_edittext R.layout.custom_row_child_with_spinner 在SSCCE);和在 onBindChildViewHolder ,按照在开关块的情况下更新数据。

的问题是dollowing ClassCastException异常:

  07-26 18:21:54.380:E / AndroidRuntime(276):致命异常:主要
07-26 18:21:54.380:E / AndroidRuntime(276):java.lang.ClassCastException:tests.test.epmc_mobile.search_module.no_ui.expandable_recycler_view.EPMCChildViewHolder
07-26 18:21:54.380:E / AndroidRuntime(276):在com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter.onBindViewHolder(ExpandableRecyclerAdapter.java:144)

问题是,<一个href=\"https://github.com/bignerdranch/expandable-recycler-view/blob/master/expandablerecyclerview/src/main/java/com/bignerdranch/expandablerecyclerview/Adapter/ExpandableRecyclerAdapter.java\"相对=nofollow> ExpandableRecyclerAdapter.java 是第三方库项目的一部分,并线#144(与142和143,我加的)是:

  Log.i(TAG+++++++++++++++++++++++++ THE RecyclerView的类型。 ViewHolder已作为参数传递IS
+ holder.getClass()getSimpleName()+++++++++++++++++++++++++++++);
PVH parentViewHolder =(PVH)持有人;

在我试图做我上面做什么,它是工作的罚款。


MainActivity.java

 公共类MainActivity扩展FragmentActivity {
    私有静态最后弦乐TAG = MainActivity.class.getSimpleName();
    私人RecyclerView recyclerView;
    私有静态诠释[] = imageIds {R.drawable.ic_action_call,R.drawable.ic_action_copy,R.drawable.ic_action_discard};
    私有静态的String []标题= {虚拟文本一号,虚拟文本二,虚拟文本三};
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        Log.i(TAGMainActivity叫的onCreate。); //检查
        super.onCreate(savedInstanceState);        的setContentView(R.layout.activity_main);
        recyclerView =(RecyclerView)findViewById(R.id.mainActivity_recyclerView);        MyExpandableRecyclerAdapter myExpandableRecyclerAdapter =新MyExpandableRecyclerAdapter(这一点,populateDataList(本));
        recyclerView.setAdapter(myExpandableRecyclerAdapter);
        recyclerView.setLayoutManager(新LinearLayoutManager(本));
    }    私人的ArrayList&LT; ParentObject&GT; populateDataList(上下文的背景下){
        Log.i(TAGMainActivity称为populateDataList。); //检查
        ArrayList的&LT; ParentObject&GT; parentObjectList =新的ArrayList&LT;&GT;();
        的for(int i = 0; I&LT; imageIds.length&放大器;&安培; I&LT; titles.length;我++){
            MyCustomParentObject myCustomParentObject =新MyCustomParentObject(背景);
            myCustomParentObject.setImageId(imageIds [I]);
            myCustomParentObject.setTitle(标题[I]);
            parentObjectList.add(myCustomParentObject);
        }
        返回parentObjectList;
    }
}

MyExpandableRecyclerAdapter.java

 公共类MyExpandableRecyclerAdapter扩展ExpandableRecyclerAdapter&LT; ParentViewHolder,ChildViewHolder&GT; {
    私有静态最后弦乐TAG = MyExpandableRecyclerAdapter.class.getSimpleName();
    LayoutInflater layoutInflater;
    上下文语境;
    私人列表&LT; ParentObject&GT; parentItemList =新的ArrayList&LT;&GT;();    私有静态最终诠释TYPE_EDITTEXT = 0;
    私有静态最终诠释TYPE_SPINNER = 1;
    公共MyExpandableRecyclerAdapter(上下文的背景下,列表与LT; ParentObject&GT; parentObjectItemsList){
        超(背景下,parentObjectItemsList);
        Log.i(TAGMyExpandableRecyclerAdapter所谓的构造函数。); //检查
        layoutInflater = LayoutInflater.from(上下文);
        this.context =背景;
        parentItemList = parentObjectItemsList;
    }    @覆盖
    公众诠释getItemViewType(INT位置){
        INT viewType;
        如果(parentItemList.get(位置).equals(EditText上输入)){
            viewType = TYPE_EDITTEXT;
        }其他{
            viewType = TYPE_SPINNER;
        }
        返回viewType;
    }
    @覆盖
    公共MyParentViewHolder onCreateParentViewHolder(ViewGroup中的容器,诠释viewType){
        Log.i(TAGonCreateParentViewHolder MyExpandableRecyclerAdapter叫。); //检查
        Log.i(TAG,在onCreateParentViewHolder,中的项目parentItemList的类型是+ parentItemList.get(1));
        返回新MyParentViewHolder(layoutInflater.inflate(R.layout.custom_row_parent,集装箱,FALSE));
    }    @覆盖
    公共MyChildViewHolder onCreateChildViewHolder(ViewGroup中的容器,诠释viewType){
        Log.i(TAGonCreateChildViewHolder MyExpandableRecyclerAdapter叫。); //检查
        开关(viewType){
        案例TYPE_EDITTEXT:
            返回新MyChildViewHolder(layoutInflater.inflate(R.layout.custom_row_child_with_edittext,集装箱,假的),背景);
        案例TYPE_SPINNER:
            返回新MyChildViewHolder(layoutInflater.inflate(R.layout.custom_row_child_with_spinner,集装箱,假的),背景);
        默认:
            返回新MyChildViewHolder(layoutInflater.inflate(R.layout.custom_row_child_with_edittext,集装箱,假的),背景);
        }
    }    @覆盖
    公共无效onBindParentViewHolder(ParentViewHolder parentViewHolder,INT位置,对象parentObject){
        Log.i(TAGonBindParentViewHolder MyExpandableRecyclerAdapter叫。); //检查
        MyParentViewHolder myParentViewHolder =(MyParentViewHolder)parentViewHolder;
        MyCustomParentObject myCustomParentObject =(MyCustomParentObject)parentObject;
        myParentViewHolder.textView.setText(myCustomParentObject.getTitle());
        myParentViewHolder.imageView.setImageResource(myCustomParentObject.getImageId());
    }    @覆盖
    公共无效onBindChildViewHolder(ChildViewHolder childViewHolder,INT位置,对象childObject){
        Log.i(TAGonBindChildViewHolder MyExpandableRecyclerAdapter叫。); //检查        开关(childViewHolder.getItemViewType()){
        案例TYPE_EDITTEXT:
            MyChildViewHolderWithEditText myChildViewHolderWithEditText =(MyChildViewHolderWithEditText)childViewHolder;
            myChildViewHolderWithEditText.textView.setText(TextView的+(位置+ 1)+标题);
        案例TYPE_SPINNER:
            MyCustomChildObject myCustomChildObject =(MyCustomChildObject)childObject;
            ArrayAdapter arrayAdapter =新ArrayAdapter(上下文,android.R.layout.simple_list_item_1,myCustomChildObject.getSpinnerItems());
            arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            MyChildViewHolderWithSpinner myChildViewHolderWithSpinner =(MyChildViewHolderWithSpinner)childViewHolder;
            myChildViewHolderWithSpinner.spinner.setAdapter(arrayAdapter);
        }
    }}

MyCustomParentObject.java

 公共类MyCustomParentObject实现ParentObject {
    私有静态最后弦乐TAG = MyCustomParentObject.class.getSimpleName();
    //列表存储在父对象的所有儿童。
    私人列表&LT;对象&gt; childObjectList;
    私有String []围兜;    MyCustomParentObject(上下文的背景下){
        超();
        围兜= context.getResources()getStringArray(R.array.spinner_options)。
    }
    @覆盖
    公开名单&LT;对象&gt; getChildObjectList(){
        Log.i(TAGMyCustomParentObject称为getChildObjectList。); //检查
        //你可以回到这里的孩子一个新创建的列表或更高附加它们
        返回populateChildObjectList();
    }    @覆盖
    公共无效setChildObjectList(列表&LT;对象&gt; childObjectList){
        Log.i(TAGMyCustomParentObject的setChildObjectList叫。); //检查
        childObjectList = childObjectList;
    }    私人列表&LT;对象&gt; populateChildObjectList(){
        Log.i(TAGMyCustomParentObject称为populateChildObjectList。); //检查
        childObjectList =新的ArrayList&LT;&GT;();
        MyCustomChildObject myCustomChildObject =新MyCustomChildObject();
        myCustomChildObject.setSpinnerItems(BIB);
        对象myCustomChildObjectCasted =(对象)myCustomChildObject;
        childObjectList.add(myCustomChildObjectCasted);
        返回childObjectList;
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    私人诠释imageId;
    私人字符串称号;
    公众诠释getImageId(){
        Log.i(TAGMyCustomParentObject称为getImageId。); //检查
        返回imageId;
    }
    公共无效setImageId(INT imageId){
        Log.i(TAGMyCustomParentObject的setImageId叫。); //检查
        this.imageId = imageId;
    }
    公共字符串的getTitle(){
        Log.i(TAGMyCustomParentObject叫的getTitle。); //检查
        返回称号;
    }
    公共无效的setTitle(字符串名称){
        Log.i(TAGMyCustomParentObject的所谓的setTitle); //检查
        this.title =称号;
    }}

MyCustomChildObject.java

 公共类MyCustomChildObject {
    私有静态最后弦乐TAG = MyCustomParentObject.class.getSimpleName();    私有String [] spinnerItems;    公众的String [] getSpinnerItems(){
        返回spinnerItems;
    }    公共无效setSpinnerItems(字符串[] spinnerItems){
        this.spinnerItems = spinnerItems;
    }}

MyCustomParentViewHolder.java

 公共类MyParentViewHolder扩展ParentViewHolder {
    私有静态最后弦乐TAG = MyParentViewHolder.class.getSimpleName();
    TextView中的TextView;
    ImageView的ImageView的;    公共MyParentViewHolder(查看ItemView控件){
        超(ItemView控件);
        Log.i(TAGMyParentViewHolder所谓的构造函数。); //检查
        的TextView =(TextView中)itemView.findViewById(R.id.parentCustomRow_textView);
        ImageView的=(ImageView的)itemView.findViewById(R.id.parentCustomRow_imageView);
    }}

MyCustomChildViewHolder.java

 公共类MyChildViewHolder扩展ChildViewHolder {
    私有静态最后弦乐TAG = MyChildViewHolder.class.getSimpleName();    公共MyChildViewHolder(查看ItemView控件,最终上下文的背景下){
        超(ItemView控件);
        Log.i(TAGMyChildViewHolder所谓的构造函数。); //检查
    }
}

MyCustomChildViewHolderWithEditText.java

 公共类MyChildViewHolderWithEditText扩展MyChildViewHolder {
    EDITTEXT的EditText;
    TextView中的TextView;    公共MyChildViewHolderWithEditText(查看ItemView控件,上下文的背景下){
        超(ItemView控件,背景);        的TextView =(TextView中)itemView.findViewById(R.id.childViewHolderWithEditText_TextView);
        EDITTEXT =(EditText上)itemView.findViewById(R.id.childViewHolderWithEditText_editText);
    }}

MyCustomChildViewHolderWithSpinner.java

 公共类MyChildViewHolderWithSpinner扩展MyChildViewHolder {
    微调微调;    公共MyChildViewHolderWithSpinner(查看ItemView控件,上下文的背景下){
        超(ItemView控件,背景);        微调=(微调)itemView.findViewById(R.id.childViewHolderWithSpinner_spinner);
        // ArrayAdapter&LT;&CharSequence的GT;适配器= ArrayAdapter.createFromResource(背景下,R.array.childViewSpinnerFields,android.R.layout.simple_spinner_item);
        //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ///spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(新OnItemSelectedListener(){
            @覆盖
            公共无效onItemSelected(适配器视图&LT;&GT;母公司,观景,INT位置,长的id){}
            @覆盖
            公共无效onNothingSelected(适配器视图&LT;&GT;母公司){}
        });
    }}


解决方案

通过比较这个堆栈跟踪线#13无错程序这一项导致异常的程序,并通过查看 onCreateViewHolder(ViewGroup中ViewGroup中,诠释viewType)方法上<定义href=\"https://github.com/bignerdranch/expandable-recycler-view/blob/master/expandablerecyclerview/src/main/java/com/bignerdranch/expandablerecyclerview/Adapter/ExpandableRecyclerAdapter.java\"相对=nofollow>#线这个 ExpandableRecyclerAdapter.java 类,从中我推断117 onCreateViewHolder 被传递了错误的 viewType 参数。

但我仍未能找出原因!


我觉得我已经想通了。

其实,我用 MyExpandableRecyclerAdapter TYPE_SOMETHING 常量的诀窍是他们在已经使用的同一个 ExpandableRecyclerAdapter ,也就是在我的 MyExpandableRecyclerAdapter 0 是值 TYPE_EDITTEXT 1 TYPE_SPINNER 的价值;而在他们的 ExpandableRecyclerAdapter ,这是我的 MyExpandableRecyclerAdapter 0 是价值 TYPE_PARENT 1 TYPE_CHILD

要如果Android框架通过同样的 viewType 参数 onCreateViewHolder 这是从返回getItemViewType(INT位置),它会通过 1 如果是 TYPE_SPINNER 在我的 MyExpandableRecyclerAdapter ,但他们的 ExpandableRecyclerAdapter 将它理解为 TYPE_CHILD ,和因此调用 onCreateChildViewHolder

所以我觉得我试图实现整个事情不能做。 =(

I need an Activity with an expandable RecyclerView like the one in this picture:

So I am using this third party library project . That part works.

The problem arose when I did what I am describing in the next two paragraphs:

My next requirement is that I want different child-rows for different parent-rows. I am reproducing (if that's the right word) this example to create multiple child-viewholders.

Description of Example: The idea is basically to have the different Child-viewholders (corresponding to each child-row style) extending one common childview-holder, and then inside our ExpandableRecyclerAdapter where we are passed the ArrayList of the data displayed in parent-rows in the constructor (parentItemList in the SSCCE), we declare constants representing all the child-row-types (TYPE_EDITTEXT and TYPE_SPINNER in the SSCCE below); and then inside getItemViewType(int position) we compare the data item from parentItemList using the passed int position as the index, with each text string on parent-rows and in each case, assign the viewType int variable a TYPE constant, and the viewType.Then this int viewType is passed to the onCreateChildViewHolder and onBindChildViewHolder, so we execute another switch-block in the definitions of each of these methods and then in onCreateChildViewHolder we return a corresponding ChildViewHolder (inflated from a corresponding layout resource) (either R.layout.custom_row_child_with_edittext or R.layout.custom_row_child_with_spinner in the SSCCE) ; and in onBindChildViewHolder, update the data according to the case in the switch-block.

The problem is the dollowing ClassCastException:

07-26 18:21:54.380: E/AndroidRuntime(276): FATAL EXCEPTION: main
07-26 18:21:54.380: E/AndroidRuntime(276): java.lang.ClassCastException: tests.test.epmc_mobile.search_module.no_ui.expandable_recycler_view.EPMCChildViewHolder
07-26 18:21:54.380: E/AndroidRuntime(276):      at com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter.onBindViewHolder(ExpandableRecyclerAdapter.java:144)

The thing is that ExpandableRecyclerAdapter.java is a part of the third party library project, and Line#144 (and 142 and 143 which I added) are:

Log.i(TAG, "+++++++++++++++++++++++++THE TYPE OF THE RecyclerView.ViewHolder WHICH HAS BEEN PASSED AS AN ARGUMENT IS "
                    + holder.getClass().getSimpleName() + "++++++++++++++++++++++++++++");
            PVH parentViewHolder = (PVH) holder;

Before I tried to do what I am doing above, it was working fine.


MainActivity.java

public class MainActivity extends FragmentActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private RecyclerView recyclerView;
    private static int [] imageIds = {R.drawable.ic_action_call, R.drawable.ic_action_copy, R.drawable.ic_action_discard};
    private static String [] titles = {"Dummy Text One", "Dummy Text Two", "Dummy Text Three"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate of MainActivity called.");//check
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.mainActivity_recyclerView);

        MyExpandableRecyclerAdapter myExpandableRecyclerAdapter = new MyExpandableRecyclerAdapter(this, populateDataList(this));
        recyclerView.setAdapter(myExpandableRecyclerAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    }

    private ArrayList<ParentObject> populateDataList(Context context) {
        Log.i(TAG, "populateDataList of MainActivity called.");//check
        ArrayList<ParentObject> parentObjectList = new ArrayList<>();
        for (int i=0; i<imageIds.length && i<titles.length; i++) {
            MyCustomParentObject myCustomParentObject = new MyCustomParentObject(context);
            myCustomParentObject.setImageId(imageIds[i]);
            myCustomParentObject.setTitle(titles[i]);
            parentObjectList.add(myCustomParentObject);
        } 
        return parentObjectList;
    }
}

MyExpandableRecyclerAdapter.java

public class MyExpandableRecyclerAdapter extends ExpandableRecyclerAdapter<ParentViewHolder, ChildViewHolder> {
    private static final String TAG = MyExpandableRecyclerAdapter.class.getSimpleName();
    LayoutInflater layoutInflater;
    Context context;
    private List<ParentObject> parentItemList = new ArrayList<>();

    private static final int TYPE_EDITTEXT = 0;
    private static final int TYPE_SPINNER = 1;


    public MyExpandableRecyclerAdapter(Context context, List<ParentObject> parentObjectItemsList) {
        super(context, parentObjectItemsList);
        Log.i(TAG, "Constructor of MyExpandableRecyclerAdapter called.");//check
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
        parentItemList = parentObjectItemsList;
    }



    @Override
    public int getItemViewType(int position) {
        int viewType;
        if (parentItemList.get(position).equals("EditText Entry")) {
            viewType = TYPE_EDITTEXT;
        } else {
            viewType = TYPE_SPINNER;
        }
        return viewType; 
    }




    @Override
    public MyParentViewHolder onCreateParentViewHolder(ViewGroup container, int viewType) {
        Log.i(TAG, "onCreateParentViewHolder of MyExpandableRecyclerAdapter called.");//check
        Log.i(TAG, "IN onCreateParentViewHolder, THE TYPE OF AN ITEM IN THE parentItemList IS " + parentItemList.get(1));
        return new MyParentViewHolder(layoutInflater.inflate(R.layout.custom_row_parent, container, false));
    }



    @Override
    public MyChildViewHolder onCreateChildViewHolder(ViewGroup container, int viewType) {
        Log.i(TAG, "onCreateChildViewHolder of MyExpandableRecyclerAdapter called.");//check
        switch(viewType) {
        case TYPE_EDITTEXT:
            return new MyChildViewHolder(layoutInflater.inflate(R.layout.custom_row_child_with_edittext, container, false), context);
        case TYPE_SPINNER:
            return new MyChildViewHolder(layoutInflater.inflate(R.layout.custom_row_child_with_spinner, container, false), context);
        default:
            return new MyChildViewHolder(layoutInflater.inflate(R.layout.custom_row_child_with_edittext, container, false), context);
        }
    }



    @Override
    public void onBindParentViewHolder(ParentViewHolder parentViewHolder, int position, Object parentObject) {
        Log.i(TAG, "onBindParentViewHolder of MyExpandableRecyclerAdapter called.");//check
        MyParentViewHolder myParentViewHolder = (MyParentViewHolder) parentViewHolder;
        MyCustomParentObject myCustomParentObject = (MyCustomParentObject) parentObject;
        myParentViewHolder.textView.setText(myCustomParentObject.getTitle());
        myParentViewHolder.imageView.setImageResource(myCustomParentObject.getImageId()); 
    }



    @Override
    public void onBindChildViewHolder(ChildViewHolder childViewHolder, int position, Object childObject) {
        Log.i(TAG, "onBindChildViewHolder of MyExpandableRecyclerAdapter called.");//check

        switch(childViewHolder.getItemViewType()) {
        case TYPE_EDITTEXT:
            MyChildViewHolderWithEditText myChildViewHolderWithEditText = (MyChildViewHolderWithEditText) childViewHolder;
            myChildViewHolderWithEditText.textView.setText("TextView " + (position+1) + " Title"); 
        case TYPE_SPINNER:
            MyCustomChildObject myCustomChildObject = (MyCustomChildObject) childObject;
            ArrayAdapter arrayAdapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, myCustomChildObject.getSpinnerItems());
            arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            MyChildViewHolderWithSpinner myChildViewHolderWithSpinner = (MyChildViewHolderWithSpinner) childViewHolder;
            myChildViewHolderWithSpinner.spinner.setAdapter(arrayAdapter);
        }
    }

}

MyCustomParentObject.java

public class MyCustomParentObject implements ParentObject {
    private static final String TAG = MyCustomParentObject.class.getSimpleName();
    //List to store all the children of the parent object in.
    private List<Object> childObjectList;
    private String [] bib;

    MyCustomParentObject (Context context) {
        super();
        bib = context.getResources().getStringArray(R.array.spinner_options);
    }


    @Override
    public List<Object> getChildObjectList() {
        Log.i(TAG, "getChildObjectList of MyCustomParentObject called.");//check
        //"You can either return a newly created list of children here or attach them later"
        return populateChildObjectList();
    }

    @Override
    public void setChildObjectList(List<Object> childObjectList) {
        Log.i(TAG, "setChildObjectList of MyCustomParentObject called.");//check
        childObjectList = childObjectList;
    }

    private List<Object> populateChildObjectList() {
        Log.i(TAG, "populateChildObjectList of MyCustomParentObject called.");//check
        childObjectList = new ArrayList<>();
        MyCustomChildObject myCustomChildObject = new MyCustomChildObject();
        myCustomChildObject.setSpinnerItems(bib);
        Object myCustomChildObjectCasted = (Object) myCustomChildObject;
        childObjectList.add(myCustomChildObjectCasted);
        return childObjectList;
    }


    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private int imageId;
    private String title;
    public int getImageId() {
        Log.i(TAG, "getImageId of MyCustomParentObject called.");//check
        return imageId;
    }
    public void setImageId(int imageId) {
        Log.i(TAG, "setImageId of MyCustomParentObject called.");//check
        this.imageId = imageId;
    }
    public String getTitle() {
        Log.i(TAG, "getTitle of MyCustomParentObject called.");//check
        return title;
    }
    public void setTitle(String title) {
        Log.i(TAG, "setTitle of MyCustomParentObject called.");//check
        this.title = title;
    }

}

MyCustomChildObject.java

public class MyCustomChildObject {
    private static final String TAG = MyCustomParentObject.class.getSimpleName();

    private String [] spinnerItems;

    public String [] getSpinnerItems() {
        return spinnerItems;
    }

    public void setSpinnerItems(String [] spinnerItems) {
        this.spinnerItems = spinnerItems;
    }

}

MyCustomParentViewHolder.java

public class MyParentViewHolder extends ParentViewHolder {
    private static final String TAG = MyParentViewHolder.class.getSimpleName();
    TextView textView;
    ImageView imageView;

    public MyParentViewHolder(View itemView) {
        super(itemView);
        Log.i(TAG, "Constructor of MyParentViewHolder called.");//check
        textView = (TextView) itemView.findViewById(R.id.parentCustomRow_textView);
        imageView = (ImageView) itemView.findViewById(R.id.parentCustomRow_imageView);
    }

}

MyCustomChildViewHolder.java

public class MyChildViewHolder extends ChildViewHolder {
    private static final String TAG = MyChildViewHolder.class.getSimpleName();

    public MyChildViewHolder(View itemView, final Context context) {
        super(itemView);
        Log.i(TAG, "Constructor of MyChildViewHolder called.");// check
    }
}

MyCustomChildViewHolderWithEditText.java

public class MyChildViewHolderWithEditText extends MyChildViewHolder {
    EditText editText;
    TextView textView;

    public MyChildViewHolderWithEditText(View itemView, Context context) {
        super(itemView, context);

        textView = (TextView) itemView.findViewById(R.id.childViewHolderWithEditText_TextView);
        editText = (EditText) itemView.findViewById(R.id.childViewHolderWithEditText_editText);
    }

}

MyCustomChildViewHolderWithSpinner.java

public class MyChildViewHolderWithSpinner extends MyChildViewHolder {
    Spinner spinner;

    public MyChildViewHolderWithSpinner(View itemView, Context context) {
        super(itemView, context);

        spinner = (Spinner) itemView.findViewById(R.id.childViewHolderWithSpinner_spinner);
        //ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.childViewSpinnerFields, android.R.layout.simple_spinner_item);
        //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ///spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {}
            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
    }

}

解决方案

By comparing Line#13 of this stack-trace of the error free program, and this one of the program which results in the exception, and by looking at the onCreateViewHolder(ViewGroup viewGroup, int viewType) method definition on Line#117 of this ExpandableRecyclerAdapter.java class, from which I infer that onCreateViewHolder is being passed the wrong viewType parameter.

But I haven't yet been able to figure out why!


I think I have figured it out.

Actually the trick of TYPE_SOMETHING constants that I am using in MyExpandableRecyclerAdapter is the same one they have used in ExpandableRecyclerAdapter, i.e. in my MyExpandableRecyclerAdapter, 0 is the value of TYPE_EDITTEXT and 1 is the value of TYPE_SPINNER; whereas in their ExpandableRecyclerAdapter, which is extended by my MyExpandableRecyclerAdapter, 0 is the value of TYPE_PARENT and 1 is the value of TYPE_CHILD.

If the Android framework passes the same viewType parameter to onCreateViewHolder which is returned from getItemViewType(int position), it will pass 1 if it was TYPE_SPINNER in my MyExpandableRecyclerAdapter, but their ExpandableRecyclerAdapter will understand it as TYPE_CHILD, and thus call onCreateChildViewHolder.

So I think the whole thing I am trying to achieve can't be done. =(

这篇关于如何使用ExpandableRecyclerView库具有多个(孩子)viewTypes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆