I have a mainactivity which includes a TabLayout with ViewPager
I have added 3 tabs and each tab has separate fragments which contains a recyclerview, and these recyclerviews has a checkbox that should be simultaneously updated/refreshed whenever i swipe the viewpager(i save checked positions in shared preference and updates via shared preference).
My problem here is whenever i check a checkbox in tab1, tab2 is not updating/refreshing until i scroll down the Recyclerview. and tab3 is working fine. and i am getting a weird warning in logcat too.
03-05 09:35:53.345 4317-4327/com.example.rubin W/art: Suspending all threads took: 6.805ms
03-05 09:35:58.310 4317-4317/com.example.rubin W/FragmentManager: moveToState: Fragment state for Tab3{10a5f1f0 #2 id=0x7f0d00b6} not updated inline; expected state 3 found 2
03-05 09:36:01.363 4317-4317/com.example.rubin W/FragmentManager: moveToState: Fragment state for Tab1{2d9aa887 #1 id=0x7f0d00b6} not updated inline; expected state 3 found 2
My PagerAdapter
public class PagerAdapter1 extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter1(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tab1();
case 1:
return new Tab2();
case 2:
return new Tab3();
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Tablayout OnPageChangelistener
final PagerAdapter1 adapter = new PagerAdapter1
(getSupportFragmentManager(), tabLayout1.getTabCount(), getApplicationContext());
viewPager1.setAdapter(adapter);
viewPager1.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout1));
tabLayout1.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager1.setCurrentItem(tab.getPosition());
adapter.notifyDataSetChanged();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
code for each fragments.
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_tab1_recycler, container, false);
RecyclerView rv = (RecyclerView) v.findViewById(R.id.home_recyclerview);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rv.setLayoutManager(llm);
rv.setHasFixedSize(true); // to improve performance
rv.setAdapter(new HomeManager()); // the projectdatabase manager is assigner to the RV
return v;
}
public class HomeManager extends RecyclerView.Adapter<HomeManager.RecyclerViewHolder> {
int Length,h;
View v1;
ArrayList<String> PROJECT_ID = new ArrayList<String>();
Set<String> set;
List<String> selected;
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
CheckBox mCheck;
RecyclerViewHolder(final View itemView) {
super(itemView);
mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
set = pref.getStringSet("FAV", null);
if (set != null) {
selected = new ArrayList<String>(set);
} else {
selected = new ArrayList<String>();
}
mCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
h = getAdapterPosition();
check = PROJECT_ID.get(h); // for saving the project id from json.
if (selected.contains(check)) {
selected.remove(check);
mCheck.setBackgroundResource(R.drawable.ic_favorite_white1_24dp);
Snackbar snackbar = Snackbar.make(v, "Property Unfavorited", Snackbar.LENGTH_SHORT);
snackbar.show();
} else {
selected.add(check);
mCheck.setBackgroundResource(R.drawable.ic_favorite_white_24dp);
Snackbar snackbar = Snackbar.make(v, "Property Favorited", Snackbar.LENGTH_SHORT);
snackbar.show();
}
Log.e("HF update checked", String.valueOf(selected));
Set<String> set = new HashSet<String>();
set.addAll(selected);
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet("FAV", set);
editor.commit();
}
}
});
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
return new RecyclerViewHolder(v1);
}
@Override
public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
SharedPreferences pref = getContext().getSharedPreferences("MirSP", Context.MODE_PRIVATE);
set = pref.getStringSet("FAV", null);
if (set != null) {
selected = new ArrayList<String>(set);
} else {
selected = new ArrayList<String>();
}
Log.e("HF update UI", String.valueOf(selected));
if (String.valueOf(selected).contains(String.valueOf(PROJECT_ID.get(i)))) {
viewHolder.mCheck.setBackgroundResource(R.drawable.ic_favorite_white_24dp);
} else {
viewHolder.mCheck.setBackgroundResource(R.drawable.ic_favorite_white1_24dp);
}
}
@Override
public int getItemCount() {
//Code for Total length of json
return Length;
}
adapter.notifyDataSetChanged();
) that is updating tab 3. – Kiushuadapter.notifyDataSetChanged();
in onTabUnselected – KiushuviewPager1.setOffscreenPageLimit(0);
. but still the same result – Siliqua@Override public void setMenuVisibility(final boolean visible) { super.setMenuVisibility(visible); if (visible) { Log.e("fragment Visible","true"); }else{ Log.e("fragment Visible","false"); } }
i just added this code to my tab2 fragment. and then i calledviewPager1.getAdapter().notifyDataSetChanged();
in onTabUnselected as you told. Now everything works correctly, but viewpager is very very slow on swipe.. – Siliqua