更换recyclerview适配器中的项目

时间:2016-06-27 11:30:47

标签: android listview android-recyclerview android-adapter

我从ArrayList onBindViewHolder中的特定位置插入/删除。现在,我想在recyclerview上显示这个修改过的列表。

适配器代码:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyAdapterViewHolder> {
    private List<Info> dataList;
    private Context mAct;
    private List<Info> totalCandidatesList;
    private  String TAG = "OWD";

    public MyAdapter(List<Info> dataList, Context context) {
        this.dataList = dataList;
        this.mAct = context;
    }

    public void addApplications(List<Info> candidates) {

        if(this.totalCandidatesList == null){
            totalCandidatesList = new ArrayList<>();
        }
        this.dataList.addAll(candidates);
        this.totalCandidatesList.addAll(candidates);
        this.notifyItemRangeInserted(0, candidates.size() - 1);

    }

    public void clearApplications() {
        int size = this.dataList.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                dataList.remove(0);
                totalCandidatesList.remove(0);
            }
            this.notifyItemRangeRemoved(0, size);
        }
    }

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


    public void onBindViewHolder(MyAdapterViewHolder mAdapterViewHolder, int i) {

        if (i % 2 == 1) {
            mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#ecf5fe"));
            mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#ecf5fe"));
        } else {
            mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#e2f1ff"));
            mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#e2f1ff"));
        }


        final WorkHolders workHolders = SingleTon.getInstance().getWorkHolders();
        final String customerName = SingleTon.getInstance().getCustomerName();
        String siteName = null;
        if(customerName !=null) {
            String[] sitenamearray = customerName.split("--");

            if (sitenamearray.length > 1) {
                siteName = sitenamearray[1];
            }
        }
        final Info ci = dataList.get(i);
        mAdapterViewHolder.title.setText(ci.heading1);
        mAdapterViewHolder.jobNumber.setText(ci.heading2);
        mAdapterViewHolder.distance.setText(ci.distance);

        if(siteName != null && siteName.equalsIgnoreCase(ci.heading2)) {
            mAdapterViewHolder.cardView.setCardBackgroundColor(Color.parseColor("#a7ffeb"));
            mAdapterViewHolder.layoutRipple.setBackgroundColor(Color.parseColor("#a7ffeb"));
            if(i!=0){


>  //Here i removed and inserted item in list . 
>                     dataList.remove(i);
>                     dataList.add(0,ci);

            }
      }

        final String finalSiteName = siteName;
        final Bundle bundle = new Bundle();

        mAdapterViewHolder.layoutRipple.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment;

                String name = ci.heading1 + "--" + ci.heading2;

                Log.d(TAG,"new Jobname : "+ name);
                if (finalSiteName == null || finalSiteName.equalsIgnoreCase("")) {

                    bundle.putString("name", customerName);
                    bundle.putString("oldwork", "yes");
                    bundle.putString("running_job_selected", "yes");
                } else {

                    Log.d(TAG,"StartedOn Before Sending Bundle :" + workHolders.startedOn);
                    Log.d(TAG, "running Job is not selected");

                    bundle.putString("name", name);
                    bundle.putString("oldwork", "yes");
                    bundle.putString("running_job_selected", "no");
                }

                FragmentTransaction ft = ((FragmentActivity) mAct).getSupportFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.glide_fragment_horizontal_in, R.anim.glide_fragment_horizontal_out);

                fragment = new WorkDescriptionFragment();

                fragment.setArguments(bundle);
                ft.addToBackStack("myadapter");
                ft.replace(R.id.content_frame, fragment).commit();
                SingleTon.getInstance().setWorkStatus("start");
            }
        });

    }

    @Override
    public MyAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.single_item1, viewGroup, false);

        return new MyAdapterViewHolder(itemView);
    }

    public static class MyAdapterViewHolder extends RecyclerView.ViewHolder {

        protected TextView title;
        protected TextView dateTime;
        protected TextView distance;
        protected TextView jobNumber;

        protected CardView cardView;
        protected LayoutRipple layoutRipple;

        public MyAdapterViewHolder(View v) {
            super(v);
            title = (TextView) v.findViewById(R.id.title);
            dateTime = (TextView) v.findViewById(R.id.dateTimeTextView);
            distance = (TextView) v.findViewById(R.id.distanceTextView);
            jobNumber = (TextView) v.findViewById(R.id.jobNumber);
            cardView = (CardView) v.findViewById(R.id.cv);
            layoutRipple = (LayoutRipple)v.findViewById(R.id.singleitemripple);
        }
    }

}

您将在上面的代码中看到以下行,我将在视图中的列表中删除/插入项目,并希望在recyclerview中显示相同的内容。

  

但是现在我正在获得正常的数据主义者(未更改)。

//Here i removed and inserted item in list . 
                      dataList.remove(i);
                      dataList.add(0,ci);

请帮助我实现这一目标。

1 个答案:

答案 0 :(得分:1)

onBindViewHolder不是您应该更新适配器的地方。 staregy是更新Adapter数据列表中的项目,然后是notifyDataChanged()。例如,这是更新适配器内信息的方法:

    public void update(Track track) {
        tracks.remove(track);
        add(track);
    }

    public void add (Track track) {
        tracks.add(track);
        this.notifyDataSetChanged();
    }

    public void addTracks(List<Track> tracks){
        this.tracks.addAll(tracks);
        this.notifyDataSetChanged();
    }

    public void clearAndAddTracks(List<Track> tracks) {

        for (int i = 0; i < this.tracks.size(); i++) {
            if (!this.tracks.get(i).isRunning()){

            }
        }
        this.tracks.clear();
        this.tracks.addAll(tracks);
        this.notifyDataSetChanged();
    }