在ExpandableListView中获取子组父视图

时间:2014-12-10 06:54:48

标签: android expandablelistview

所有

我想更改所有子组[不是列表中的个别孩子]的背景。

我尝试调用getParent()方法view.getParent(),但抛出错误。

这是我的代码

        View parentGroup = (View)view.getParent();
        parentGroup.setBackground(new ColorDrawable(Color.CYAN));

我想要这种UI。 - > child group background

修改

我的自定义适配器:

package com.icanstudioz.android.njoyful.adapters;

import java.util.HashMap;
import java.util.List;

import com.icanstudioz.android.njoyful.R;
import com.icanstudioz.android.njoyful.utils.Commons;
import com.icanstudioz.android.pojoclasses.Categories;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

public class ExpandableCategoriesListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<Categories>> _listDataChild;

    public ExpandableCategoriesListAdapter(Context context,
            List<String> listDataHeader,
            HashMap<String, List<Categories>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    public int getSubCategoryId(int groupPosition, int childPosition) {

        return _listDataChild.get(_listDataHeader.get(groupPosition))
                .get(childPosition).getId();

    }

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

        ViewHolder holder;
        View view = convertView;

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) _context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            holder = new ViewHolder();

            view = inflater.inflate(R.layout.categories_list_items, null);
            View parentGroup = (View)view.getParent();
            parentGroup.setBackground(new ColorDrawable(Color.CYAN));
            Commons.applyFont(_context,
                    view.findViewById(R.id.RelativeLayout1),
                    "RobotoSlab-Light.ttf");
            // view.setBackgroundColor(color_arr[position]);
            holder.cat_name = (TextView) view.findViewById(R.id.cat_name);
            view.findViewById(R.id.cat_item_check).setVisibility(View.GONE);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        Categories dItem = (Categories) getChild(groupPosition, childPosition);

        holder.cat_name.setText(dItem.getCat_name());

        return view;
    }

    public class ViewHolder {
        TextView cat_name;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.order_category_header,
                    null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        Commons.applyFont(_context, lblListHeader, "RobotoSlab-Bold.ttf");
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

希望这会有所帮助。

请指导我如何做到这一点或任何其他解决方案?

0 个答案:

没有答案