mercredi 22 mai 2019

setChecked and setSelected in CheckBox not working

I have an Activity which has 5 fragments in pager. I used

 viewPager.setOffscreenPageLimit(fragmentList.size());

to be able to creat all fragment in one time I also have a listener wich will pass parameter(object) to all fragment once I have a newIntent.

in one of my fragment I have CheckBox wich should be selcted according to the parameter from activity. and other views wich I were able to change the value of thier texts and background. just this check box I have set it on But when I see it, I have see it is off , all other events works well.

here is the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppTheme_PopupOverlay);
    setContentView(R.layout.activity_configurations);

    ViewPager viewPager = findViewById(R.id.container);
    TabLayout tabLayout = findViewById(R.id.tabs);

    setNdef(getIntent().getExtras());
    String type = getNdef().getString("id");

    List<Fragment> fragmentList = createFragments(type);
    SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), fragmentList);
    viewPager.setAdapter(pagerAdapter);

    fillTableLayout(tabLayout, type);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    viewPager.setOffscreenPageLimit(fragmentList.size());
    readNfcDialog = createReadDialog();
    writeNfcDialog = createWriteDialog();
    readNfcDialog.show();
    setForeground();
}

 @Override
protected void onNewIntent(Intent intent) {

    super.onNewIntent(intent);


    ClassNfcTag wepTag = new ClassNfcTag(intent);
    String type = wepTag.defineClassTag();
    OmsRepeaterTag omsRepeaterTag = new OmsRepeaterTag(intent);
    byteMap = omsRepeaterTag.readTag();
   if (byteMap != null) {
   setTagOmsConfiguration(OmsConfiguration.fromByteArray(byteMap));
   for (OmsListener listener : onReceiveOmsList) {
        listener.gotOms(getInTagOmsConfiguration());
       }
   }
}

and here is one of the fragments

    @Override
public void gotOms(OmsConfiguration configuration) {
    setConfiguration(configuration);
    if (isAdded()) {
    boolean status = getConfiguration().getDeleteRsl();
    delete.setChecked(status);
    delete.setSelected(status);

    }
}

as a solution I have tried to use Switch instead of CheckBox and it worked well.

and I also tried to set the pager to start from exactly the fragment which has the CheckBox and it also work.

viewPager.setCurrentItem(checkBoxFragmentPosition);

I also tried to debugg the code and It shows me that the checBox is checked and when I tried to change it to checked by touching it (progamitly it is checked , in Ui I see it unchecked) it change itself from unchecked to unchecked .and then with the next touch changed to checked.

I will be really happy to see your solutions.




Aucun commentaire:

Enregistrer un commentaire