从Recyclerview中删除项目

从Recyclerview中删除项目

本文介绍了时间到期后,从Recyclerview中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在时间到期时删除一个项目,部分是通过引用此

I need to remove an item when time expired, partially i got output by referring this How to handle multiple countdown timers in ListView?

滚动列表后,一些过期的项目不会被删除,时间也不会更新.

After scrolled the list, some of the time expired items are not removed and times are not updated.

package com.techno.deliveryboy.Adapter;

import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.techno.deliveryboy.Pojos.DummyTimer;
import com.techno.deliveryboy.R;

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

/**
 * Created by developer on 23/7/16.
 */
public class Incoiming_orders_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {

    private LayoutInflater lf;
    Boolean onBind = true;
    private List<MyViewHolder> lstHolders;
    ArrayList<Integer> unbindvalues=new ArrayList<>();
    private Handler mHandler = new Handler();
    List<DummyTimer> lstProducts=new ArrayList<>();
    private Runnable updateRemainingTimeRunnable = new Runnable() {
        @Override
        public void run() {
            synchronized (lstHolders) {
                long currentTime = System.currentTimeMillis();

                for (MyViewHolder holder : lstHolders) {

                    holder.updateTimeRemaining(currentTime);


                }


            }
        }
    };

    public Incoiming_orders_Adapter(FragmentActivity activity, List<DummyTimer> lstProduct) {

        lstHolders = new ArrayList<>();
        lstProducts=lstProduct;

        startUpdateTimer();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.incoming_orders_item, parent, false);
        return new MyViewHolder(itemView);
    }

    private void startUpdateTimer() {
        Timer tmr = new Timer();
        tmr.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.post(updateRemainingTimeRunnable);

            }
        }, 1000, 1000);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
      if(holder instanceof MyViewHolder){
          onBind=true;
          MyViewHolder holder1= (MyViewHolder) holder;
          synchronized (lstHolders) {
              lstHolders.add(holder1);

           }
          holder1.setData(lstProducts.get(position),position);
          onBind=false;

      }
    }

    @Override
    public int getItemCount() {
        return lstProducts.size();
    }
    public void removeAt(int position) {

       /* for (Iterator<MyViewHolder> iterator = lstHolders.iterator(); iterator.hasNext();) {

           iterator.remove();
        }*/


        if(!onBind) {
            if(unbindvalues!=null&&unbindvalues.size()!=0&&position>=0){
                for (Integer i:unbindvalues) {
                    lstProducts.remove(position);
                    notifyItemRemoved(position);
                }
                unbindvalues.clear();

            }else {
                if(position>=0){
                    lstProducts.remove(position);
                    notifyItemRemoved(position);
                }

            }




            notifyDataSetChanged();
        }else {
            System.out.println("+Bind->true+");
            if(position>=0)
            unbindvalues.add(position);
        }
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, tvTimeRemaining, genre;
        DummyTimer mProduct;
        public void setData(DummyTimer item, int position) {
            mProduct = item;
            title.setText(item.name);
            updateTimeRemaining(System.currentTimeMillis());
        }

        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.Item_Totalvalue);
            tvTimeRemaining= (TextView) view.findViewById(R.id.Item_timeout);
        }

        public void updateTimeRemaining(long currentTime) {
            long timeDiff = mProduct.expirationTime - currentTime;
            if (timeDiff > 0) {
                int seconds = (int) (timeDiff / 1000) % 60;
                int minutes = (int) ((timeDiff / (1000 * 60)) % 60);
                int hours = (int) ((timeDiff / (1000 * 60 * 60)) % 24);
                tvTimeRemaining.setText(hours + " hrs " + minutes + " mins " + seconds + " sec");
            } else {
              // tvTimeRemaining.setText(String.valueOf(getAdapterPosition()));


                removeAt(getAdapterPosition());

            }
        }
    }

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

请发表您的评论.

推荐答案

您可以通过设置 CountDownTimer ,它将每秒触发一次onTick()方法.在 onTick() 方法中检查那些过期的对象?如果找到任何对象,请调用notifyItemRemoved()删除它们.

You can achieve this by setting up the CountDownTimer which will trigger onTick() method every second. In onTick() method check if there are any objects those are expired? If you find any object remove them by calling notifyItemRemoved().

以下是回收站视图适配器的代码:

私有类RecyclerViewAdapter扩展了RecyclerView.Adapter { 私有ArrayList mData;

private class RecyclerViewAdapter extends RecyclerView.Adapter { private ArrayList mData;

    public RecyclerViewAdapter(ArrayList<RowData> data) {
        mData = data;

        //find out the maximum time the timer
        long maxTime = System.currentTimeMillis();
        for (RowData item : mData) {
            maxTime = Math.max(maxTime, item.endTime);
        }

        //set the timer which will refresh the data every 1 second.
        new CountDownTimer(maxTime - System.currentTimeMillis(), 1000) {
            @Override
            public void onTick(long l) {
                for (int i = 0, dataLength = mData.size(); i < dataLength; i++) {
                    RowData item = mData.get(i);
                    item.timeRemaining -= 1000;
                }

                //remove the expired items
                Iterator<RowData> dataIterator = mData.iterator();
                while (dataIterator.hasNext()) {
                    RowData rd = dataIterator.next();
                    if (rd.timeRemaining <= 0) dataIterator.remove();
                }
                notifyDataSetChanged();
            }

            @Override
            public void onFinish() {
                mData.clear();
                notifyDataSetChanged();
            }
        }.start();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(android.R.layout.simple_list_item_2, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        RowData rowData = mData.get(position);
        holder.titleTv.setText(rowData.title);
        holder.remainingTimeTv.setText(millToMins(rowData.timeRemaining) + " mins remaining");
    }

    private String millToMins(long millisec) {
        return millisec / (60000) + ":" + (int) (millisec/1000) % (60);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView remainingTimeTv;
        private TextView titleTv;

        public MyViewHolder(View itemView) {
            super(itemView);
            remainingTimeTv = (TextView) itemView.findViewById(android.R.id.text2);
            titleTv = (TextView) itemView.findViewById(android.R.id.text1);
        }
    }

}

这篇关于时间到期后,从Recyclerview中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:46