Recyclerview有时不匹配适配器值

时间:2019-04-28 16:13:08

标签: android

我正在开发基于GCC的android应用程序。我有一个Recyclerview,可获取所有国家/地区的详细信息,包括国家/地区名称,代码,图像等。 在这里,我得到了响应并设置为视图。但是当我单击视图时,没有获得该“适配器位置”的正确值。

private void getCountryList(){

    if (AppUtils.isConnectedToInternet(this, true)) {
        Callback<CountryResponse> callback = new Callback<CountryResponse>() {
            @Override
            public void onResponse(Response<CountryResponse> response) {
                int set_send_pos = 0;
                Log.e("api", ".....getCountryList......." + response.raw() + "...." + response.raw().request().url());
                Log.e("api", ".....getCountryList......." + new Gson().toJson(response.body()));
                dataBeans.addAll(response.body().getData());

                int selected = pref.getInt(AppUtils.PROPERTY_COUNTRY_CODE, 1);
                for (int i = 0; i < dataBeans.size(); i++) {
                    if (dataBeans.get(i).getId() == selected) {
                        set_send_pos = i;
                        selectedCountryBean = dataBeans.get(i);

                        pref.edit().putString(AppUtils.PROPERTY_COUNTRY_PHONE, PHONES[i]).apply();
                    }
                }

                countryAdapter = new CountryAdapter(CountryActivityNew.this, dataBeans, new CountryAdapter.FlagClickListener() {
                    @Override
                    public void onFlagClicked(CountryResponse.DataBean pos, int poss) {
                        onFlagSelected(pos, poss);
                    }
                }, set_send_pos);


                rv_country.setLayoutManager(new GridLayoutManager(CountryActivityNew.this, 2));
                rv_country.setAdapter(countryAdapter);

            }

            @Override
            public void onFailure(Throwable t) {

            }
        };
        ApiManager.getApi().getService().getCountryList(Constants.GET_COUNTRY_LIST).enqueue(callback);
    }

}

回收站适配器类

@Override     public void onBindViewHolder(最终的CountryViewHolder持有人,最终的int位置){

    final CountryResponse.DataBean dataBean = mCountry.get(position);
    holder.tv_country.setText(mCountry.get(position).getName());



    Picasso.with(mContext).load(Constants.NEW_URL_BASE + mCountry.get(position).getFlag_image())
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .into(holder.img_country);

    DisplayMetrics dm = mContext.getResources().getDisplayMetrics();

    double density = dm.density * 160;
    double x = Math.pow(dm.widthPixels / density, 2);
    double y = Math.pow(dm.heightPixels / density, 2);
    double screenInches = Math.sqrt(x + y);
    int screenSize = (int) Math.round(screenInches);

    if (position == selected_position)
        holder.img_country_selected.setVisibility(View.VISIBLE);
    else
        holder.img_country_selected.setVisibility(View.INVISIBLE);

    holder.radio.setChecked(position == selected_position);

    holder.radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            RealmResults<Cart> cartList = realmUtils.getCart();
            RealmResults<Address> addressList = realmUtils.getAddressList();
            if(addressList.size()>0) {
                System.out.println("Changed Country-----");
                realmUtils.deleteAddressList();
            }
            if(cartList.size()>0) {
                System.out.println("Changed Country-----");
                realmUtils.deleteCart();
            }
            if (isChecked)
                clickListener.onFlagClicked(dataBean, selected_position);
        }
    });
}

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

public class CountryViewHolder extends RecyclerView.ViewHolder {

 //   @BindView(R.id.tv_country)
    TextView tv_country;
  //  @BindView(R.id.img_country_selected)
    ImageView img_country_selected;
  //  @BindView(R.id.img_country)
    ImageView img_country;
//    @BindView(R.id.radio)
    RadioButton radio;


    public CountryViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);

        radio=itemView.findViewById(R.id.radio);
        tv_country=itemView.findViewById(R.id.tv_country);
        img_country=itemView.findViewById(R.id.img_country);
        img_country_selected=itemView.findViewById(R.id.img_country_selected);

        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selected_position = getAdapterPosition();
                notifyDataSetChanged();
            }
        };

        itemView.setOnClickListener(clickListener);
        radio.setOnClickListener(clickListener);
    }


}

0 个答案:

没有答案