我正试图从一个网站获取新闻。有些文章有图片。这是我的问题:一些文章没有图像,所以我想删除该行和标题textview的Imageview应该获取该空间。
我尝试在BaseAdapter
中实现它,但我认为setvisibility(View.Gone)
无效。
这是我的布局文件:
<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp" />
<!-- News Title -->
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/title"
android:ellipsize="end"
android:textStyle="bold"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"/>
这是我在基本适配器中的实现:
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
// getting movie data for the row
Movie m = movieItems.get(position);
if(m.getThumbnailUrl() == null)
{
thumbNail.setVisibility(View.GONE); //Not working
}
else {
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
}
目前的输出是: 我希望textview使用Imageview的空白区域。
当我尝试使用ViewHolder进行平滑过渡时,我无法实现此逻辑:
if (position==-1)
{
viewHolder.thumbnail.setVisibility(View.GONE);
}
else
{
viewHolder.thumbnail.setImageUrl(news.getThumbnailUrl(), imageLoader);
}
答案 0 :(得分:1)
您可以检查图片的URL是否有效,然后您可以使用Visibility gone concept。使用类似下面的代码:
if(!URLUtil.isValidUrl(m.getThumbnailUrl())){
thumbNail.setVisibility(View.GONE);
}else {
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
thumbNail.setVisibility(View.VISIBLE);
}
希望这对你有所帮助。
答案 1 :(得分:1)
试试这个:
if(m.getThumbnailUrl() == null)
{
thumbNail.setVisibility(View.GONE);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addrule(LinearLayout.RIGHT_OF,0);
lp.setMargins(-150,0,0,0); //set your margins for your textview as your want
title.setLayoutParams(lp);
Your_Linear_Layout_Name.addView(title, lp);
//or RelativeLayoutName.addView(title, lp);
}
.....
答案 2 :(得分:1)
m.getThumbnailUrl()返回一个String,对吗?
你试过这个吗?
if(m.getThumbnailUrl() == null || TextUtils.isEmpty(m.getThumbnailUrl()))
尝试调试并检查是否点击此行:
thumbNail.setVisibility(View.GONE);
答案 3 :(得分:1)
如果您没有以编程方式为该特定LayoutParameters
设置文章集TextView
的图片。
if(m.getThumbnailUrl() == null)
{
thumbNail.setVisibility(View.GONE);
RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) title.getLayoutParams();
layoutParams.addRule(RelativeLayout.RIGHT_OF,0);
layoutParams.addRule(RelativeLayout.ALIGN_TOP,0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
title.setLayoutParams(layoutParams);
}
答案 4 :(得分:1)
这是因为ListView的工作原理。你可以尝试这样的事情:
if(m.getThumbnailUrl() == null)
{
thumbNail.setVisibility(View.GONE);
LayoutParams layoutParams = title.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
title.setLayoutParams(layoutParams);
}
else {
thumbanil.setVisibility(View.VISIBLE);
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
LayoutParams layoutParams = title.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
title.setLayoutParams(layoutParams);
}