Java 当我在getView()中将某些项设置为view.invisible时,其他项不';不出现
visible在我的自定义适配器getView方法中,我为不同类型的布局单元提供了一个开关盒。我为这些单元格引用一个布局xml,并通过setVisibility(视图…)显示/隐藏某些元素。在我的第一个单元格“StreamLayout”中,我只想显示一个imageView“Boombox”。我将此图像视图设置为在所有其他布局中不可见,但当我这样做时,它不会显示在第一个布局中。我将提供我的getView方法和布局xml:Java 当我在getView()中将某些项设置为view.invisible时,其他项不';不出现,java,android,listview,imageview,getview,Java,Android,Listview,Imageview,Getview,visible在我的自定义适配器getView方法中,我为不同类型的布局单元提供了一个开关盒。我为这些单元格引用一个布局xml,并通过setVisibility(视图…)显示/隐藏某些元素。在我的第一个单元格“StreamLayout”中,我只想显示一个imageView“Boombox”。我将此图像视图设置为在所有其他布局中不可见,但当我这样做时,它不会显示在第一个布局中。我将提供我的getView方法和布局xml: <ImageView android:layou
<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>
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;
}
<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>
我的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>
这不起作用的原因是,您使用的是一个
ViewHolder
,它将为所有适配器项维护对每个UI元素的引用
<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>
因此,如果一个元素在第一次迭代中被设置为View.GONE
,并且不再被触摸,那么它将保持该状态,而不管它在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>
为了解决这个问题,您应该始终在
getView()
中显式设置该元素的可见性,具体取决于您使用的布局,因为如果它不是第一次迭代,您将引用ViewHolder及其所处的状态,不是由XML文件膨胀的视图。我将R.id.boombox设置为view.Visible且无任何更改只有当您获得空convertView时才应膨胀视图。@TEDYWilson在所有示例中,您都将其设置为view.Goe
。您希望它在哪些布局中可见?另外,您的开关丢失了break
^另外,正如blackbelt指出的,您不应该在StreamLayout中每次都对布局进行充气,这会破坏拥有一个viewHolder的目的。我真的对这个示例感到困惑,我看到您将所有内容都设置为查看。消失了,为什么?对不起,我改了
<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>