RecyclerView适配器问题

时间:2017-10-17 10:17:47

标签: android android-recyclerview android-checkbox

在循环视图的适配器视图中加载数据时出现问题。

我在活动中有两个checkbox,第二个recycleview有3个回收视图。但问题是我在原始视图持有者中有public class VenueOrderPriceAdapter extends RecyclerView.Adapter<VenueOrderPriceAdapter.DataViewHolder> { static Context mContext; public static double final_total; public static String selected_hr,selected_min; static private List<VenueOrderPriceModel> stList; static private String str_id, str_charges, str_is_flat_charges, str_is_per_person_charges; /*str_hour_extension_charges, str_extra_person_charges=null, str_is_group_size, str_group_size_from, str_group_size_to*/; private static boolean isPkgAdded; public VenueOrderPriceAdapter(Context mContext, List<VenueOrderPriceModel> students) { this.mContext = mContext; this.stList = students; } // Create new views @Override public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_venueorder_price, parent,false); Log.e("on create called---","dsdf--111"); // create ViewHolder DataViewHolder viewHolder = new DataViewHolder(itemLayoutView); final_total=0; return viewHolder; } @Override public void onBindViewHolder(final DataViewHolder viewHolder, final int positiona) { Log.e("on bind called---","dsdf"+positiona); viewHolder.setIsRecyclable(false); } // Return the size arraylist @Override public int getItemCount() { return stList.size(); } public static class DataViewHolder extends RecyclerView.ViewHolder { public TextView tvName,tv_details,tv_duration ,tv_guestCount,tv_price,tv_extra_charge,tv_addtocart,tv_pkg_rate; CheckBox cb_selectedprice; private LinearLayout ll_extra,ll_extra_charge; Spinner sp_hh,sp_mm,sp_qty; ImageView img_clock,img_guest,img_remove; public DataViewHolder(View itemLayoutView) { super(itemLayoutView); tvName = (TextView) itemLayoutView.findViewById(R.id.tvName); tv_details = (TextView) itemLayoutView.findViewById(R.id.tv_details); tv_duration = (TextView) itemLayoutView.findViewById(R.id.tv_duration); tv_guestCount = (TextView) itemLayoutView.findViewById(R.id.tv_guestCount); tv_price = (TextView) itemLayoutView.findViewById(R.id.tv_price); tv_extra_charge = (TextView)itemLayoutView.findViewById(R.id.tv_extra_charge); tv_pkg_rate = (TextView)itemLayoutView.findViewById(R.id.tv_pkg_rate); img_clock = (ImageView) itemLayoutView.findViewById(R.id.img_clock); img_guest = (ImageView) itemLayoutView.findViewById(R.id.img_guest); img_remove = (ImageView) itemLayoutView.findViewById(R.id.img_remove); sp_hh = (Spinner)itemView.findViewById(R.id.sp_hh); sp_mm = (Spinner)itemView.findViewById(R.id.sp_mm); sp_qty = (Spinner)itemView.findViewById(R.id.sp_qty); // tv_ext_person= (TextView)itemView.findViewById(R.id.tv_ext_person); ll_extra =(LinearLayout)itemView.findViewById(R.id.ll_extra); ll_extra_charge =(LinearLayout)itemView.findViewById(R.id.ll_extra_charge); tv_addtocart= (TextView) itemView.findViewById(R.id.tv_addtocart); cb_selectedprice =(CheckBox)itemView.findViewById(R.id.cb_selectedprice); final int position = getAdapterPosition(); String extra_duration_charge = ""; str_id= stList.get(position).getId(); if(stList.get(position).getIs_applicable().equalsIgnoreCase("0")){ itemView.setEnabled(false); itemView.setClickable(false); itemView.setBackgroundResource(R.color.md_blue_grey_50); tv_addtocart.setVisibility(View.GONE); } tvName.setText(stList.get(position).getPackage_name()); tv_details.setText(stList.get(position).getChrages_inclusion()); tv_price.setText(" $ "+Const.GLOBAL_FORMATTER.format(Double.parseDouble(stList.get(position).getCharges()))); if(stList.get(position).getPacakage_hours()!=null){ tv_duration.setText(stList.get(position).getPacakage_hours().substring(0,stList.get(position).getPacakage_hours().length()-3)+" hr"); }else{ tv_duration.setVisibility(View.GONE); } if(stList.get(position).getIs_group_charges().equalsIgnoreCase("1")){ tv_guestCount.setText(stList.get(position).getGroup_size_from() +"-" +stList.get(position).getGroup_size_to() +" Guest"); }else{ tv_guestCount.setVisibility(View.GONE); } //======EXTRA GUEST AND TIME CONDITION FOR TEXT VIEW ========================================== if(stList.get(position).getIs_hour_extension_charges()!=null) { if(stList.get(position).getIs_hour_extension_charges().equalsIgnoreCase("1")){ ll_extra_charge.setVisibility(View.VISIBLE); if (stList.get(position).getExtension_hours().substring(0, 2).equalsIgnoreCase("00")) { extra_duration_charge = "$ " + stList.get(position).getHour_extension_charges() + " / " + stList.get(position).getExtension_hours().substring(3, 5) + " min"; } else { extra_duration_charge = "$ " + stList.get(position).getHour_extension_charges() + " / " + stList.get(position).getExtension_hours().substring(0, 5) + " hours"; } tv_extra_charge.setText(extra_duration_charge); } } if(stList.get(position).getIs_group_charges().equalsIgnoreCase("1") && stList.get(position).getIs_extra_person_charges().equalsIgnoreCase("1")){ extra_duration_charge = extra_duration_charge+"\n"+"$"+stList.get(position).getIs_extra_person_charges()+"/ person"; tv_extra_charge.setText(extra_duration_charge); } if(extra_duration_charge==null || extra_duration_charge.length()<1){ ll_extra_charge.setVisibility(View.GONE); if(stList.get(position).getIs_flat_charges().equalsIgnoreCase("1")){ tv_pkg_rate.setText("Flat Rate"); } else if(stList.get(position).getIs_perperson_charges().equalsIgnoreCase("1")){ tv_pkg_rate.setText("Per Person"); } else if(stList.get(position).getIs_perhour_charges().equalsIgnoreCase("1")) { tv_pkg_rate.setText("Per Hour"); } }else{ if(stList.get(position).getIs_flat_charges().equalsIgnoreCase("1")){ tv_pkg_rate.setText("Flat Rate"); } else if(stList.get(position).getIs_perperson_charges().equalsIgnoreCase("1")){ tv_pkg_rate.setText("Per Person"); } else if(stList.get(position).getIs_perhour_charges().equalsIgnoreCase("1")) { tv_pkg_rate.setText("Per Hour"); } } //==========FLAT PER PERSON PER HOURE CHARGES CONDITION====================================================== if(stList.get(position).getIs_flat_charges().equalsIgnoreCase("1")){ ll_extra.setVisibility(View.INVISIBLE); // tv_ext_person.setVisibility(View.GONE); } else if(stList.get(position).getIs_perperson_charges().equalsIgnoreCase("1")){ if(stList.get(position).getIs_group_charges().equalsIgnoreCase("1") && stList.get(position).getExtra_person_charges()!=null){ ll_extra.setVisibility(View.VISIBLE); img_clock.setVisibility(View.GONE); sp_hh.setVisibility(View.GONE); //np_itemcount.setVisibility(View.VISIBLE); // tv_ext_person.setVisibility(View.VISIBLE); //np_itemcount.setMaxValue(Integer.parseInt(stList.get(position).getGroup_size_to())); }else{ ll_extra.setVisibility(View.INVISIBLE); //tv_ext_person.setVisibility(View.GONE); } if(stList.get(position).getIs_extra_person_charges().equalsIgnoreCase("0")){ ll_extra.setVisibility(View.INVISIBLE); //tv_ext_person.setVisibility(View.GONE); } }else if(stList.get(position).getIs_perhour_charges().equalsIgnoreCase("1")) { ll_extra.setVisibility(View.VISIBLE); img_guest.setVisibility(View.GONE); sp_qty.setVisibility(View.GONE); //ll_duration.setVisibility(View.GONE); } List<String> list_hh = new ArrayList<String>(); list_hh.add("HH"); list_hh.add("01"); list_hh.add("02"); list_hh.add("03"); list_hh.add("04"); list_hh.add("05"); list_hh.add("06"); list_hh.add("07"); list_hh.add("08"); list_hh.add("09"); list_hh.add("10"); list_hh.add("11"); list_hh.add("12"); ArrayAdapter<String> dataAdapter_hh = new ArrayAdapter<String>(mContext,android.R.layout.simple_spinner_item, list_hh); dataAdapter_hh.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp_hh.setAdapter(dataAdapter_hh); List<String> list_mm = new ArrayList<String>(); list_mm.add("MM"); list_mm.add("00"); list_mm.add("15"); list_mm.add("30"); list_mm.add("45"); ArrayAdapter<String> dataAdapter_mm = new ArrayAdapter<String>(mContext,android.R.layout.simple_spinner_item, list_mm); dataAdapter_mm.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp_mm.setAdapter(dataAdapter_mm); List<String> list_qty = new ArrayList<String>(); list_qty.add("Guest"); for(int a=0;a<100;a++){ list_qty.add(Integer.toString(a)); } ArrayAdapter<String> dataAdapter_qty = new ArrayAdapter<String>(mContext,android.R.layout.simple_spinner_item, list_qty); dataAdapter_qty.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp_qty.setAdapter(dataAdapter_qty); sp_hh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int positionSP, long id) { if(sp_mm.getSelectedItemPosition()>1 && positionSP>1){ final_total = Double.parseDouble(stList.get(position).getCharges())+(Double.parseDouble(stList.get(position).getCharges()) * (double) (sp_hh.getSelectedItemPosition() +1) ); }else{ if(positionSP > 1) { final_total = Double.parseDouble(stList.get(position).getCharges())+(Double.parseDouble(stList.get(position).getCharges()) * (double) (sp_hh.getSelectedItemPosition())); }else{ final_total = Double.parseDouble(stList.get(position).getCharges()); } } /* Toast.makeText(parent.getContext(), "Time : " + sp_hh.getItemAtPosition(sp_hh.getSelectedItemPosition()).toString() + ":" +String.valueOf(sp_mm.getSelectedItem()), Toast.LENGTH_SHORT).show();*/ } @Override public void onNothingSelected(AdapterView<?> parent) { } }); sp_mm.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int positionSP, long id) { if(positionSP>1){ final_total = Double.parseDouble(stList.get(position).getCharges())+(Double.parseDouble(stList.get(position).getCharges()) *(double) (sp_hh.getSelectedItemPosition() + 1 )); // tv_menutitle_venuepricing.setText("Pricing Plans "+" ( Total : "+final_total+" )"); }else{ if(sp_hh.getSelectedItemPosition()>0){ final_total =Double.parseDouble(stList.get(position).getCharges())+(Double.parseDouble(stList.get(position).getCharges()) * (double) (sp_hh.getSelectedItemPosition())); }else{ final_total = Double.parseDouble(stList.get(position).getCharges()); } } /*Toast.makeText(parent.getContext(), "Time : " + sp_hh.getItemAtPosition(sp_hh.getSelectedItemPosition()).toString() + ":" +sp_mm.getItemAtPosition(sp_mm.getSelectedItemPosition()).toString(), Toast.LENGTH_SHORT).show();*/ } @Override public void onNothingSelected(AdapterView<?> parent) { } }); /*chkSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(chkSelected.isChecked()==true){ checked_count=checked_count+1; if(checked_count > 1){ chkSelected.setChecked(false); checked_count=checked_count-1; Log.d("chk count ",""+checked_count); }else { Log.d("chk count",""+checked_count); if(stList.get(position).getIs_perperson_charges().equalsIgnoreCase("1")){ final_total = Double.parseDouble(stList.get(position).getCharges()) * Double.parseDouble(et_guestCount.getText().toString()); // tv_menutitle_venuepricing.setText("Pricing Plans "+" ( Total : "+final_total+" )"); }else if(stList.get(position).getIs_perhour_charges().equalsIgnoreCase("1")){ if(np_minutes_venue_pkg.getValue()>0){ final_total = Double.parseDouble(stList.get(position).getCharges()) * (double)(np_hour_venue_pkg.getValue()+1); // tv_menutitle_venuepricing.setText("Pricing Plans "+" ( Total : "+final_total+" )"); }else{ if(np_hour_venue_pkg.getValue()>0) { final_total = Double.parseDouble(stList.get(position).getCharges()) * (double) (np_hour_venue_pkg.getValue()); // tv_menutitle_venuepricing.setText("Pricing Plans " + " ( Total : "+ Const.GLOBAL_FORMATTER.format(final_total) + " )"); }else{ final_total = Double.parseDouble(stList.get(position).getCharges()); //tv_menutitle_venuepricing.setText("Pricing Plans " + " ( Total : "+ Const.GLOBAL_FORMATTER.format(final_total) + " )"); } } selected_hr= Integer.toString(np_hour_venue_pkg.getValue()); if(selected_hr.length()<=1){ selected_hr="0"+selected_hr; } selected_min=Integer.toString(np_minutes_venue_pkg.getValue()); if(selected_min.length()<=1){ selected_min="0"+selected_min; } }else if(stList.get(position).getIs_flat_charges().equalsIgnoreCase("1")){ if(stList.get(position).getIs_group_charges().equalsIgnoreCase("1") && stList.get(position).getIs_extra_person_charges().equalsIgnoreCase("1") ){ int guest_from =Integer.parseInt(stList.get(position).getGroup_size_from()); int guest_to =Integer.parseInt(stList.get(position).getGroup_size_to()); int guest =Integer.parseInt(et_guestCount.getText().toString()); if(guest>guest_to){ int extra_guest = guest-guest_to; final_total = ((double)extra_guest * Double.parseDouble(stList.get(position).getExtra_person_charges()) ) +Double.parseDouble(stList.get(position).getCharges()); // tv_menutitle_venuepricing.setText("Pricing Plans " + " ( Total : " + Const.GLOBAL_FORMATTER.format(final_total) + " )"); }else{ final_total = Double.parseDouble(stList.get(position).getCharges()); // tv_menutitle_venuepricing.setText("Pricing Plans " + " ( Total : "+ Const.GLOBAL_FORMATTER.format(final_total) + " )"); } }else { final_total = Double.parseDouble(stList.get(position).getCharges()); // tv_menutitle_venuepricing.setText("Pricing Plans " + " ( Total : "+ Const.GLOBAL_FORMATTER.format(final_total) + " )"); } } } }else{ checked_count=checked_count-1; Log.d("chk count",""+checked_count); if(checked_count==0){ // tv_menutitle_venuepricing.setText("Pricing Plans "); } } VenueOrderPriceModel contact = (VenueOrderPriceModel) chkSelected.getTag(); contact.setSelected(chkSelected.isChecked()); stList.get(getAdapterPosition()).setSelected(chkSelected.isChecked()); *//*Toast.makeText( chkSelected.getContext(), "Clicked on Checkbox: " + chkSelected.getText() + " is " + chkSelected.isChecked(), Toast.LENGTH_LONG).show();*//* } });*/ if (cb_selectedprice.isChecked()==true){ img_remove.setVisibility(View.VISIBLE); }else{ img_remove.setVisibility(View.GONE); } img_remove.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isPkgAdded=false; cb_selectedprice.setChecked(false); img_remove.setVisibility(View.GONE); tv_addtocart.setBackground(mContext.getResources().getDrawable(R.drawable.rounded_corner_orange_white_borde,mContext.getTheme())); tv_addtocart.setEnabled(true); final_total=0; } }); tv_addtocart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isPkgAdded == true || cb_selectedprice.isChecked()==true) { Toast.makeText(mContext, "First Remove Added Pacakge", Toast.LENGTH_LONG).show(); } else { Log.e("boooking details --",str_dateselected+str_guest_count+str_timeslot); } } }); } } // method to access in activity after updating selection public List<VenueOrderPriceModel> getStudentist() { return stList; } } ,我检查了其中一个。当我向上/向下滚动时,!temp.equals("**")数据的数据重新排列为默认值,所有复选框再次为假。

我在绑定方法中使用了set recycle(false)但仍无效。

if (!temp.equals("**")) {
                picklistDogs.add(temp);
            } else {
                break;
            }

2 个答案:

答案 0 :(得分:0)

您必须从每个位置的复选框w的onCheckChangeListener维护数据模型中每个复选框的状态。

然后你必须在适配器的onBind方法中设置复选框状态

答案 1 :(得分:0)

Recycler视图回收OnBindViewHolder中的视图。所以当项目滚动时,视图会再次被回收。要解决此问题。

创建一个全局变量来存储点击的位置。

private mItemSelected=-1;

然后在视图内部添加clickListener并onClick存储所单击项目的位置。

public class ViewHolder extends RecyclerView.ViewHolder {

    public ViewHolder(View v) {
        super(v);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemSelected = getAdapterPosition();
                notifyDataSetChanged();

            }
        });
    }
}

在OnBindViewHolder内部,

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    if(mItemSelected==position){
        holder.status.setChecked(true)‌;
    }else{
        holder.status.setChecked(false)‌;
    }
}