0

看来我和这个问题有同样的问题:here。但是我的列表视图已经有 fill_parent 的高度。我做了一个调试,它在 4 行后停止,并且一直循环相同的行。

这是我的代码:

public class NewsAdapter extends BaseAdapter{
List<News> news;
private Context context;
public NewsAdapter(Context context, List<News> news) {
    this.context = context;
    this.news = news;
}
@Override
public int getCount() {
    return news.size();
}

@Override
public Object getItem(int position) {
    return news.get(position);
}

@Override
public long getItemId(int arg0) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.adapter_news, null);
        ViewHolder holder = new ViewHolder();
        holder.tv_title = (TextView) v.findViewById(R.id.tv_adapter_news_title);
        holder.tv_description = (TextView) v.findViewById(R.id.tv_adapter_news_description);
        holder.tv_date = (TextView) v.findViewById(R.id.tv_adapter_news_date);
        //
        holder.siv_picture = (SmartImageView) v.findViewById(R.id.siv_adapter_news_picture);
        //
        holder.view_color = (View) v.findViewById(R.id.v_adapter_news_color);
        ///
        String type = news.get(position).getNewsType();
        String title = news.get(position).getNewsTitle();
        String description = news.get(position).getNewsDescription();
        String picture = news.get(position).getNewsPicture();
        String date = news.get(position).getNewsDate();
        String link = news.get(position).getNewsLink();
        String id = news.get(position).getNewsId();

        if(title != null){
            holder.tv_title.setText(title);
        }
        if(description != null){
            holder.tv_description.setText(description);
        }
        if(picture != null){
            holder.siv_picture.setImageUrl(picture);
        }else{
            holder.siv_picture.setImageUrl("http://aovivo.slbenfica.pt/Portals/3/noticias/Epoca2012_13/Futebol%20Profissional/CampeonatoNacional_2012_13/Benfica_fcPorto/SLB_Futebol_Matic_Festejos_Benfica_FCPorto_13Janeiro2013_V.jpg?w=227");
        }
        if(date != null){
            holder.tv_date.setText(date);
        }
        if(type.equalsIgnoreCase(News.CATEGORY_ARTICLE)){
            holder.view_color.setBackgroundColor(context.getResources().getColor(R.color.red));
            if(date != null){
                holder.tv_date.setTextColor(context.getResources().getColor(R.color.red));
            }

        }else{
            holder.view_color.setBackgroundColor(context.getResources().getColor(R.color.blue));
        }
    }
    System.out.println("getView " + position + " " + convertView);
    return v;
}
static class ViewHolder {
    public SmartImageView siv_picture;
    public TextView tv_title;
    public TextView tv_description;
    public TextView tv_date;
    public View view_color;
}

}

这是 xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100" 

>

<!-- Photo part -->

<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="30"
    android:layout_marginTop="@dimen/dim20dp" 
    android:layout_marginBottom="@dimen/dim20dp" 
    >

    <View
        android:id="@+id/v_adapter_news_color"
        android:layout_width="5dp"
        android:layout_height="100dp" 
        android:layout_centerVertical="true"/>

    <com.loopj.android.image.SmartImageView
        android:id="@+id/siv_adapter_news_picture"
        android:layout_width="150dp"
        android:layout_height="100dp" 
        android:layout_centerInParent="true"
        android:scaleType="fitCenter"
        />
</RelativeLayout>

<!-- Text Part -->

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="70"
    android:orientation="vertical" 
    android:layout_marginTop="@dimen/dim20dp"
    android:layout_marginBottom="@dimen/dim20dp"
    >

    <TextView 
        android:id="@+id/tv_adapter_news_date"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="date"
        android:layout_marginBottom="@dimen/dim5dp"
        />

    <TextView
        android:id="@+id/tv_adapter_news_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title" 
        style="@style/news_text_title"
        android:layout_marginBottom="@dimen/dim15dp"/>

    <TextView
        android:id="@+id/tv_adapter_news_description"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/news_text_description"
        android:text="description" />
</LinearLayout>

</LinearLayout>
4

2 回答 2

1

您的 getView() 实现不处理 convertView 不为空的情况。确保您使用 setTag() 和 getTag() 将 ViewHolder 绑定到该视图。

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View v = convertView;
    if(v == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.adapter_news, null);

        ViewHolder holder = new ViewHolder();
        holder.tv_title = (TextView) v.findViewById(R.id.tv_adapter_news_title);
        // etc...

        v.setTag(holder);
    } else {
        holder = v.getTag();
    }

    News item = getItem(position);
    holder.tv_title.setText(item.getNewsTitle());
    // etc...

    return v;
}
于 2013-06-20T16:24:41.257 回答
0

问题在于getView您检查v == null哪个好但是如果 v 不为空会发生什么...您不处理它,因此它重用了最后一个视图,这就是您看到倍数的原因

你应该这样做

if(v == null){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.adapter_news, null);
}

//then set the views after you do the check so it is always done
于 2013-06-20T16:22:33.367 回答