如何将后退按钮动画添加到汉堡按钮动画中

时间:2016-01-25 07:56:48

标签: java android animation

我正在使用Started POST "/line_items" for ::1 at 2016-01-25 08:53:41 +0100 Processing by LineItemsController#create as JS Parameters: {"utf8"=>"✓", "line_item"=>{"product_id"=>"5", "instruction"=>""}, "commit"=>"Legg til"} Cart Load (0.4ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT $2 [["id", 92], ["LIMIT", 1]] Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]] (0.3ms) SELECT COUNT(*) FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."product_id" = $2 [["cart_id", 92], ["product_id", 5]] LineItem Load (0.4ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."product_id" = $2 [["cart_id", 92], ["product_id", 5]] LineItemAttribute Load (0.4ms) SELECT "line_item_attributes".* FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 [["line_item_id", 132]] LineItem Load (0.4ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1 AND "line_items"."id" = $2 LIMIT $3 [["cart_id", 92], ["id", 132], ["LIMIT", 1]] (0.2ms) BEGIN Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]] SQL (11.6ms) UPDATE "line_items" SET "quantity" = $1, "updated_at" = $2 WHERE "line_items"."id" = $3 [["quantity", 2], ["updated_at", 2016-01-25 07:53:41 UTC], ["id", 132]] (0.8ms) COMMIT LineItem Exists (1.0ms) SELECT 1 AS one FROM "line_items" WHERE "line_items"."cart_id" = $1 LIMIT $2 [["cart_id", 92], ["LIMIT", 1]] LineItem Load (0.7ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = $1 [["cart_id", 92]] Product Load (0.5ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] LineItemAttribute Exists (0.4ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]] LineItemAttribute Exists (1.0ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]] Product Load (0.4ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]] LineItemAttribute Exists (0.4ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]] LineItemAttribute Exists (0.3ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]] LineItemAttribute Exists (0.4ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]] LineItemAttribute Exists (0.3ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]] LineItemAttribute Exists (0.3ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]] LineItemAttribute Exists (0.3ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]] Rendered line_items/_line_item.html.erb (22.9ms) LineItemAttribute Exists (0.5ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 131], ["LIMIT", 1]] LineItemAttribute Exists (0.4ms) SELECT 1 AS one FROM "line_item_attributes" WHERE "line_item_attributes"."line_item_id" = $1 LIMIT $2 [["line_item_id", 132], ["LIMIT", 1]] Rendered carts/_cart.html.erb (74.4ms) Rendered line_items/create.js.erb (78.1ms) 并支持设计appcompat v7 我想在汉堡按钮动画中添加后退按钮,当后退按下的应用程序将返回到启动页面。

现在我的应用程序中有汉堡包按钮。当在主窗口中按下(标有橙色矩形)时,材料导航打开,当应用程序导航到所选窗口时,选择Home(标记为红色矩形)再次有汉堡包(黄色矩形)按钮,我想更改它。 enter image description here

我的MainActivity.java:

23.1.1.

FragmentDrawer.java:

    public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

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

    private Toolbar mToolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FragmentDrawer drawerFragment;

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

        viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(new PagerAdapter(this));

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().popBackStack();
            }
        });
        drawerFragment = (FragmentDrawer)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
        drawerFragment.setDrawerListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
            case R.id.action_search:
                Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onDrawerItemSelected(View view, int position) {
        displayView(position);
    }

    private void displayView(int position) {
        Fragment fragment = null;
        String title = getString(R.string.app_name);
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                title = getString(R.string.title_home);
                break;
            case 1:
                fragment = new FriendsFragment();
                title = getString(R.string.title_friends);
                break;
            case 2:
                fragment = new MessagesFragment();
                title = getString(R.string.title_messages);
                break;
            default:
                break;
        }

        if (fragment != null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container, fragment)
                    .addToBackStack(fragment.getClass().getSimpleName())
                    .commit();
           mToolbar.setTitle(title);
        }
    }
}

NavDrawerItem.java:

    public class FragmentDrawer extends Fragment {

    private static String TAG = FragmentDrawer.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 FragmentDrawer() {

    }

    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_drawer, 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.setDrawerListener(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.getChildPosition(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);
    }
}

2 个答案:

答案 0 :(得分:0)

如果第三张图片中的活动是新的,您只需在新活动的onCreate()方法中添加以下代码即可。它可能有帮助...

final ActionBar toolbar = getSupportActionBar();
if (toolbar != null) {
    toolbar.setDisplayHomeAsUpEnabled(true);
}

如果它在同一个活动中,并且通过viewpager只是不同的片段,请使用Toolbar.setNavigationIcon()来更改你的图标。

答案 1 :(得分:0)

在xml中尝试这种方式

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="start"
        android:background="@android:color/transparent"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/toolBarStyle"
        app:titleTextAppearance="@style/Toolbar.TitleText" />

    <android.support.v4.widget.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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:fitsSystemWindows="true">

        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp"
            android:background="@android:color/transparent" />

        <fragment
            android:id="@+id/navigation_drawer"
            class="com.buzzintown.consumer.drawer.NavigationDrawerFragment"
            android:layout_width="310dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/drawer_layout" />
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>