Navigation Drawer animation is lagging while closi

2019-08-15 09:32发布

问题:

I going to implement navigation drawer for one of my project. I am able to implement the same. However there is some lag in navigation drawer. Please Suggest.here is the image

Below is my code

Home_activity.xml

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@drawable/image_login">



            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


            <RelativeLayout
                android:id="@+id/searchBox"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_below="@+id/toolbar"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:background="#ffffff">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:text="Search a Property"
                    android:textColor="#b8bcc0"
                    android:textSize="14sp" />

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/icon_search" />

            </RelativeLayout>


            <RelativeLayout
                android:id="@+id/box"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/searchBox"
                android:gravity="center">


                <ImageView
                    android:id="@+id/buyRentImage"
                    android:layout_width="110dp"
                    android:layout_height="110dp"
                    android:layout_marginBottom="2dp"
                    android:layout_marginRight="1dp"
                    android:src="@drawable/image_buy_rent" />


                <ImageView
                    android:id="@+id/applyLoanImage"
                    android:layout_width="110dp"
                    android:layout_height="110dp"
                    android:layout_marginRight="3dp"
                    android:layout_toRightOf="@+id/buyRentImage"
                    android:src="@drawable/image_apply_for_loan" />

                <ImageView
                    android:id="@+id/newProjectImage"
                    android:layout_width="110dp"
                    android:layout_height="110dp"
                    android:layout_marginRight="1dp"
                    android:layout_below="@+id/buyRentImage"
                    android:src="@drawable/image_new_projects" />

                <ImageView
                    android:id="@+id/postPropertyImage"
                    android:layout_width="110dp"
                    android:layout_height="110dp"
                    android:layout_marginRight="3dp"
                    android:layout_below="@+id/buyRentImage"
                    android:layout_toRightOf="@+id/newProjectImage"
                    android:src="@drawable/image_post_my_property" />


            </RelativeLayout>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="70dp">

            <RelativeLayout
                android:id="@+id/homeiconImage"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:layout_marginLeft="20dp"
                android:layout_centerVertical="true"
                android:background="@drawable/circle_blue"
                android:gravity="center">

                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:src="@drawable/icon_verified_property" />

            </RelativeLayout>

            <TextView
                android:id="@+id/totalProperty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@id/homeiconImage"
                android:text="300+"
                android:textColor="#008dd5"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/totalProperty"
                android:layout_marginLeft="25dp"
                android:layout_toRightOf="@id/homeiconImage"
                android:text="VARIFIED PROPERTIES"
                android:textColor="#3d3e40"
                android:textSize="16sp" />

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="4dp"
                android:src="@drawable/icon_next" />


        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:layout_alignParentBottom="true"
            android:background="#008dd5" />


    </LinearLayout>


    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.androidmobile.thanehomes.thanehomes.activities.home.NavigationFragment"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation" />

Home.java

public class Home extends AppCompatActivity implements NavigationFragment.FragmentDrawerListener {
    Toolbar toolbar;
    RelativeLayout box, searchBox;
    ImageView buyRentImg, applyLoanImg, newProjectImg, postProjectImg;
    TextView buyRentTxt, postPropertyTxt;
    Menu myMenu;
    int settleCount;
    private NavigationFragment drawerFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        settleCount = 1;
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        searchBox = (RelativeLayout) findViewById(R.id.searchBox);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(" Thanehomes");
        getSupportActionBar().setIcon(R.drawable.ic_logo);
        box = (RelativeLayout) findViewById(R.id.box);
        box.setRotation(45.0f);
        searchBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent searchIntent = new Intent(Home.this, SearchProperty.class);
                startActivity(searchIntent);
            }
        });
        drawerFragment = (NavigationFragment)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerLayout), toolbar);
        drawerFragment.setDrawerListener(this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        myMenu = menu;
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.home_menu, menu);
        MenuItem item = myMenu.findItem(R.id.notification);
        MenuItemCompat.setActionView(item, R.layout.update_count);
        View count = myMenu.findItem(R.id.notification).getActionView();
        TextView t = (TextView) count.findViewById(R.id.count);
        if (settleCount < 1) {
            t.setVisibility(View.GONE);
        } else {
            t.setVisibility(View.VISIBLE);
            t.setText("" + settleCount);
        }
        count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_SHORT).show();
                Intent notificationIntent = new Intent(Home.this, NotificationList.class);
                startActivity(notificationIntent);
            }
        });
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    @Override
    public void onDrawerItemSelected(View view, int position) {
        Toast.makeText(getApplicationContext(), position + "", Toast.LENGTH_SHORT).show();
    }
}

NavigationFragment.java

public class NavigationFragment extends Fragment {

    private static String TAG = NavigationFragment.class.getSimpleName();

    private RecyclerView recyclerView;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerLayout mDrawerLayout;
    private NavigationDrawerAdapter adapter;
    private View containerView;
    private static String[] titles = null;
    private FragmentDrawerListener drawerListener;

    public NavigationFragment() {

    }

    public void setDrawerListener(FragmentDrawerListener listener) {
        this.drawerListener = listener;
    }

    public static List<NavDrawerItem> getData() {
        List<NavDrawerItem> data = new ArrayList<>();


        // preparing navigation drawer items
        for (int i = 0; i < titles.length; i++) {
            NavDrawerItem navItem = new NavDrawerItem();
            navItem.setTitle(titles[i]);
            data.add(navItem);
        }
        return data;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // drawer labels
        titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflating view layout
        View layout = inflater.inflate(R.layout.fragment_navigation, container, false);
        recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);

        adapter = new NavigationDrawerAdapter(getActivity(), getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                drawerListener.onDrawerItemSelected(view, position);
                mDrawerLayout.closeDrawer(containerView);
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));

        return layout;
    }


    public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
        containerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                getActivity().invalidateOptionsMenu();
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                toolbar.setAlpha(1 - slideOffset / 2);
            }
        };

        mDrawerLayout.addDrawerListener(mDrawerToggle);
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

    }

    public static interface ClickListener {
        public void onClick(View view, int position);

        public void onLongClick(View view, int position);
    }

    static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
                    }
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }


    }

    public interface FragmentDrawerListener {
        public void onDrawerItemSelected(View view, int position);
    }
}

回答1:

If Navigation drawer is lagging while opening it means you are doing some resource intensive work in your navigation drawer.

If it lags while closing it means the fragment you are showing through navigation drawer is taking longer than navigation close time to draw itself.

Your problem is that you are doing some heavy task in the fragment you are opening through nav drawer.



回答2:

Not completely sure but I got issues with lag at navigation drawer for some reason when I define support-screens and it is running in screen compatibility mode.

However it seemed to be caused by update of the burger menu icon by the ActionBarDrawerToggle.

May you can check if the problem vanishes when you provide an empty implementaion (supress also super-call) of onDrawerSlide

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}

If it looks better you can try to perform that actions in a separate thread (if this UI features are necessary for you at all).