在Recyclerview中比较旧数据与新数据

时间:2018-10-16 07:48:33

标签: android android-recyclerview recycler-adapter

我是Android开发的新手,正在从事一个项目,该项目需要每秒钟调用一次API,在该API中,字段“ Amount”(dBID)一直在变化,因此我需要更新最新的recyclerview中的数量(dBID)。

为此,我每隔一秒钟便在服务中调用此API。

数据显示正确无问题。

但是现在我需要对“旧金额”和“新金额”执行一些操作。

需要采取的措施:我需要将旧值(dBID)与新值(dBID)进行比较。

如果“新值”较大,则需要将“数量的文本颜色(dBID)”更改为“蓝色”。

如果“新值”较小,那么我需要将“数量的文本颜色(dBID)”更改为“红色”。

试图通过将旧数据存储在变量中,然后将其与新值进行比较来实现此目的。

问题:此逻辑在直到第6个项目被添加到recyclerview中的项目不超过5个之前,都无法正常工作。

如果有人知道我如何实现这一目标,请帮助我。

例如,您可以引用App Vertexfx的“报价”标签。

下面是我尝试过的代码。

RecyclerView的适配器类:

public class QuoteAdapter extends RecyclerView.Adapter <QuoteAdapter.MyViewHolder>{

Context context;
List<QuoteData> data;

public QuoteAdapter(Context context,List<QuoteData> data)
{
    this.data = data;
    this.context = context;
}

 class MyViewHolder extends RecyclerView.ViewHolder{

    TextView time,symbol,sellmax,selllow,buymax,buylow,buy,sell,spread,lowtext,hightext;
    LinearLayout layout,layoutbid,layoutask;

    float currentbid,lastbid,currentask,lastask;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        time = itemView.findViewById(R.id.TVTime);
        symbol = itemView.findViewById(R.id.TVSymbol);
        sellmax = itemView.findViewById(R.id.TVSELLMAX);
        selllow = itemView.findViewById(R.id.TVSELLLOW);
        buymax = itemView.findViewById(R.id.TVBUYMAX);
        buylow = itemView.findViewById(R.id.TVBUYHIGH);
        buy = itemView.findViewById(R.id.TVBUY);
        sell = itemView.findViewById(R.id.TVSELL);
        spread = itemView.findViewById(R.id.TVSpread1);
        lowtext = itemView.findViewById(R.id.low);
        hightext = itemView.findViewById(R.id.high);
        layout = itemView.findViewById(R.id.layout);
        layoutbid = itemView.findViewById(R.id.LLBid);
        layoutask = itemView.findViewById(R.id.LLAsk);
        currentbid = 0;
        lastbid = 0;
        currentask = 0;
        lastask = 0;
    }
}

 @NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.quotelist,viewGroup,false);
    return new MyViewHolder(view);
}

@SuppressLint("ResourceAsColor")
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {

    final QuoteData data1 = data.get(i);

    myViewHolder.time.setText(data1.dLut);
    myViewHolder.symbol.setText(data1.dSymbol);
    myViewHolder.sellmax.setText(data1.dBid);  //Bid
    myViewHolder.selllow.setText(data1.dLow);
    myViewHolder.buymax.setText(data1.dAsk);  //ask
    myViewHolder.buylow.setText(data1.dHigh);


    myViewHolder.currentbid = Float.parseFloat((data1.dBid));
    myViewHolder.currentask = Float.parseFloat((data1.dAsk));

    if (myViewHolder.currentbid > myViewHolder.lastbid)
    {
        myViewHolder.sellmax.setTextColor(Color.BLUE);
    }
    if (myViewHolder.currentbid < myViewHolder.lastbid)
    {
        myViewHolder.sellmax.setTextColor(Color.RED);
    }

    myViewHolder.lastbid = myViewHolder.currentbid;
    myViewHolder.lastask = myViewHolder.currentask;

        }
    });
}

2 个答案:

答案 0 :(得分:1)

我建议您看看Android SDK中的那些类:

DiffUtil

DiffUtil旨在比较现有和新的回收者视图项目并触发适当的事件。您需要传递一个回调,该回调可以判断两个项目是否相同以及它们的内容是否已更改。

AsyncListDiffer

