Search code examples
androidandroid-viewpagertogglebutton

Toggle Button State in ViewPager when a page is removed


I have a toggle button (in all pages) to allow users to like the contents of a page (of viewpager).

When user unlikes a page, the page gets removed from the viewpager.

Below is the cycle

User "Unlikes" -> Updates SQLite -> NotifyDatasetChanged() -> Fragments Rebuilt -> Viewpager displayed

The issue is suppose I am in position 2 and I "unlike" - The page gets removed - in its place a new page is placed with the same toggle button state as that of the removed page, whereas I expect the togglebutton state to be set based on actual value returned by the Cursor.

Even though "isChecked()" status of the ToggleButton is "true" and is being returned in getItem() as a part of rootview - and is also being displayed in a mock TextView I created - Somehow the "Checked" State is retained from the removed page.

Adapter

public class CursorPagerAdapter extends FragmentStatePagerAdapter {
    .
    .
    public Fragment getItem(int position) {
        Fragment fragment = null;

            if (mCursor.moveToPosition(position)) {
                Bundle arguments = new Bundle();
                fragment = new myDetailFragment();
                fragment.setArguments(arguments);
            }
        return fragment;

    }
}

Population

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            ToggleButton addFav = (ToggleButton) rootView.findViewById(R.id.addFavorite);

            .
            .

             if (item_status.equals("0")) {
                addFav.setChecked(false);
                afw.setBackgroundColor(Color.GRAY);
             } else (item_status.equals("1")) {
                addFav.setChecked(true);
                afw.setBackgroundColor(Color.RED);
             }

            .
            .

            addFav.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final Context ctx = getActivity();
                        ContentValues values = new ContentValues(1);
                        SharedPreferences prefs = getActivity().getSharedPreferences("com.dap.qgit", Context.MODE_PRIVATE);
                        ToggleButton tb = (ToggleButton) v;
                        if (tb.isChecked()) {
                            values.put(DataProvider.COL_ITEM_STATUS, "1");
                        } else {
                            values.put(DataProvider.COL_ITEM_STATUS, "0");
                        }

                        String[] args = new String[1];
                        args[0] = "" + tb.getTag();
                        ctx.getContentResolver().update(DataProvider.CONTENT_URI_ITEM, values, "item_id=?", args);
                        getContext().getContentResolver().notifyChange(DataProvider.CONTENT_URI_TAG_ITEMS,null);

                    }
                });
      }

Solution

  • First please read "Martin Albedinsky" reply - Thanks to him for helping me isolate the issue.

    I used the below in my layout XML to circumvent the issue:

                     <ToggleButton
                            .
                            .
                            .
                            android:saveEnabled="false"
                            .
                            .
                            .
                      >
    

    Though I have not personally tried Martin's solution - which by his explanation appears to be a cleaner approach.

    I am yet to ascertain if this solution that worked for me causes any performance/untoward issues.