【问题标题】:How to hide group indicator from specific group item in custom expandable list view?如何在自定义可扩展列表视图中隐藏特定组项中的组指示符?
【发布时间】:2014-01-15 06:05:40
【问题描述】:

我在组项中创建了带有文本视图和图像的自定义可扩展列表视图。我想在可扩展列表视图中的某些组中隐藏组指示器。如何解决这个问题? 这是可扩展的列表适配器。

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;
    ExpandableListView exp;
    private int[] parentClickStatus ;
    GroupHolder groupHolder;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData,ExpandableListView exp) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
        this.exp=exp;
        parentClickStatus = new int[6];
        setListEvent();
    }
    private void setListEvent() {
            exp.setOnGroupExpandListener(new OnGroupExpandListener() {
              @Override
              public void onGroupExpand(int arg0) {
                    parentClickStatus[arg0] = 0;
                    }
                });

        exp.setOnGroupCollapseListener(new OnGroupCollapseListener() {

                    @Override
                    public void onGroupCollapse(int arg0) {
                        parentClickStatus[arg0] = 1;
                    }
                });
    }



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

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

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

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

        txtListChild.setText(childText);
        int colorPos = childPosition % 2;
        if(colorPos==0)
        {
        convertView.setBackgroundColor(Color.MAGENTA);
        }
        else
        {
            convertView.setBackgroundColor(Color.BLUE); 
        }

        return convertView;
    }

    @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(final int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            System.out.println("convert view is null");
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
            groupHolder = new GroupHolder();
            groupHolder.arrowImg = (ImageView) convertView.findViewById(R.id.imageView1);
            groupHolder.lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
            groupHolder.lblListHeader.setTypeface(null, Typeface.BOLD);
            groupHolder.lblListHeader.setText(headerTitle);
            convertView.setTag(groupHolder);
            } else {
                groupHolder = (GroupHolder) convertView.getTag();
               }

        /*if ( getChildrenCount( groupPosition ) == 0 ) {
            groupHolder.arrowImg.setVisibility( View.INVISIBLE );
            } 
        else {
            groupHolder.arrowImg.setImageResource(R.drawable.arrow_down);
            }*/
          if (parentClickStatus[groupPosition] == 0) {
            System.out.println("group position "+groupPosition);
            groupHolder.arrowImg.setImageResource(R.drawable.arrow_up);
            } else {
              System.out.println("else group position "+groupPosition);
            groupHolder.arrowImg.setImageResource(R.drawable.arrow_down);
           }
        return convertView;
    }

    class GroupHolder {
        ImageView arrowImg;
        TextView lblListHeader;
    }


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

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

}

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:
    1. 在 list_group.xml 中添加一个 ImageView
    2. 在适配器中检查您要隐藏该 ImageView 的条件。
    3. 通过获取ImageView

      ImageView imgV = (ImageVIew )convertView.findViewById(R.id.imgVId);
      
    4. 隐藏ImageView
      imgV.setVisibility(View.INVISIBLE);
      

    【讨论】:

    • 我想在某些组项中隐藏groupindicator null,而不是在所有组项中。
    • if ( getChildrenCount( groupPosition ) == 0 ) { ((ImageView) convertView.findViewById(R.id.imageView1)).setVisibility( View.INVISIBLE ); } else { ((ImageView) convertView.findViewById(R.id.imageView1)).setVisibility(View.VISIBLE); ((ImageView) convertView.findViewById(R.id.imageView1)) .setImageResource(isExpanded?R.drawable.arrow_up:R.drawable.arrow_down); }
    【解决方案2】:

    这是我用来解决这个问题的代码。

    if ( getChildrenCount( groupPosition ) == 0 ) { ((ImageView) convertView.findViewById(R.id.imageView1)).setVisibility(View.INVISIBLE); } 别的 { ((ImageView) convertView.findViewById(R.id.imageView1)).setVisibility(View.VISIBLE); ((ImageView) convertView.findViewById(R.id.imageView1)) .setImageResource(isExpanded?R.drawable.arrow_up:R.drawable.arrow_down); }

    【讨论】:

    • 你在哪个方法中使用了这个逻辑? @user2101858
    • 看起来像 getGroupView().. 但 OP 正在尝试获取 groupIndicator 的 imageView.. 此解决方案不提供。
    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多