打开和关闭时导航抽屉太慢

时间:2017-02-22 11:21:45

标签: android performance android-recyclerview navigation-drawer

抽屉的打开和关闭非常缓慢且滞后。显示器中不显示相关错误。如果有人熟悉这个问题?可能出现什么错误?

提前致谢

这就是我做的

Recycler视图适配器

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    String[] titles;
    TypedArray icons;
    Context context;

    // The default constructor to receive titles,icons and context from MainActivity.
    public RecyclerViewAdapter(String[] titles, TypedArray icons, Context context) {

        this.titles = titles;
        this.icons = icons;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (viewType == 1) {
            View itemLayout = layoutInflater.inflate(R.layout.drawer_item_layout, null);
            return new ViewHolder(itemLayout, viewType, context);
        } else if (viewType == 0) {
            View itemHeader = layoutInflater.inflate(R.layout.header_layout, null);
            return new ViewHolder(itemHeader, viewType, context);
        }


        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position != 0) {
            holder.navTitle.setText(titles[position - 1]);
            holder.navIcon.setImageResource(icons.getResourceId(position - 1, -1));
        }
    }

    @Override
    public int getItemCount() {
        return titles.length + 1;
    }




    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView navTitle;
        ImageView navIcon;
        Context context;

        public ViewHolder(View drawerItem, int itemType, Context context) {

            super(drawerItem);
            this.context = context;
            drawerItem.setOnClickListener(this);
            if (itemType == 1) {
                navTitle = (TextView) itemView.findViewById(R.id.tv_NavTitle);
                navIcon = (ImageView) itemView.findViewById(R.id.iv_NavIcon);
            }
        }

        /**
         * This defines onClick for every item with respect to its position.
         */

        @Override
        public void onClick(View v) {



            switch (getAdapterPosition()){
                case 1:
                    Intent home= new Intent(context,HomeActivity.class);
                    home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    context.startActivity(home);

                    break;
                case 2:
//                    Intent profile= new Intent(context,HomeActivity.class);
//                    profile.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                    context.startActivity(profile);

                    break;
                case 3:
//                    Intent accnt= new Intent(context,HomeActivity.class);
//                    accnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                    context.startActivity(accnt);
                    break;
                case 4:
                    Intent cart= new Intent(context,MyCartLoginActivity.class);
                    cart.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    context.startActivity(cart);
                    break;
                case 5:
//                    Intent phis= new Intent(context,HomeActivity.class);
//                    phis.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                    context.startActivity(phis);
//                    break;
                case 6:
                    Intent logout= new Intent(context,LoginActivity.class);
                    logout.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    context.startActivity(logout);
                    break;

            }

        }
    }


    public int getItemViewType(int position) {
        if (position == 0) return 0;
        else return 1;
    }


}

主要活动

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    DrawerLayout drawerLayout;
    RecyclerView recyclerView;
    String navTitles[];
    TypedArray navIcons;
    RecyclerView.Adapter recyclerViewAdapter;
    ActionBarDrawerToggle drawerToggle;
    FrameLayout frameLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);frameLayout = (FrameLayout)findViewById(R.id.containerView);
        setupToolbar();

        //Initialize Views
        recyclerView  = (RecyclerView) findViewById(R.id.recyclerView);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerMainActivity);

        // Setup Titles and Icons of Navigation Drawer
        navTitles = getResources().getStringArray(R.array.navDrawerItems);
        navIcons = getResources().obtainTypedArray(R.array.navDrawerIcons);

        recyclerViewAdapter = new RecyclerViewAdapter(navTitles,navIcons,this);
        recyclerView.setAdapter(recyclerViewAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //  Finally setup ActionBarDrawerToggle
        setupDrawerToggle();
    }

    void setupToolbar(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       // toolbar.setNavigationIcon(R.drawable.ic_action_menu);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    void setupDrawerToggle(){
        drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
        //This is necessary to change the icon of the Drawer Toggle upon state change.
        drawerToggle.setDrawerIndicatorEnabled(false);
        drawerToggle.setHomeAsUpIndicator(R.drawable.ic_action_menu);
        drawerToggle.syncState();
    }
}

标题布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="25dp"
        android:layout_marginTop="25dp">

        <RelativeLayout
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:id="@+id/rlh"
            android:layout_marginLeft="15dp"
            android:background="@drawable/circ"

            android:gravity="center">
            <ImageView
                android:id="@+id/icon"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerInParent="true"
                android:scaleType="fitXY"
                android:src="@drawable/logo5" />

        </RelativeLayout>


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/rlh"
            android:layout_toEndOf="@+id/rlh"

            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="15dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Nithin Kumar"
                android:textStyle="bold"
                android:textColor="#FFF"
                android:id="@+id/uname"
                android:textSize="17dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/uname"
                android:layout_marginTop="2dp"
                android:textStyle="bold"
                android:text="+919876543210"
                android:textColor="#FFF"
                android:textSize="13dp" />

        </RelativeLayout>

    </RelativeLayout>
</RelativeLayout>

抽屉物品布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_marginTop="140dp"
    android:layout_height="60dp">
    <RelativeLayout
        android:id="@+id/drawerItem"
        android:layout_gravity="center_horizontal"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="40dp">
        <ImageView
            android:layout_marginLeft="45dp"
            android:layout_width="30dp"
            android:layout_height="30dp"

            android:layout_gravity="center"
            android:tint="#251e1e"
            android:layout_centerVertical="true"
            android:id="@+id/iv_NavIcon"/>

        <de.morrox.fontinator.FontTextView
            android:layout_toRightOf="@+id/iv_NavIcon"
            app:font="Lato-Regular.ttf"
            android:layout_gravity="center"
            android:text="Claimed Offers"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:id="@+id/tv_NavTitle"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#251e1e"
            android:textSize="16sp"
            android:gravity="center"/>
    </RelativeLayout>
</RelativeLayout>

主要活动布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerMainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
        <FrameLayout
            android:layout_below="@+id/toolbar"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/containerView">



        </FrameLayout>

    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:layout_width="240dp"
        android:layout_height="fill_parent"
        android:id="@+id/recyclerView"
        android:scrollbars="vertical"
        android:background="#FFF"
        android:layout_gravity="left"
        />

</android.support.v4.widget.DrawerLayout>

0 个答案:

没有答案