Good day everyone, I have an Activity that hosts 4 Fragments with a PagerAdapter. In 2 of those fragments, I use a Custom ListView Adapter to show data (a row consists of a checkbox and a textview). The user can then either send or delete the selected rows (marked by the checkbox) using buttons on the fragment itself. Now, in my Custom ListView Adapter, I note down whenever the check status of the checkbox changes as such:
(Note: This happens on the getView function)
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e(TAG, "onCheck changed at position = " + position + " , boolean = " + isChecked);
Log.e(TAG, "context = " + context);
android.support.v4.app.Fragment fragment = getVisibleFragment();
String fragmentName = fragment.getClass().getSimpleName();
Log.e(TAG, "fragmentName = " + fragmentName);
if(fragmentName.equals("RecordsSent")) {
RecordsSent rsFragment = (RecordsSent) fragment;
Log.e(TAG, "null check on fm = " + fragment);
Log.e(TAG, "null check on rsFragment = " + rsFragment);
if(rsFragment != null)
rsFragment.updateCheckBox(position, isChecked);
}
else{
RecordsPending rpFragment = (RecordsPending) fragment;
if(rpFragment != null)
rpFragment.updateCheckBox(position, isChecked);
}
}
});
What the code basically does is to determine which fragment is visible by using the getVisibleFragment() function. After that, based on which ever fragment was detected, I call that fragment up and then fire a function within that fragment and mark which checkbox just got checked/unchecked.
This was working really well for quite some time until the time I added the 4th Fragment. Suddenly, I was getting a class cast error inside my if-else block. Upon examining the log statements, it seemed that I was getting the other fragment as the visible one. This means that the fragmentName variable would be RecordsSent if RecordsPending was visible and vice versa. I don't know how or why that happened.
Sure I can just "reverse" the logic and put an ! in my if statement BUT the null checks would return true and my function inside the fragments WON'T get called at all.
I don't think there was something wrong with how I added my fragments.
Here is the code I used to make the fragments:
TextView tv1 = createTab("Sent", new ColorDrawable(Color.parseColor("#006633")));
TextView tv2 = createTab("Pending", new ColorDrawable(Color.parseColor("#CCCC00")));
TextView tv3 = createTab("Daily Sales Report", new ColorDrawable(Color.parseColor("#F7C05F")));
TextView tv4 = createTab("News Records", new ColorDrawable(Color.parseColor("#B72153")));
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setCustomView(tv1).setTag("Sent"));
tabLayout.addTab(tabLayout.newTab().setCustomView(tv2).setTag("Pending"));
tabLayout.addTab(tabLayout.newTab().setCustomView(tv3).setTag("Sales"));
tabLayout.addTab(tabLayout.newTab().setCustomView(tv4).setTag("News"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//more code here
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter (getSupportFragmentManager(), tabLayout.getTabCount());
Log.e(TAG, "tab count = " + tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
And then my PagerAdapter class looks like this:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
RecordsSent tab1 = new RecordsSent();
return tab1;
case 1:
RecordsPending tab2 = new RecordsPending();
return tab2;
case 2:
RecordsSales tab3 = new RecordsSales();
return tab3;
case 3:
RecordsNews tab4 = new RecordsNews();
return tab4;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Now I'm not quite sure how or why I'm getting the wrong visible fragment. As I mentioned before, this code was working well and I don't know how to go around this. If anyone has a their way of checking which fragment is visible, I'd be very grateful.
Any help will be very much appreciated. Thanks in advanced.
Aucun commentaire:
Enregistrer un commentaire