导航抽屉图标显示不正确

时间:2015-07-28 04:58:51

标签: java android navigation navigation-drawer

我遵循了如何将导航抽屉添加到我的应用程序的教程。我得到它主要工作但是在使用导航抽屉后,图标始终是指向左侧的箭头。我读到许多人忘了添加onPostCreate方法。然而,在添加该方法之后,问题仍在继续。

注意:导航抽屉的xml布局为空白

ProjectsList.java

package com.austinerck.projectteamwork;

import android.content.Context;
import android.os.PersistableBundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;


public class ProjectsList extends AppCompatActivity {

    ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_projects_list);

        //Toolbar
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        if(getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        //Navigation Drawer
        NavigationDrawerFragment navigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentNavigationDrawer);
        navigationDrawerFragment.setup((DrawerLayout) findViewById(R.id.drawerLayout), (Toolbar) findViewById(R.id.toolbar));
        drawerToggle = navigationDrawerFragment.getDrawerToggle();


        //SwipeRefreshLayout
        final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresher);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //TODO: Refresh code goes here
                swipeRefreshLayout.setRefreshing(false);
            }
        });
        swipeRefreshLayout.setColorSchemeColors(R.color.colorPrimary);

        //RecyclerView
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new ProjectsListAdapter());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_projects_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        int id = item.getItemId();
        switch (id){
            case R.id.action_refresh:
                break;
            case R.id.action_sort:
                break;
            case R.id.action_feedback:
                break;
            case R.id.action_settings:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

}

class ProjectsListAdapter extends RecyclerView.Adapter{

    private ArrayList<ProjectsListInfo> projectInfo = new ArrayList<>();

    ProjectsListAdapter(){
        //TODO: Load in information to projectInfo
        projectInfo.add(new ProjectsListInfo(NasaProjects.GEMINI));
        projectInfo.add(new ProjectsListInfo(NasaProjects.APOLLO));
        projectInfo.add(new ProjectsListInfo(NasaProjects.ISS));
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_project_card, viewGroup, false);

        CardView card = (CardView) view.findViewById(R.id.projectCard);
        TextView name1 = (TextView) view.findViewById(R.id.projectName);
        TextView creator = (TextView) view.findViewById(R.id.creator);
        TextView description = (TextView) view.findViewById(R.id.description);
        RelativeLayout background = (RelativeLayout) view.findViewById(R.id.background);
        Button buttonViewProject = (Button) view.findViewById(R.id.button_view_project);
        Button buttonDescription = (Button) view.findViewById(R.id.button_description);

        buttonViewProject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Shortcut.toast(view.getContext(), "View Project Clicked!");
            }
        });

        buttonDescription.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Shortcut.toast(view.getContext(), "Description Clicked");
            }
        });

        return new ProjectCardView(view, card, name1, creator, description, background, buttonViewProject, buttonDescription);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        ProjectCardView view;
        view = (ProjectCardView) viewHolder;
        view.name.setText(projectInfo.get(i).getName());
        view.creator.setText("Created by " + projectInfo.get(i).getCreator());
        view.description.setText(projectInfo.get(i).getDescription());
        view.background.setBackgroundResource(projectInfo.get(i).getBackground());

    }

    @Override
    public int getItemCount() {
        return projectInfo.size();
    }

    class ProjectCardView extends RecyclerView.ViewHolder /*implements View.OnClickListener*/{

        View itemView;
        CardView card;
        TextView name, creator, description;
        RelativeLayout background;
        Button buttonViewProject, buttonDescription;
        Context context;

        public ProjectCardView(View view, CardView card, TextView name, TextView creator, TextView description, RelativeLayout background, Button buttonViewProject, Button buttonDescription) {
            super(view);
            context = view.getContext();
            this.itemView = view;
            this.card = card;
            this.name = name;
            this.creator = creator;
            this.description = description;
            this.background = background;
            this.buttonViewProject = buttonViewProject;
            this.buttonDescription = buttonDescription;
        }
    }
}

class ProjectsListInfo{

    private String name, creator, description;
    private int background;

    //TODO: Use this to bundle information from the server
    /*ProjectsListInfo(String name, String creator, String description, int background){
        this.name = name;
        this.creator = creator;
        this.description = description;
        this.background = background;

    }*/

    ProjectsListInfo(NasaProjects exampleProject){
        name = exampleProject.getName();
        creator = exampleProject.getCreator();
        description = exampleProject.getDesc();
        background = exampleProject.getBackground();
    }

    public String getName() {
        return name;
    }

    public String getCreator() {
        return creator;
    }

    public String getDescription() {
        return description;
    }

    public int getBackground() {
        return background;
    }
}

NavigationDraweFragment.java

package com.austinerck.projectteamwork;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class NavigationDrawerFragment extends Fragment {

    private ActionBarDrawerToggle drawerToggle;

    public NavigationDrawerFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    }

    public void setup(DrawerLayout drawerLayout, Toolbar toolbar){

        drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.nav_drawer_open, R.string.nav_drawer_close){
            @Override
            public void onDrawerOpened(View drawerView){
                super.onDrawerOpened(drawerView);
                getActivity().invalidateOptionsMenu();
            }

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

        drawerLayout.setDrawerListener(drawerToggle);
        drawerLayout.post(new Runnable() {
            @Override
            public void run() {
                drawerToggle.syncState();
            }
        });

    }

    public ActionBarDrawerToggle getDrawerToggle() {
        return drawerToggle;
    }
}

activity_projects_list.xml

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context=".ProjectsList">

        <include
            android:id="@+id/toolbar"
            layout="@layout/fragment_toolbar"/>

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefresher"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/paddingSmall"
                android:paddingRight="@dimen/paddingSmall"/>

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

    </LinearLayout>

    <fragment
        android:id="@+id/fragmentNavigationDrawer"
        android:layout_width="@dimen/drawerWidth"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        android:name="com.austinerck.projectteamwork.NavigationDrawerFragment"/>

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

1 个答案:

答案 0 :(得分:0)

请更改

getSupportActionBar().setDisplayShowHomeEnabled(true);

getSupportActionBar().setDisplayShowHomeEnabled(false);

这应该可以解决问题。