I am using https://github.com/mikepenz/MaterialDrawer as a library for Drawer, and I am using https://github.com/florent37/MaterialViewPager for View Pager.
The problem that I am facing is that I face a bit of lag(kind of stutter lag) when I click on list item in my drawer and the closing of the drwawer, after which the fragment is replaced over. I face this in a fragment which is a ViewPager over is a fragment.
Activity(which has NavBar):
public class mvpAct extends AppCompatActivity {
private RelativeLayout mRelativeLayout;
public static List<Model_Slots> list;
private Drawer result;
private static GetDetails gd;
public static List<Model_Daywise> todayslist_m;
public static List<Model_Daywise> todayslist_t;
public static List<Model_Daywise> todayslist_w;
public static List<Model_Daywise> todayslist_th;
public static List<Model_Daywise> todayslist_fr;
public static List<detailattlist_subcode> detail_att_all = new ArrayList<>();
public static HashMap<String, List<DetailAtten>> hash = new HashMap<>();
public static List<AttendBrief> attendBriefs = null;
public static List<Marks_Model> marks_det;
public static List<PBL_Model> lpbl;
public void setMTWTFLists(final Context ctxt) {
String mark = new SharedPrefs(ctxt).getMsg("marksdone").trim();
String att = new SharedPrefs(ctxt).getMsg("ttdone").trim();
String tt = new SharedPrefs(ctxt).getMsg("attendone").trim();
Log.d("Done Value", mark + "\t" + att + "\t" + tt);
if (mark.equals("y") && att.equals("y") && tt.equals("y")) {
Log.d("Here", "naive");
try {
new Thread(new Runnable() {
public void run() {
todayslist_m = new MTWTHgetset(ctxt, "monday").getAllCredentials();
todayslist_t = new MTWTHgetset(ctxt, "tuesday").getAllCredentials();
todayslist_w = new MTWTHgetset(ctxt, "wednesday").getAllCredentials();
todayslist_th = new MTWTHgetset(ctxt, "thursday").getAllCredentials();
todayslist_fr = new MTWTHgetset(ctxt, "friday").getAllCredentials();
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onBackPressed() {
if (gd.isAdded())
getSupportFragmentManager()
.beginTransaction().remove(gd).commit();
else super.onBackPressed();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_frame_mvp);
gd = new GetDetails();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.mvp_frame_act, gd).commit();
}
try {
list = new Slots_GetSet(this).getAllCredentials();
String classnbrs[] = new String[list.size()];
int r = 0;
for (Model_Slots ms : list) {
classnbrs[r++] = ms.getNumber().trim();
}
for (int t = 0; t < classnbrs.length; t++) {
detailattlist_subcode dr = new detailattlist_subcode();
hash.put(classnbrs[t], new IndivAttGetSet(this, " table_of_" + classnbrs[t]).getAllCredentials());
}
} catch (Exception e) {
e.printStackTrace();
}
try {
attendBriefs = new Attend_GetSet(this).getAllCredentials();
} catch (Exception e) {
e.printStackTrace();
}
try {
marks_det = new CBL_Get_Set(this).getAllCredentials();
} catch (Exception e) {
e.printStackTrace();
}
try {
lpbl = new PBL_Get_Set(this).getAllCredentials();
} catch (Exception e) {
e.printStackTrace();
lpbl = new ArrayList<>();
}
try {
setMTWTFLists(getApplication());
} catch (Exception e) {
e.printStackTrace();
}
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withName("Slots").withIdentifier(1);
SecondaryDrawerItem item2 = new SecondaryDrawerItem().withName("ImageStudentLogin").withIdentifier(2);
result = new DrawerBuilder()
.withActivity(this)
.addDrawerItems(
item1,
new DividerDrawerItem(),
item2,
new SecondaryDrawerItem().withName("Setup"),
new SecondaryDrawerItem().withName("Daywise")
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
displayView(position);
if (result.isDrawerOpen())
result.closeDrawer();
return true;
}
})
.build();
}
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
String s = getSharedPreferences("sp", Context.MODE_PRIVATE).getString("own", "no");
if (s.equals("already")) {
fragment = new Fragment_slots();
} else if (s.equals("yes")) {
list = new Slots_GetSet(getApplicationContext()).getAllCredentials();
fragment = new Fragment_slots();
} else if (s.equals("no")) {
fragment = new GetDetails();
}
break;
case 2:
fragment = new Photo_Stu();
break;
case 3:
fragment = new GetDetails();
break;
case 4:
fragment = MaterialVPFrag.newInstance();
break;
default:
fragment = MaterialVPFrag.newInstance();
break;
}
if (fragment != null) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.mvp_frame_act, fragment).commit();
} else {
Log.e("DrawerActivity", "Error creating fragment");
}
}
}
Fragment(where I face lag(kind of stutter lag) in process of closing the drawer, After which this is replaced over other frame):
public class MaterialVPFrag extends Fragment {
private MaterialViewPager mViewPager;
public static MaterialVPFrag newInstance() {
return new MaterialVPFrag();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.mvp_layout, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
mViewPager = (MaterialViewPager) view.findViewById(R.id.materialViewPager);
mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new RecyclerViewFragment(mvpAct.todayslist_m);
case 1:
return new RecyclerViewFragment(mvpAct.todayslist_t);
case 2:
return new RecyclerViewFragment(mvpAct.todayslist_w);
case 3:
return new RecyclerViewFragment(mvpAct.todayslist_th);
case 4:
return new RecyclerViewFragment(mvpAct.todayslist_fr);
default:
return new RecyclerViewFragment(mvpAct.todayslist_m);
}
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Monday";
case 1:
return "Tuesday";
case 2:
return "Wednesday";
case 3:
return "Thursday";
case 4:
return "Friday";
}
return "";
}
});
mViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() {
@Override
public HeaderDesign getHeaderDesign(int page) {
switch (page) {
case 0:
return HeaderDesign.fromColorAndDrawable(getResources().getColor(R.color.colorPrimary), getResources().getDrawable(R.drawable.myback));
case 1:
return HeaderDesign.fromColorResAndUrl(
R.color.blue,
"http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2014/06/wallpaper_51.jpg");
case 2:
return HeaderDesign.fromColorResAndUrl(
R.color.cyan,
"http://www.droid-life.com/wp-content/uploads/2014/10/lollipop-wallpapers10.jpg");
case 3:
return HeaderDesign.fromColorResAndUrl(
R.color.red,
"http://www.tothemobile.com/wp-content/uploads/2014/07/original.jpg");
}
return null;
}
});
mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount()); //it works without it don't know why!
mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager());
mViewPager.getPagerTitleStrip().setBackgroundColor(Color.TRANSPARENT);
mViewPager.getPagerTitleStrip().setIndicatorColor(Color.RED);
mViewPager.getPagerTitleStrip().setTabBackground(Color.TRANSPARENT);
View logo = view.findViewById(R.id.logo_white);
if (logo != null)
logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.notifyHeaderChanged();
}
});
}
}
Inner Fragment of ViewPager:
public class RecyclerViewFragment extends Fragment {
private RecyclerView mRecyclerView;
public static RecyclerView.Adapter mAdapter;
List<Model_Daywise> list;
public RecyclerViewFragment() {
}
public RecyclerViewFragment(List<Model_Daywise> lis) {
list = lis;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new RecyclerViewMaterialAdapter(new CardAdapter_Daywise(list, getActivity()));
mRecyclerView.setAdapter(mAdapter);
MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}
}
Thanks!
The latest version of the MaterialDrawer comes with a built in delay of 50ms after firing the listener event before closing the drawer itself.
If you have a more complex behavior when switching fragments it makes sense to modify this delay and change it to 150ms or even 250ms.
This can be done via the builder method
withDelayOnDrawerClose(int delay)
If you want the drawer to close instantly and afterwards switching the fragments you should set the delay to 0ms and add a handler inside your drawer item click listener
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//your logic in here
}
}, yourDelay);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With