当我在getView()中设置一些要查看的项目时,其他项目不会出现

时间:2015-06-19 20:21:09

标签: java android listview imageview getview

Visi在我的自定义适配器getView方法中,我有一个用于不同类型布局单元的开关盒。我指的是这些单元格的一个布局xml,并通过setVisibility(View ....)显示/隐藏某些元素。在我的第一个单元格中,“StreamLayout”,我想要显示的是一个imageView,“Boombox”。我将此图像视图设置为在所有其他布局中不可见,但是当我这样做时,它将不会显示在第一个布局中。我将提供我的getView方法和我的布局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>

1 个答案:

答案 0 :(得分:1)

这不起作用的原因是因为您正在使用phantom.js来维护对所有适配器项的每个UI元素的引用。

因此,如果在第一次迭代中将元素设置为ViewHolder并且不再触及该元素,则无论它在XML中如何定义,它都将保持该状态。

要解决此问题,您应该始终在View.GONE中明确设置该元素的可见性,具体取决于您使用的布局,因为除了第一次迭代之外,它将引用ViewHolder和状态它在,而不是XML文件膨胀的视图。