5

我使用https://github.com/mikepenz/MaterialDrawer作为 Drawer 的库,我使用https://github.com/florent37/MaterialViewPager作为 View Pager。

我面临的问题是,当我单击抽屉中的列表项并关闭 drwawer 时,我会遇到一些滞后(某种口吃滞后),然后替换片段。我在一个片段中面对这个,它是一个 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");
    }
}


}

片段(我在关闭抽屉的过程中面临滞后(一种口吃滞后),之后将其替换为其他框架):

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

            }
        });

}
}

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

谢谢!

4

1 回答 1

3

最新版本的MaterialDrawer在触发侦听器事件后,在关闭抽屉本身之前具有 50 毫秒的内置延迟。

如果您在切换片段时有更复杂的行为,则可以修改此延迟并将其更改为 150 毫秒甚至 250 毫秒。

这可以通过 builder 方法完成

withDelayOnDrawerClose(int delay)

如果您希望抽屉立即关闭,然后切换片段,您应该将延迟设置为 0ms 并在您的抽屉项目单击侦听器中添加一个处理程序

new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
      //your logic in here
   }
}, yourDelay);
于 2015-09-14T08:37:33.263 回答