listviewitem在滚动后重新加载

时间:2016-05-15 01:21:49

标签: android listview

我有一个listView女巫显示新闻列表。当用户阅读新闻项目时,它的外观应该变灰。所以在listadapter我检查新闻是否被加入,如果是读取布局1加载到项目,如果不是布局2加载到此项目。问题是:litview中前10个第一个加载的项目显示完美,但是当我滚动以加载更多项目并返回到listview灰色listview项目的操作时,布局2获得更改布局1!这是我的listadapter代码:问题是什么?

    package ir.whc.news.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.support.annotation.LayoutRes;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.orm.query.Condition;
import com.orm.query.Select;

import java.util.List;

import ir.whc.news.R;
import ir.whc.news.classes.MyApplication;
import ir.whc.news.classes.Utility;
import ir.whc.news.model.NewsItem;
import ir.whc.news.model.SeenItem;

/**
 * Created by marzieh on 18/03/2016.
 */
public class NewsListAdapter extends BaseAdapter {


    private Activity activity;
    private LayoutInflater inflater;
    private List<NewsItem> newsItems;
    private int node_id;
    private int subdomain_id;
    Context context;

    ImageLoader imageLoader = MyApplication.getInstance().getImageLoader(true);

    public NewsListAdapter(Activity activity, List<NewsItem> newsItems, int subdomain_id, int node_id) {
        this.activity = activity;
        this.newsItems = newsItems;
        this.node_id = node_id;
        this.subdomain_id = subdomain_id;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = activity;

    }


    @Override
    public int getCount() {
        return newsItems.size();
    }

    @Override
    public NewsItem getItem(int position) {
        return newsItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return newsItems.get(position).get_id();
    }

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

        Log.d("position is " ,String.valueOf(position));

        final NewsItem item = newsItems.get(position);
        if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
            if (IsReed(item.get_id(),item.get_termId()))
                convertView = inflater.inflate(R.layout.item_news_iread, null);
            else
                convertView = inflater.inflate(R.layout.item_news, null);

        if (imageLoader == null)
            imageLoader = MyApplication.getInstance().getImageLoader(true);


        LinearLayout parrentLayout = (LinearLayout) convertView.findViewById(R.id.parentL);
        TextView titleHolder = (TextView) convertView.findViewById(R.id.text_title);
        TextView summeryHolder = (TextView) convertView.findViewById(R.id.text_summery);
        TextView dateHolder = (TextView) convertView.findViewById(R.id.text_date);
        NetworkImageView thumbpicHolder = (NetworkImageView) convertView
                .findViewById(R.id.image_news_thumb);
        final ImageView image_bookmark = (ImageView) convertView.findViewById(R.id.image_bookmark);



        titleHolder.setText(item.get_title());
        summeryHolder.setText(item.get_summary());
        imageLoader.get(item.get_imgPath(), imageLoader.getImageListener(thumbpicHolder,
                R.drawable.ic_launcher,
                android.R.drawable.ic_dialog_alert));

        thumbpicHolder.setImageUrl(item.get_imgPath(), imageLoader);
        thumbpicHolder.setVisibility(item.get_imgPath().trim().isEmpty() ? View.GONE : View.VISIBLE);
        dateHolder.setText(Utility.GetFormattedDteTime(newsItems.get(position).get_datePublish()));


        //checkBookmark
        if (Select.from(NewsItem.class).where(Condition.prop("_id").eq(item.get_id())).list().size() > 0)
            image_bookmark.setImageResource(R.drawable.ic_bookmarkfill);
        else
            image_bookmark.setImageResource(R.drawable.ic_bookmark_normal);
        //add or delet from  bookmark
        image_bookmark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utility.Insert_bookmark("_id", item, image_bookmark, NewsItem.class);
            }
        });



       /* Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_down);
        convertView.startAnimation(animation);

*/
        return convertView;
    }

    private boolean IsReed(int _id,int _term_id) {
        ////check is reed

        if (SeenItem.find(SeenItem.class, "_id = ? and _nodeid = ? and _termid = ? and _domainid= ? ", String.valueOf(_id),
                String.valueOf(node_id),
                String.valueOf(_term_id),
                String.valueOf(subdomain_id)).size() > 0) {

            return true;
        } else
            return false;

    }

}

2 个答案:

答案 0 :(得分:3)

在Android Adpater 中,遵循重复使用视图的概念。均值如果屏幕上显示5个视图,则滚动列表,然后第1个视图将再循环到第6个视图。这就是你得到这种结果的原因。 所以只需删除代码中的if条件。

if (convertView == null)

或者您可以使用两种类型的视图概念BaseAdapter还有两种方法,例如getViewType(int position) 通过这个,您可以确定要渲染的视图类型。

答案 1 :(得分:1)

你的适配器的getView没有正确写入你的inflater&amp; convertView未正确初始化以下代码是初始化项目的正确方法:

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

        final ViewHolder holder;


        if (convertView == null) {

            convertView = mLayoutInflater.inflate(R.layout.row_list_base, parent,false);
            holder = new ViewHolder();
            holder.videoImage = (ImageView)convertView.findViewById(R.id.image_video_preload);
            holder.title = (TextView) convertView.findViewById(R.id.text_video_title);
            holder.subtitle = (TextView) convertView.findViewById(R.id.text_video_subtitle);

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

        final ShortVideoModel item = getItem(position);

        if (item != null) {

            ImageLoaderHelper.displayImage(mContext, holder.videoImage, item.getImageUri());

            if (!TextUtils.isEmpty(item.getTitle())) {
                holder.title.setText(item.getTitle());
            }


           //... initialize rest of things
        }


        return convertView;
    }