如何在recycleview适配器中设置布局可见性,我的适配器代码是

时间:2017-01-19 09:32:40

标签: android android-layout android-recyclerview android-adapter

@Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i)
{
    // below I put layout show hide logic.this logic working but when i scroll the layout disturb. 
    if (month_value.equalsIgnoreCase("") || !(month_value.equalsIgnoreCase(ci.monthgrpmore_str))) {
        month_value = ci.monthgrpmore_str;

        contactViewHolder.dfgnew.setVisibility(View.VISIBLE);
        contactViewHolder.product_header2.setVisibility(View.VISIBLE);
    } else {
        contactViewHolder.dfgnew.setVisibility(View.GONE);
        contactViewHolder.product_header2.setVisibility(View.GONE);
    }

    if (Global_Data.target_grpby.equalsIgnoreCase("By Product")) {
        contactViewHolder.month_grpby.setText("Product Sub Category");
    }
}

1 个答案:

答案 0 :(得分:0)

  // I find answer of my problem just replace some code in onBindViewHolder

  if (month_value.equalsIgnoreCase("") || !(month_value.equalsIgnoreCase(ci.monthgrpmore_str))) {
                    month_value = ci.monthgrpmore_str;
                    contactViewHolder.dfgnew.setVisibility(View.VISIBLE);
                    contactViewHolder.product_header2.setVisibility(View.VISIBLE);
                } else {
                    contactViewHolder.dfgnew.setVisibility(View.GONE);
                    contactViewHolder.product_header2.setVisibility(View.GONE);
                }

                //replace  above cobe with below code inside onBindViewHolder


if (i > 0 && !(contactList.get(i).monthgrpmore_str.equalsIgnoreCase(contactList.get(i-1).monthgrpmore_str))) {
    month_value = ci.monthgrpmore_str;
    contactViewHolder.dfgnew.setVisibility(View.VISIBLE);
    contactViewHolder.product_header2.setVisibility(View.VISIBLE);
} else {
    if(i != 0)
    {
        contactViewHolder.dfgnew.setVisibility(View.GONE);
        contactViewHolder.product_header2.setVisibility(View.GONE);
    }


}

// And my full adapter code is below.


 public class GrpbyMore_Adapter extends RecyclerView.Adapter<GrpbyMore_Adapter.ContactViewHolder> {

        int a=0;
        String month_value = "";
        private List<TargetValue_info> contactList;
        private List<TargetValue_info> contactListnew;
        public GrpbyMore_Adapter(List<TargetValue_info> contactList) {
            this.contactList = contactList;
            this.contactListnew = contactList;
        }

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

        @Override
        public void onBindViewHolder(ContactViewHolder contactViewHolder, int i)
        {

            ++a;
    //        if(a <= contactListnew.size()) {

                TargetValue_info ci = contactList.get(i);
    //            if (contactViewHolder != null) {
                    contactViewHolder.setIsRecyclable(false);
                    contactViewHolder.prdcatg_more.setText(ci.prdcatg_morestr);
                    contactViewHolder.month_more.setText(ci.monthgrpmore_str);




                    if (month_value.equalsIgnoreCase("") || !(month_value.equalsIgnoreCase(ci.monthgrpmore_str))) {
                        month_value = ci.monthgrpmore_str;
                        contactViewHolder.dfgnew.setVisibility(View.VISIBLE);
                        contactViewHolder.product_header2.setVisibility(View.VISIBLE);
                    } else {
                        contactViewHolder.dfgnew.setVisibility(View.GONE);
                        contactViewHolder.product_header2.setVisibility(View.GONE);
                    }


                    if (Global_Data.target_grpby.equalsIgnoreCase("By Product")) {
                        contactViewHolder.month_grpby.setText("Product Sub Category");
                    }

        }

        @Override
        public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View itemView = null;

            itemView = LayoutInflater.
                    from(viewGroup.getContext()).
                    inflate(R.layout.summary2_adapter, viewGroup, false);

                return new ContactViewHolder(itemView);


        }

        public static class ContactViewHolder extends RecyclerView.ViewHolder {

            protected TextView prdcatg_more;
            protected TextView month_more;


            public ContactViewHolder(View v) {
                super(v);
                prdcatg_more =  (TextView) v.findViewById(R.id.prdcatg_more);
                month_grpby =  (TextView) v.findViewById(R.id.month_grpby);

                dfgnew = (LinearLayout) v.findViewById(R.id.dfgnew);
                product_header2 = (LinearLayout) v.findViewById(R.id.product_header2);
            }
        }
    }