【问题标题】:Android listview keep showing the same rowsAndroid listview保持显示相同的行
【发布时间】:2013-06-20 16:19:38
【问题描述】:

看来我和这个问题有同样的问题: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>

【问题讨论】:

    标签: android listview android-adapter


    【解决方案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;
    }
    

    【讨论】:

    • 我知道 ViewHolder 是做什么用的,但是标签的用途是什么,因为我没有设置标签并且它仍然有效?
    • 标签是ViewHolder工作的全部原因。你这样做是为了当一个视图被回收时你不需要调用findViewById()来再次获取所有子视图——它们已经被ViewHolder引用了,所以你只需调用getTag()并将其转换为@ 987654325@.
    【解决方案2】:

    问题出在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-04-17
    • 2012-03-06
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    相关资源
    最近更新 更多