它包装DiffUtil并异步执行其逻辑,从而提供更好的性能。

ItemAnimator

给定的ItemAnimator的{​​{1}}在其项目上触发更改事件时会默认调用。您可以提供RecyclerView方法的实现来相应地更改颜色。

答案 1 :(得分:1)

为以后的参考,我已使用以下代码解决了上述问题。

在适配器中定义了两个String ArrayList

public class QuoteAdapter extends RecyclerView.Adapter <QuoteAdapter.MyViewHolder>{

Context context;
List<QuoteData> data;
List<String> olddatabid = new ArrayList<String>();
List<String> newdatabid = new ArrayList<String>();
List<String> olddataask = new ArrayList<String>();
List<String> newdataask = new ArrayList<String>();

class MyViewHolder extends RecyclerView.ViewHolder{

    TextView time,symbol,sellmax,selllow,buymax,buylow,buy,sell,spread,lowtext,hightext;
    LinearLayout layout,layoutbid,layoutask;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        time = itemView.findViewById(R.id.TVTime);
        symbol = itemView.findViewById(R.id.TVSymbol);
        sellmax = itemView.findViewById(R.id.TVSELLMAX);
        selllow = itemView.findViewById(R.id.TVSELLLOW);
        buymax = itemView.findViewById(R.id.TVBUYMAX);
        buylow = itemView.findViewById(R.id.TVBUYHIGH);
        buy = itemView.findViewById(R.id.TVBUY);
        sell = itemView.findViewById(R.id.TVSELL);
        spread = itemView.findViewById(R.id.TVSpread1);
        lowtext = itemView.findViewById(R.id.low);
        hightext = itemView.findViewById(R.id.high);
        layout = itemView.findViewById(R.id.layout);
        layoutbid = itemView.findViewById(R.id.LLBid);
        layoutask = itemView.findViewById(R.id.LLAsk);
    }
}

public QuoteAdapter(Context context,List<QuoteData> data)
{
    this.data = data;
    this.context = context;
}


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.quotelist,viewGroup,false);
    return new MyViewHolder(view);
}


@SuppressLint("ResourceAsColor")
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
    final QuoteData data1 = data.get(i);

    myViewHolder.time.setText(data1.dLut);
    myViewHolder.symbol.setText(data1.dSymbol);
    myViewHolder.sellmax.setText(data1.dBid);  //Bid
    myViewHolder.selllow.setText(data1.dLow);
    myViewHolder.buymax.setText(data1.dAsk);  //ask
    myViewHolder.buylow.setText(data1.dHigh);

    if (newdatabid.size()< data.size())
    {
        newdatabid.add(data1.dBid);  //Insert Value in array for the first time
    }

    if (olddatabid.size()< data.size())
    {
        olddatabid.add(data1.dBid);  //Insert Value in array for the first time
    }



    if (newdataask.size()< data.size())
    {
        newdataask.add(data1.dAsk);   //Insert Value in array for the first time
    }

    if (olddataask.size()< data.size())    //Insert Value in array for the first time
    {
        olddataask.add(data1.dAsk);
    }





    newdatabid.set(i,data1.dBid);   //Store Value in array
    newdataask.set(i,data1.dAsk);   //Store Value in array

    //Compare and perform Logic
    if (Float.valueOf(newdatabid.get(i)) > Float.valueOf(olddatabid.get(i)))
    {
        myViewHolder.sellmax.setTextColor(Color.BLUE);
    }

    if (Float.valueOf(newdatabid.get(i)) < Float.valueOf(olddatabid.get(i)))
    {
        myViewHolder.sellmax.setTextColor(Color.RED);
    }



    if (Float.valueOf(newdataask.get(i)) > Float.valueOf(olddataask.get(i)))
    {
        myViewHolder.buymax.setTextColor(Color.BLUE);
    }

    if (Float.valueOf(newdataask.get(i)) < Float.valueOf(olddataask.get(i)))
    {
        myViewHolder.buymax.setTextColor(Color.RED);
    }


    olddatabid.set(i,newdatabid.get(i));
    olddataask.set(i,newdataask.get(i));

            }
        });
    }
}


@Override
public int getItemCount() {
    return data.size();
}
}