gyymz1992
gyymz1992
2016-01-04 12:05

ListView自动刷新多个Item

20
  • android
  • list

图片说明ListView自定刷新Item中有改变的数据,并将此项 item背景闪烁一下。

 package master.modle.masterfinance.adapter;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import master.modle.masterfinance.R;
import master.modle.masterfinance.model.biz.MarketList;
import master.modle.masterfinance.utils.LogUtils;
import master.modle.masterfinance.utils.UIUtils;
import master.modle.masterfinance.utils.ViewHolderUtils;
/**
 *
 *  @action:
 *  @author:  YangShao
 *  @date: 2016/1/4 @time: 10:46
 */
public class MarkItmeAapter extends BaseAdapter{
    List<MarketList> listEntitys;
    ListView  listView;
   public MarkItmeAapter(List<MarketList> listEntitys,ListView listView){
        this.listEntitys=listEntitys;
       this.listView=listView;
    }

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

    @Override
    public Object getItem(int i) {
        return listEntitys.get(i);
    }

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

    public static ViewHolder mviewHolder;
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view==null){
            view = LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.item_market, viewGroup, false);
            holder = new ViewHolder();
            holder.tx_name = (TextView) view.findViewById(R.id.id_mr_name);
            holder.tx_new = (TextView) view.findViewById(R.id.id_mr_new);
            holder.tv_range = (TextView) view.findViewById(R.id.id_mr_range);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        setData(holder,i);
        return view;
    }

    private static class ViewHolder {
        private TextView tx_name;
        private TextView tx_new;
        private TextView tv_range;
        private MarketList market;
    }

    public List<MarketList> getListViewData(){
        return  listEntitys;
    }

    public void setListEntitys(List<MarketList> listEntitys) {
        this.listEntitys = listEntitys;
    }



    /**
     * 设置viewHolder的数据
     * @param holder
     * @param itemIndex
     */
    private void setData(ViewHolder holder, int itemIndex) {
        holder.tx_name.setText(listEntitys.get(itemIndex).getName());
        holder.tx_new.setText(listEntitys.get(itemIndex).getLatestpri() + "");
        holder.tv_range.setText(listEntitys.get(itemIndex).getMarkup() + "");
        holder.market=listEntitys.get(itemIndex);
        //int markup=Integer.valueOf(String.valueOf(listEntitys.get(i).getMarkup()));
        // int markup = Integer.parseInt(new java.text.DecimalFormat("0").format(listEntitys.get(i).getMarkup()));
        int markup = (int)listEntitys.get(itemIndex).getMarkup();
        if (markup>=0){
            holder.tv_range.setTextColor(Color.RED);
            holder.tx_new.setTextColor(Color.RED);
        }else{
            holder.tv_range.setTextColor(Color.GREEN);
            holder.tx_new.setTextColor(Color.GREEN);
        }
    }

}

package master.modle.masterfinance.frg;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import master.modle.masterfinance.R;
import master.modle.masterfinance.adapter.MarkItmeAapter;
import master.modle.masterfinance.adapter.PagerItmeAapter;
import master.modle.masterfinance.base.BaseApplication;
import master.modle.masterfinance.base.BaseFragment;
import master.modle.masterfinance.model.biz.InformatTitle;
import master.modle.masterfinance.model.biz.MarketList;
import master.modle.masterfinance.mpresenter.IMarketListPresnter;
import master.modle.masterfinance.mview.IMarketView;
import master.modle.masterfinance.utils.LogUtils;
import master.modle.masterfinance.utils.ToastUtils;

/**
 * @action:
 * @author: YangShao  VapagerFragment
 * @date: 2015/12/24 @time: 16:22
 */
public class MarkItmeFrag extends BaseFragment implements IMarketView {
    @Bind(R.id.id_mr_list)
    ListView idMrList;
    private View rootView;
    InformatTitle categoryEntitie;
    //标签内容
    IMarketListPresnter iMarketListPresnter = new IMarketListPresnter(this);
    private String actName;
    public static MarkItmeFrag informationItmeFrag;
    private PagerItmeAapter itmeFragAapter;


     Handler handler  = new Handler(){
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
                iMarketListPresnter.getMarkList(String.valueOf(categoryEntitie.getId()));
            }
        }
    };


    private Timer timer = new Timer(true);
    //任务
    private TimerTask task = new TimerTask() {
        public void run() {
            Message msg = new Message();
            msg.what = 1;
            handler.sendMessage(msg);
        }
    };


    public static MarkItmeFrag getFragment(InformatTitle categoryEntitie, String mactName) {
        LogUtils.e("InformationItmeFrag getFragment" + mactName);
        informationItmeFrag = new MarkItmeFrag();
        informationItmeFrag.categoryEntitie = categoryEntitie;
        informationItmeFrag.actName = mactName;
        return informationItmeFrag;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = getRootView();
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }


    public void initView() {
        //启动定时器
        timer.schedule(task, 0, 10 * 1000);
      //  iMarketListPresnter.getMarkList(String.valueOf(categoryEntitie.getId()));
    }


    public View getRootView() {
        rootView = LayoutInflater.from(getActivity()).inflate(
                R.layout.frag_mark_home, null);
        return rootView;
    }

    /* 摧毁视图 */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        timer.cancel();
        timer=null;
    }

    /* 摧毁该Fragment,一般是FragmentActivity 被摧毁的时候伴随着摧毁 */
    @Override
    public void onDestroy() {
        super.onDestroy();
        BaseApplication.requestQueue.cancelAll(this);
        if (timer!=null){
            timer.cancel();
            timer=null;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    MarkItmeAapter markItmeAapter;
    @Override
    public void setInfomatList(final  List<MarketList> listEntitys) {
        if (listEntitys.size()==0){
            return;
        }if (markItmeAapter==null){
            markItmeAapter=new MarkItmeAapter(listEntitys,idMrList);
            idMrList.setAdapter(markItmeAapter);
        }else {
            setUpdateListView(listEntitys);
        }
    }


    public void setUpdateListView(List<MarketList> mlistEntitys){
        //得到原有数据
        List<MarketList> liste = markItmeAapter.getListViewData();
        for (int i=0;i<liste.size();i++){
            double markup1=liste.get(i).getLatestpri();
            double markup2=mlistEntitys.get(i).getLatestpri();
            if (!equal(markup1,markup2)){
                ToastUtils.showToastReal(markup2 + "");
                LogUtils.e("发生改变的数据id" + i);
            }
        }
        markItmeAapter.setListEntitys(mlistEntitys);
        markItmeAapter.notifyDataSetChanged();
    }

    boolean equal(double num1,double num2)
    {
        if((num1-num2>-0.000001)&&(num1-num2)<0.000001)
            return true;
        else
            return false;
    }

    @Override
    public void noData() {

    }

}



package master.modle.masterfinance.model.biz;

public  class MarketList {
            private int id;
            private String name;
            private double latestpri;
            private double markup;

            public void setId(int id) {
                this.id = id;
            }

            public void setName(String name) {
                this.name = name;
            }

            public void setLatestpri(double latestpri) {
                this.latestpri = latestpri;
            }

            public void setMarkup(double markup) {
                this.markup = markup;
            }

            public int getId() {
                return id;
            }

            public String getName() {
                return name;
            }

            public double getLatestpri() {
                return latestpri;
            }

            public double getMarkup() {
                return markup;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (!(o instanceof MarketList)) return false;

                MarketList that = (MarketList) o;

                return getId() == that.getId();

            }

            @Override
            public int hashCode() {
                return getId();
            }
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

6条回答