有一个自定义列表视图,它显示文本或图像或两者。现在它显示文本或图像或两者,但问题是只有文本显示仍然可以看到imageView占用的空间。我已将可见性设置为GONE,但它不是隐藏的。
Listview的适配器代码:
public class DescAdapter extends BaseAdapter implements Filterable{
private final listdisplay ds;
private ArrayList<descusers> dusers;
private ArrayList<descusers> orig;
public DescAdapter(listdisplay ds, ArrayList<descusers> dusers) {
this.ds = ds;
this.dusers = dusers;
}
@Override
public int getCount() {
return dusers.size();
}
@Override
public Object getItem(int position) {
return dusers.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
descusers du = dusers.get(position);
String username = du.loginname;
String descCrip=du.descCrip;
String Limage = du.image;
convertView = LayoutInflater.from(ds).inflate(R.layout.customlist,null);
TextView uname = (TextView)convertView.findViewById(R.id.username);
TextView desc = (TextView)convertView.findViewById(R.id.description);
ImageView iview= (ImageView)convertView.findViewById(R.id.imageList);
iview.setVisibility(View.GONE);
uname.setText(username);
desc.setText(descCrip);
if(!Limage.isEmpty()){
Glide.with(convertView.getContext()).load(Limage)
.diskCacheStrategy(DiskCacheStrategy.ALL).fitCenter().centerCrop().into(iview);
iview.setVisibility(View.VISIBLE);
}
return convertView;
}
}
自定义XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="226dp"
android:orientation="vertical"
android:background="@drawable/test"
android:layout_gravity="center_horizontal|top">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"
android:textColor="#ff6aa9ff"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="45dp"/>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Suggest"
android:textColor="#ff06010b"
android:textSize="18sp"
android:layout_marginLeft="45dp"/>
<ImageView
android:layout_width="275dp"
android:layout_height="165dp"
android:id="@+id/imageList"
android:layout_marginTop="6dp"
android:layout_marginLeft="45dp"/>
</LinearLayout>
我在上面的代码中做了什么错误?
答案: 问题在于线性布局高度设置为android:layout_height =&#34; 226dp&#34; ,改为Wrap-Content解决了这个问题。
答案 0 :(得分:0)
在适配器的getView方法中尝试此代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
descusers du = dusers.get(position);
String username = du.loginname;
String descCrip=du.descCrip;
String Limage = du.image;
ViewHolder viewHolder = null;
if(convertView==null){
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(ds).inflate(R.layout.customlist,null);
viewHolder.uname = (TextView)convertView.findViewById(R.id.username);
viewHolder.desc = (TextView)convertView.findViewById(R.id.description);
viewHolder.iview= (ImageView)convertView.findViewById(R.id.imageList);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.uname.setText(username);
viewHolder.desc.setText(descCrip);
if(!Limage.isEmpty()){
Glide.with(convertView.getContext()).load(Limage)
.diskCacheStrategy(DiskCacheStrategy.ALL).fitCenter().centerCrop().into( viewHolder.iview);
viewHolder.iview.setVisibility(View.VISIBLE);
}else{
viewHolder.iview.setVisibility(View.GONE);
}
return convertView;
}
并使用此类作为持有人:
class ViewHolder {
TextView uname;
TextView desc;
ImageView iview;
}