如何使用不同的子布局处理Expandablelistview中的回收?

时间:2014-12-31 13:30:51

标签: android expandablelistview recycle

众所周知,Android的listview和expandablelistview会回收它的观点。更多信息:The nuance of android listview

我已经阅读了文章和信息,例如上面关于如何处理的链接,但他们只给出列表视图上的示例而不是 Expandablelistview (而getView是listview中唯一的方法expandablelistview有getGroupView和getChildView)。

我的可扩展listview的子项已经或需要具有不同的布局,因为这是实现应用程序输出所必需的。

现在,我的问题是如何正确处理可扩展列表视图的回收,这会扩大其子项中不同的布局?比如在我的expandablelistview的getChildView方法中放置 viewHolder convertView 的位置。

我按照下面的代码site来实现一个根据其位置膨胀不同布局的孩子。

每当我点击另一个组时。前一组的子项将其视图重新循环到其默认值,因此不会保留微调器和编辑文本值。我想也许是因为它再次膨胀了观点?

以下是我的 getChildView 方法:

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

            //layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
            //convertView = layoutInflater.inflate(R.layout.expandable_child_layout, parent, false);
        try{
            int itemType = getChildType(groupPosition, childPosition);

            switch(itemType){
            case 0:
                ViewHolder mainHolder = null;
                View v1 = null;
                if(v1 == null){

                    mainHolder = new ViewHolder();
                    v1 = View.inflate(context, R.layout.newtrip_main, null);
                    v1.setTag(mainHolder);

                    mainHolder.tripType = (Spinner) v1.findViewById(R.id.spinTripType);

                    /*TripTypeAdapter = new ArrayAdapter<String>(NewTrip.this, android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.triptypes)); //selected item will look like a spinner set from XML
                    TripTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    tripType.setAdapter(TripTypeAdapter);*/
                    mainHolder.tripType.setOnItemSelectedListener(this);

                }//else{
                    //mainHolder = (ViewHolder) v1.getTag();
                //}
                return v1;
                //break;
            case 1:

                ViewHolder  budgetHolder = null;
                View v2 = null;
                if(v2 == null){

                    budgetHolder = new ViewHolder();
                    v2 = View.inflate(context, R.layout.newtrip_budget, null);
                    v2.setTag(budgetHolder);

                    budgetHolder.budget = (Spinner) v2.findViewById(R.id.spinBudget);
                    budgetHolder.budgetAdapter = new ArrayAdapter<String>(NewTrip.this, android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.budgettype)); //selected item will look like a spinner set from XML
                    budgetHolder.budgetAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    budgetHolder.budget.setAdapter(budgetHolder.budgetAdapter);
                    budgetHolder.budget.setOnItemSelectedListener(this);

                }

                return v2;
                //break;
            case 2:

                ViewHolder  foodHolder= null;
                View v3 = null;
                if(v3 == null){
                    foodHolder = new ViewHolder();
                    v3 = View.inflate(context, R.layout.newtrip_food, null);
                    v3.setTag(foodHolder);
                }
                return v3;
                //break;
            case 3:

                ViewHolder  accomHolder= null;
                View v4 = null;
                if(v4 == null){
                    accomHolder = new ViewHolder();
                    v4 = View.inflate(context, R.layout.newtrip_accommodation, null);
                    v4.setTag(accomHolder);

                    accomHolder.accommodation = (Spinner) v4.findViewById(R.id.spinAccommodation);

                    accomHolder.accommodationAdapter = new ArrayAdapter<String>(NewTrip.this, android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.economicalAccomodation)); //selected item will look like a spinner set from XML
                    accomHolder.accommodationAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    accomHolder.accommodation.setAdapter(accomHolder.accommodationAdapter);
                    //return convertView;
                }

                return v4;
                //break;

            }
        }catch(Exception e){
            Toast toast = Toast.makeText(context, e.toString(), Toast.LENGTH_LONG);
            toast.show();
        }
            convertView.invalidate();
            return convertView;
    }

这是 getGroupView 方法:

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {          


        convertView = View.inflate(context, R.layout.expandable_group_layout, null);
        TextView txtView = (TextView) convertView.findViewById(R.id.txt1);                 

        if(groupPosition == 0) {
          txtView.setText("   MAIN");
          txtView.setTextSize(15f);
          }
        if(groupPosition == 1) {
            txtView.setText("   BUDGET");
            txtView.setTextSize(15f);
          }
        if(groupPosition == 2) {
            txtView.setText("   FOOD");
            txtView.setTextSize(15f);
          }
        if(groupPosition == 3) {
            txtView.setText("   ACCOMMODATION");
            txtView.setTextSize(15f);
          }

        convertView.invalidate();
        return convertView;

    }

3 个答案:

答案 0 :(得分:0)

这是完全一样的。只需按照与普通getChildView相同的方式处理getView方法即可。因此,对于您的情况,您需要执行以下操作:

@Override
public int getChildType(int groupPosition, int childPosition) {
    return groupPosition;
}

@Override
public int getChildTypeCount() {
    return getGroupCount();
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    switch (getChildType(groupPosition, childPosition) {
    case 1:
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            //Inflate your view
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        //Load any data into view
        return convertView;
    case 2:
        //Repeat for all expected types
    }
}

答案 1 :(得分:0)

要控制可扩展列表视图中子列表的回收,请确保将更改的数据保存在哈希映射中,以便在向上滚动或解析哈希映射数据时可以跟踪已更改的数据列表,你可以看到结果。

答案 2 :(得分:0)

我遇到了几乎相同的问题,我用这个来解决:https://stackoverflow.com/a/28550013/4342099

尝试扩展BaseExpandableListAdapter并覆盖getChildTypeCount()getChildType()