ExpandableListView setAlpha第一次不工作

时间:2016-02-25 09:30:07

标签: java android expandablelistview expandablelistadapter

我只想让一个孩子正常,其他孩子都是透明的。一些不可扩展的组也可以是透明的,而可扩展组必须是完全不透明的。填充视图是完美的,但设置alpha不会。第一次,所有项目都是100%不透明的,当我展开一个组时,每个项目都会根据我的需要设置他们的alpha。

它必须是第一次工作。

我的BaseExpandableListAdapter是这样的:

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

    ImageView imgContentListThumbnail;
    ImageView imgContentListRibbon;
    ImageView imgTLIcon;
    ImageView imgKilitIcon;
    TextView txtContentListTitle;
    TextView txtContentListDescription;
    ImageView imgContentListDetail;
    RelativeLayout rlThumbnail;
    ImageView imgPreview;
    RelativeLayout rlContentListRoot;

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.item_contentlist, parent, false);

        imgContentListThumbnail = (ImageView) convertView.findViewById(R.id.imgContentListThumbnail);
        imgContentListRibbon = (ImageView) convertView.findViewById(R.id.imgContentListRibbon);
        imgTLIcon = (ImageView) convertView.findViewById(R.id.imgTLIcon);
        imgKilitIcon = (ImageView) convertView.findViewById(R.id.imgKilitIcon);
        txtContentListTitle = (TextView) convertView.findViewById(R.id.txtContentListTitle);
        Fonts.setTypeface(Fonts.BOLD, txtContentListTitle, activity.getBaseContext());
        txtContentListDescription = (TextView) convertView.findViewById(R.id.txtContentListDescription);
        imgContentListDetail = (ImageView) convertView.findViewById(R.id.imgContentListDetail);
        rlThumbnail = (RelativeLayout) convertView.findViewById(R.id.rlThumbnail);
        imgPreview = (ImageView) convertView.findViewById(R.id.imgOnizleme);
        rlContentListRoot = (RelativeLayout) convertView.findViewById(R.id.rlContentListRoot);

        convertView.setTag(new ViewHolder(imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail, rlThumbnail,imgPreview, rlContentListRoot));
    } else {
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        imgContentListThumbnail = viewHolder.imgContentListThumbnail;
        imgContentListRibbon = viewHolder.imgContentListRibbon;
        imgTLIcon = viewHolder.imgTLIcon;
        imgKilitIcon = viewHolder.imgKilitIcon;
        txtContentListTitle = viewHolder.txtContentListTitle;
        txtContentListDescription = viewHolder.txtContentListDescription;
        imgContentListDetail = viewHolder.imgContentListDetail;
        rlThumbnail = viewHolder.rlThumbnail;
        imgPreview = viewHolder.imgPreview;
        rlContentListRoot = viewHolder.rlContentListRoot;
    }

    final NativeProgramSet nativeProgramSet = (NativeProgramSet) getGroup(groupPosition);
    Course course = null;
    for(Course item : programOnly.getProgramSummary().getCourseList()) {
        if(item.getId().equals(nativeProgramSet.getCourseIdList().get(0))) {
            course = item;
            break;
        }
    }

    //DECIDING THE ITEM TO BE TRANSPARENT
    convertView.setBackgroundColor(Color.parseColor("#F6F6F6"));
    if(nativeProgramSet.getId() != null) { //THIS IS EXPANDABLE, AND MUST BE 100% OPAQUE
        rlThumbnail.setVisibility(View.GONE);
        imgContentListThumbnail.setVisibility(View.GONE);
        imgContentListRibbon.setVisibility(View.GONE);
        imgTLIcon.setVisibility(View.GONE);
        imgKilitIcon.setVisibility(View.GONE);
        txtContentListDescription.setText(getGroupDetailContentNumbers(nativeProgramSet, programOnly.getProgramSummary().getCourseList()));
        if(isExpanded) {
            imgContentListDetail.setImageResource(R.drawable.accordion_close_icon);
        } else {
            imgContentListDetail.setImageResource(R.drawable.accordion_open_icon);
        }

        txtContentListTitle.setText(nativeProgramSet.getName());
        rlContentListRoot.setAlpha(1.0f);
    } else { //THIS HAS NO CHILDREN, MAY BE TRANSPARENT
        rlThumbnail.setVisibility(View.VISIBLE);
        imgContentListThumbnail.setVisibility(View.VISIBLE);
        txtContentListDescription.setVisibility(View.VISIBLE);
        imgContentListDetail.setImageResource(R.drawable.top_search_filter_button_icon);

        if(course != null) {
            convertView = fillListItem(true, course, imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail, imgPreview, convertView);
            if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                final Course crs = ((NewProgramDetayActivity)activity).course;
                if ((course.getId().equals(crs.getId())) && intro == null) { //THE ITEM IS SELECTED ONE, MUST BE 100% OPAQUE
                    rlContentListRoot.setAlpha(1.0f);
                } else { //THE ITEM IS NO SELECTED ONE, MUST BE TRANSPARENT
                    rlContentListRoot.setAlpha(0.5f);
                }
            }
        } else {
            rlContentListRoot.setAlpha(1.0f);
        }
    }

    return convertView;
}

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

    ImageView imgContentListThumbnail;
    ImageView imgContentListRibbon;
    ImageView imgTLIcon;
    ImageView imgKilitIcon;
    TextView txtContentListTitle;
    TextView txtContentListDescription;
    ImageView imgContentListDetail;
    RelativeLayout rlThumbnail;
    ImageView imgPreview;
    RelativeLayout rlContentListRoot;

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.item_contentlist, parent, false);

        imgContentListThumbnail = (ImageView) convertView.findViewById(R.id.imgContentListThumbnail);
        imgContentListRibbon = (ImageView) convertView.findViewById(R.id.imgContentListRibbon);
        imgTLIcon = (ImageView) convertView.findViewById(R.id.imgTLIcon);
        imgKilitIcon = (ImageView) convertView.findViewById(R.id.imgKilitIcon);
        txtContentListTitle = (TextView) convertView.findViewById(R.id.txtContentListTitle);
        Fonts.setTypeface(Fonts.BOLD, txtContentListTitle, activity.getBaseContext());
        txtContentListDescription = (TextView) convertView.findViewById(R.id.txtContentListDescription);
        imgContentListDetail = (ImageView) convertView.findViewById(R.id.imgContentListDetail);
        rlThumbnail = (RelativeLayout) convertView.findViewById(R.id.rlThumbnail);
        imgPreview = (ImageView) convertView.findViewById(R.id.imgOnizleme);
        rlContentListRoot = (RelativeLayout) convertView.findViewById(R.id.rlContentListRoot);

        convertView.setTag(new ViewHolder(imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail, rlThumbnail, imgPreview, rlContentListRoot));
    } else {
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        imgContentListThumbnail = viewHolder.imgContentListThumbnail;
        imgContentListRibbon = viewHolder.imgContentListRibbon;
        imgTLIcon = viewHolder.imgTLIcon;
        imgKilitIcon = viewHolder.imgKilitIcon;
        txtContentListTitle = viewHolder.txtContentListTitle;
        txtContentListDescription = viewHolder.txtContentListDescription;
        imgContentListDetail = viewHolder.imgContentListDetail;
        rlThumbnail = viewHolder.rlThumbnail;
        imgPreview = viewHolder.imgPreview;
        rlContentListRoot = viewHolder.rlContentListRoot;
    }

    convertView.setBackgroundColor(Color.WHITE);

    Long courseId = (Long) getChild(groupPosition, childPosition);

    rlThumbnail.setVisibility(View.VISIBLE);
    imgContentListThumbnail.setVisibility(View.VISIBLE);
    txtContentListDescription.setVisibility(View.VISIBLE);
    imgContentListDetail.setImageResource(R.drawable.top_search_filter_button_icon);

    Course course = null;
    for(Course item : programOnly.getProgramSummary().getCourseList()) {
        if(item.getId().equals(courseId)) {
            course = item;
            break;
        }
    }

    // DECIDING THE ITEM TO BE TRANSPARENT
    if(course != null) {
        convertView = fillListItem(false, course, imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail, imgPreview, convertView);
        if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            final Course crs = ((NewProgramDetayActivity)activity).course;
            if ((course.getId().equals(crs.getId())) && intro == null) { //THE ITEM IS SELECTED ONE, MUST BE 100% OPAQUE
                rlContentListRoot.setAlpha(1.0f);
            } else { //THE ITEM IS NOT SELECTED ONE, MUST BE TRANSPARENT
                rlContentListRoot.setAlpha(0.5f);
            }
        }
    } else {
        rlContentListRoot.setAlpha(0.5f);
    }

    return convertView;
}

