viewpager studio example español activity android android-fragments android-viewpager materialdrawer

example - tabbed activity android studio español



Retraso al reemplazar el fragmento ViewPager en la barra de navegación sobre FrameLayout? (1)

La última versión de MaterialDrawer viene con un retardo incorporado de 50 ms después de disparar el evento del oyente antes de cerrar el cajón.

Si tiene un comportamiento más complejo al cambiar fragmentos, tiene sentido modificar este retraso y cambiarlo a 150ms o incluso 250ms.

Esto puede hacerse a través del método de construcción

withDelayOnDrawerClose(int delay)

Si desea que el cajón se cierre instantáneamente y luego cambie los fragmentos, debe establecer el retraso en 0ms y agregar un controlador dentro del elemento del cajón, haga clic en oyente.

new Handler().postDelayed(new Runnable() { @Override public void run() { //your logic in here } }, yourDelay);

Estoy usando https://github.com/mikepenz/MaterialDrawer como una biblioteca para Drawer, y estoy usando https://github.com/florent37/MaterialViewPager para View Pager.

El problema al que me enfrento es que enfrento un poco de retraso (tipo de retraso de tartamudeo) cuando hago clic en el elemento de la lista en mi cajón y en el cierre del drwawer, después de lo cual el fragmento se reemplaza. Me enfrento a esto en un fragmento que es una ViewPager más es un fragmento.

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"); } } }

Fragmento (donde me encuentro retrasado (tipo de retraso de tartamudeo) en el proceso de cerrar el cajón, después de lo cual este se reemplaza por otro marco):

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(); } }); } }

Fragmento interno de 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); } }

¡Gracias!