滚动时的ListView位置

时间:2015-08-13 09:22:21

标签: android listview android-listview

我的ListView有问题,特别是在getView中。在我的Article对象中,我包含一个布尔参数,对象文本的颜色取决于param。但是在屏幕上显示对象时,文本颜色仅取决于两个第一个对象。例如,fisrt obeject包含“true”参数,第二个包含“false”参数,并且所有下一个obect都在“true”/“false”查询中获取自己的参数,尽管所有下一个对象仅包含“false”参数。 对象的其他内容很好,并且表现良好。

这是我的getView方法。在那里我标记了我的Log.d命令。滚动时我得到了这个,它只是连续滚动到第三个对象:

08-13 12:20:51.538  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 512  true
08-13 12:20:51.568  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 510  false
08-13 12:20:51.588  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 512  true
08-13 12:20:51.618  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 510  false
08-13 12:20:53.460  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 508  false
08-13 12:20:54.511  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 512  true
08-13 12:20:54.561  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 510  false
08-13 12:20:54.591  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 512  true
08-13 12:20:54.621  21990-21990/md.mybebe.mybebe D/readCUstom﹕ 510  false

public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    Typeface robotoRegular = Typeface.createFromAsset(activity.getAssets(),"font/Roboto-Regular.ttf");
    Typeface robotoBold = Typeface.createFromAsset(activity.getAssets(),"font/Roboto-Bold.ttf");

    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = activity.getLayoutInflater();

        DisplayMetrics dm = new DisplayMetrics();
        ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);

        if (dm.widthPixels <= 480) {
            rowView = inflater.inflate(R.layout.list_row_little_width, parent, false);
        } else {
            rowView = inflater.inflate(R.layout.list_row, parent, false);
        }

        if (imageLoader == null)    imageLoader = AppController.getInstance().getImageLoader();

        holder = new ViewHolder();

        holder.thumbNail = (NetworkImageView) rowView.findViewById(R.id.thumbnail);

        holder.title = (TextView) rowView.findViewById(R.id.title);
        holder.title.setTypeface(robotoBold);

        holder.description = (TextView) rowView.findViewById(R.id.description);
        holder.description.setTypeface(robotoRegular);


        rowView.setTag(holder);
    } else {
        holder = (ViewHolder) rowView.getTag();
    }

   ---->> Log.d("readCUstom", articleItems.get(position).getId()+ "  " +articleItems.get(position).isRead());
    if (articleItems.get(position).isRead()) {
        holder.title.setTextColor(Color.argb(100, 0, 0, 0));
        holder.description.setTextColor(Color.argb(100, 0, 0, 0));
    }

    final Button readMoreButton = (Button) rowView.findViewById(R.id.readMoreButton);
    readMoreButton.setTypeface(robotoBold);

    ImageButton toShare = (ImageButton) rowView.findViewById(R.id.toShareButton);
    toAddToFavorite = (ImageButton) rowView.findViewById(R.id.toAddToFavoriteButton);

    SharedPreferences mSettings = activity.getSharedPreferences(LoginActivity.APP_PREFERENCES, Context.MODE_PRIVATE);
    int userID = mSettings.getInt(LoginActivity.APP_PREFERENCES_PROFILE_ID,0);

    if(articleItems.get(position).isFavorite() & userID != 0) toAddToFavorite.setImageResource(R.drawable.ic_star);
    else toAddToFavorite.setImageResource(R.drawable.ic_star_outline);

    // getting movie data for the row
    final Article article = articleItems.get(position);

    final View view = rowView;

    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            readMoreButton.performClick();
            readMoreButton.setPressed(true);
            view.setSelected(true);


        }
    });

    URLImageParser p = new URLImageParser(holder.description, rowView.getContext(), activity);
    String html = article.getDescription();
    MyHtmlTagHandler tagHandler = new MyHtmlTagHandler();
    Spanned htmlSpan = Html.fromHtml(html, p, tagHandler);

    // thumbnail image
    holder.thumbNail.setImageUrl(article.getThumbnailUrl(), imageLoader);

    // title
    holder.title.setText(TextUtils.htmlEncode(article.getTitle()));

    // description
    holder.description.setText(htmlSpan);

    toShare.setOnClickListener(toShareOnClickListener(article, position));
    toAddToFavorite.setOnClickListener(toAddToFavoriteListener(position));

    // button read more
    readMoreButton.setOnClickListener(readMoreClickListener(position));

    return view;
}

2 个答案:

答案 0 :(得分:1)

适配器重用视图。因此,如果您为某一行设置了颜色,那么当您重复使用此行时,颜色更改将会有效。

您应始终将颜色设置为所需的颜色。添加以下行:

{{1}}

答案 1 :(得分:1)

尝试设置其他颜色[else]:

  if (articleItems.get(position).isRead()) {
        holder.title.setTextColor(Color.argb(100, 0, 0, 0));
        holder.description.setTextColor(Color.argb(100, 0, 0, 0));
    }else{
        holder.title.setTextColor(Color.argb(100, 100, 0, 0));
        holder.description.setTextColor(Color.argb(100, 100, 0, 0));
   }