简体   繁体   中英

When I set some items to view.invisible in getView(), others don't appear

VisiIn my custom adapter getView method, I have a switch case for different types of layout cells. I refer to one layout xml for these cells and show/hide certain elements by setVisibility(View....) In my first cell, "StreamLayout," all I want to show is one imageView, "Boombox". I set this image view as invisible in all the other layouts, but when I do this, it won't show up in the first one. I will provide my getView method and my layout xml:

getView:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        holder = null;
        int type = getItemViewType(position);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview_cell, null);
            holder = new ViewHolder();
        } else {
            holder = (ViewHolder) convertView.getTag();
        }



         switch (type) {

                case STREAM_LAYOUT:

                    convertView = mInflater.inflate(R.layout.listview_cell, null);

                    ImageView boombox = (ImageView) convertView.findViewById(R.id.boombox).setVisibility(View.Visible);




                    convertView.findViewById(R.id.cell_image).setVisibility(View.GONE);

                        convertView.findViewById(R.id.divider).setVisibility(View.GONE);
                        convertView.findViewById(R.id.song).setVisibility(View.GONE);
                        convertView.findViewById(R.id.artist).setVisibility(View.GONE);
                        convertView.findViewById(R.id.bottom_layout).setVisibility(View.GONE);


                    convertView.setTag(holder);


                case TALKSET_LAYOUT:
                    convertView.findViewById(R.id.boombox).setVisibility(View.GONE);

                    convertView.findViewById(R.id.cell_image).setVisibility(View.GONE);
                    convertView.findViewById(R.id.divider).setVisibility(View.GONE);
                    convertView.findViewById(R.id.playcut).setVisibility(View.GONE);

                    holder.talkset = (TextView) convertView.findViewById(R.id.talk_break_text);
                    holder.talkset.setText("TALKSET");

                    holder.talkset_icon = (ImageView) convertView.findViewById(R.id.talk_break_image);
                    holder.talkset_icon.setImageResource(R.drawable.temp_talkset);

                    convertView.setTag(holder);
                    break;

                case BREAKPOINT_LAYOUT:

                    convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
                    convertView.findViewById(R.id.cell_image).setVisibility(View.GONE);
                    convertView.findViewById(R.id.divider).setVisibility(View.GONE);
                    convertView.findViewById(R.id.artist).setVisibility(View.GONE);



                       //convertView.findViewById(R.id.boombox).setVisibility(View.GONE);

                long l = Long.parseLong(oslist.get(position).get("hour"));

                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(l);
                calendar.setTimeInMillis(l * 1000);

                int hour = calendar.get(Calendar.HOUR);

                holder.song.setText("Breakpoint: "+hour+":00");

                    holder.breakpoint = (TextView) convertView.findViewById(R.id.talk_break_text);
                    holder.breakpoint.setText("BREAKPOINT");

                    holder.breakpoint_icon = (ImageView) convertView.findViewById(R.id.talk_break_image);
                    holder.breakpoint_icon.setImageResource(R.drawable.temp_talkset);

                    convertView.setTag(holder);


                    break;

                case PLAYCUT_LAYOUT: //Playcut


                    convertView = mInflater.inflate(R.layout.listview_cell, null);

                    convertView.findViewById(R.id.boombox).setVisibility(View.GONE);


                    holder.cell_image = (ImageView) convertView.findViewById(R.id.cell_image);

                    try {
                        if(oslist.get(position).get("albumArtUrl")!=null) {
                            Picasso
                                    .with(context)
                                    .load(oslist.get(position).get("albumArtUrl"))
                                    .placeholder(R.drawable.no_album_art)
                                    .error(R.drawable.no_album_art).into(holder.cell_image);
                        }else{
                            Picasso
                                    .with(context)
                                    .load(oslist.get(position).get("artistArtUrl"))
                                    .placeholder(R.drawable.no_album_art)
                                    .error(R.drawable.no_album_art).into(holder.cell_image);
                        }

                    } catch (IllegalArgumentException e) {
                        holder.cell_image.setImageResource(R.drawable.no_album_art);
                    }

                    holder.song = (TextView) convertView.findViewById(R.id.song);
                    holder.artist = (TextView) convertView.findViewById(R.id.artist);

                    holder.song.setText(oslist.get(position).get("songTitle"));
                    holder.artist.setText(oslist.get(position).get("artistName"));

                    convertView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateView(position);
                        }
                    });

                    convertView.setTag(holder);
                    break;
                case NULL_LAYOUT:

                    convertView.setTag(holder);
                    break;

            }

        return convertView;

    }

MY XML

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/boombox"
        android:id="@+id/boombox"
        />

    <!-- Versatile image view storage for either boombox, no album art image, or album art -->

    <ImageView
        android:id="@+id/cell_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        />

    <!-- Divider between image and data -->

    <View
        android:id="@+id/divider"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>

   <!-- Layout for bottom half of playcut cells -->

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/bottom_layout"
        android:layout_below="@id/divider">

        <!-- Data for Song and Artist -->

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/playcut">

            <TextView
                android:id="@+id/song"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:gravity="center"
             />

            <TextView
                android:id="@+id/artist"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/song"
                android:gravity="center"
             />
        </RelativeLayout>

        <!-- Talkset and breakpoint cell data -->

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/talk_break"
            android:paddingLeft="1dp"
            android:paddingTop="1dp"
            android:paddingBottom="1dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/talk_break_image"/>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/talk_break_text"
                android:layout_toRightOf="@id/talk_break_image"/>


        </RelativeLayout>

        <!-- Playcut Cell, clicked, displaying Facebook, Twitter, heart, and search icons -->

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="0,1,2,3,4"
            android:paddingBottom="2dp"
            android:id="@+id/table">

            <TableRow>

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/twitter_icon"/>

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/facebook_icon"/>

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/unclicked_heart_icon"/>

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/search_icon"/>

            </TableRow>
        </TableLayout>

     </RelativeLayout>

</LinearLayout>

The reason this doesn't work is because you're using a ViewHolder which will maintain a reference to each UI element for all adapter items.

Because of this, if an element is set to View.GONE in the first iteration and isn't touched again, it will remain in that state, regardless of how it is defined in the XML.

To work around this, you should always explicitly set the visibility of that element in getView() depending on which layout you are using, because if it is anything but the first iteration you will be referencing the ViewHolder and the state it is in, not the view inflated by the XML file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM