子视图上的Android Expandable Listview自定义 [英] Android Expandable Listview customization on child view

查看:63
本文介绍了子视图上的Android Expandable Listview自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现具有多个子布局的可扩展列表视图. 一切正常,但问题om,child view..child未出现在适当的位置.. 这是我的代码... PLZ HELP ME ..

i want to implement expandable list view with multiple child layouts. all works fine but problem om,child view..child not appear on appropriate position.... here is my code...PLZ HELP ME..

我认为getchildview()上的问题.我无法保存每个父母的孩子ID ...

i think problem on getchildview().i not able to hold child id of each parent...

public class ExpandableListAdapter extends BaseExpandableListAdapter {

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

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

    @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 View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        Object childPosititon;
        if(convertView == null )
        {
        if ( getChildId(groupPosition, childPosition)!=3) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.alarm_list, null);
        }
        else if( getChildId(groupPosition, childPosition)==3)
        {

            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.vibrate, null);

        }
        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);

    }
        return convertView;
    }

    private List<String> get(int groupPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @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 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.listgroup, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        //btn=(Button)convertView.findViewById(R.id.delete);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        lblListHeader.setTag(""+groupPosition);
        OnTouchListener item = null;
        lblListHeader.setOnTouchListener(item);


        return convertView;
    }

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

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


    public int getViewTypeCount() 
    {
        return 2;
    }

    public int getItemViewType(int groupposition,int childposition) {

        if (getChildId(groupposition, childposition)==3) 
            return 0;

        //Not free
        else 
            return 1;
    }

}

推荐答案

BaseExpandableListAdapter没有getViewTypeCount()和getItemViewType().为了拥有不同的子视图类型,您必须改写以下方法.

BaseExpandableListAdapter doesn't have getViewTypeCount() and getItemViewType(). in order to have different children view types you have to override the following methods instead.

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

@Override
public int getChildType(int groupPosition, int childPosition) {
if (getChildId(groupposition, childposition)==3) 
        return 0;

    //Not free
    else 
        return 1;
}

这篇关于子视图上的Android Expandable Listview自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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