群组和子群使用与以下相同的ViewHolder:

private static class ViewHolder {
    public final ImageView imgContentListThumbnail;
    public final ImageView imgContentListRibbon;
    public final ImageView imgTLIcon;
    public final ImageView imgKilitIcon;
    public final TextView txtContentListTitle;
    public final TextView txtContentListDescription;
    public final ImageView imgContentListDetail;
    public final RelativeLayout rlThumbnail;
    public final ImageView imgPreview;
    public final RelativeLayout rlContentListRoot;

    public ViewHolder(ImageView imgContentListThumbnail, ImageView imgContentListRibbon, ImageView imgTLIcon, ImageView imgKilitIcon, TextView txtContentListTitle, TextView txtContentListDescription, ImageView imgContentListDetail, RelativeLayout rlThumbnail, ImageView imgPreview, RelativeLayout rlContentListRoot) {
        this.imgContentListThumbnail = imgContentListThumbnail;
        this.imgContentListRibbon = imgContentListRibbon;
        this.imgTLIcon = imgTLIcon;
        this.imgKilitIcon = imgKilitIcon;
        this.txtContentListTitle = txtContentListTitle;
        this.txtContentListDescription = txtContentListDescription;
        this.imgContentListDetail = imgContentListDetail;
        this.rlThumbnail = rlThumbnail;
        this.imgPreview = imgPreview;
        this.rlContentListRoot = rlContentListRoot;
    }
}

我知道代码非常复杂,但必须有一些我想念的东西。如果您可以看到我们可以设置在视图外观的第一次正确设置alpha。 rlContentListRoot是viewHolder的根元素。

感谢您的指导

1 个答案:

答案 0 :(得分:0)

与往常一样,我找到了解决问题的方法。

在XML中,animateLayoutChanges为true,此设置阻止项目透明

所以我的旧expandableListView是这样的:

<ExpandableListView
    android:id="@+id/elvProgramCourseList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:divider="#ffffff"
    android:dividerHeight="1dp">

新的是这样的:

<ExpandableListView
    android:id="@+id/elvProgramCourseList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#ffffff"
    android:dividerHeight="1dp">