插入符号显示导航抽屉

时间:2014-12-01 21:04:58

标签: android android-fragments navigation-drawer back-stack fragment-backstack

没什么好说的。我使用单个活动,然后进入调用setDisplayHomeAsUpEnabled(Boolean.TRUE)setHasOptionsMenu(Boolean.TRUE)的片段。菜单有两个项目,与它们的互动是正确的。我在应用程序的所有onOptionsItemSelected上设置了断点(包括Activity,Fragment和抽屉切换)但是当我调试时,菜单打开并且没有触发断点。

活动和片段代码,以防他们提供帮助:

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

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

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/content_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<fragment
    android:id="@+id/navigation_drawer_fragment"
    android:name="org.jorge.lolin1.ui.fragment.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    layout="@layout/fragment_navigation_drawer" />

片段代码:

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ScrollView;
import android.widget.TextView;

import com.melnykov.fab.FloatingActionButton;
import com.squareup.picasso.Picasso;

import org.jorge.lolin1.LoLin1Application;
import org.jorge.lolin1.R;
import org.jorge.lolin1.datamodel.FeedArticle;
import org.jorge.lolin1.ui.activity.MainActivity;
import org.jorge.lolin1.ui.util.StickyParallaxNotifyingScrollView;
import org.jorge.lolin1.util.PicassoUtils;

public class ArticleReaderFragment extends Fragment {

private Context mContext;
private int mDefaultImageId;
private String TAG;
private FeedArticle mArticle;
public static final String ARTICLE_KEY = "ARTICLE";
private MainActivity mActivity;
private Drawable mActionBarBackgroundDrawable;
private final Drawable.Callback mDrawableCallback = new Drawable.Callback() {
    @Override
    public void invalidateDrawable(Drawable who) {
        final ActionBar actionBar = mActivity.getSupportActionBar();
        if (actionBar != null)
            actionBar.setBackgroundDrawable(who);
    }

    @Override
    public void scheduleDrawable(Drawable who, Runnable what, long when) {
    }

    @Override
    public void unscheduleDrawable(Drawable who, Runnable what) {
    }
};
private ActionBar mActionBar;
private float mOriginalElevation;
private FloatingActionButton mMarkAsReadFab;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = LoLin1Application.getInstance().getContext();
    Bundle args = getArguments();
    if (args == null)
        throw new IllegalStateException("ArticleReader created without arguments");
    mArticle = args.getParcelable(ARTICLE_KEY);
    TAG = mArticle.getUrl();
    mActivity = (MainActivity) activity;
    mDefaultImageId = getArguments().getInt(FeedListFragment.ERROR_RES_ID_KEY);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.actionbar_article_reader, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.homeAsUp:
            mActivity.onBackPressed();
            return Boolean.TRUE;
        case R.id.action_browse_to:
            mArticle.requestBrowseToAction(mContext);
            return Boolean.TRUE;
        case R.id.action_share:
            mArticle.requestShareAction(mContext);
            return Boolean.TRUE;
        default:
            return super.onOptionsItemSelected(item);
    }
}

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

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        mActionBarBackgroundDrawable.setCallback(mDrawableCallback);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(Boolean.TRUE);
    final View ret = inflater.inflate(R.layout.fragment_article_reader, container, Boolean.FALSE);
    View mHeaderView = ret.findViewById(R.id.image);
    PicassoUtils.loadInto(mContext, mArticle.getImageUrl(), mDefaultImageId, (android.widget.ImageView) mHeaderView, TAG);
    final String title = mArticle.getTitle();
    mHeaderView.setContentDescription(title);
    ((TextView) ret.findViewById(R.id.title)).setText(title);
    ((TextView) ret.findViewById(android.R.id.text1)).setText(mArticle.getPreviewText());

    mActionBar = mActivity.getSupportActionBar();
    mActionBar.setDisplayHomeAsUpEnabled(Boolean.TRUE);
    mActionBarBackgroundDrawable = new ColorDrawable(mContext.getResources().getColor(R.color.toolbar_background));
    mActionBar.setBackgroundDrawable(mActionBarBackgroundDrawable);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mOriginalElevation = mActionBar.getElevation();
        mActionBar.setElevation(0); //So that the shadow of the ActionBar doesn't show over the article title
    }
    mActionBar.setTitle(mActivity.getString(R.string.section_title_article_reader));

    StickyParallaxNotifyingScrollView scrollView = (StickyParallaxNotifyingScrollView) ret.findViewById(R.id.scroll_view);
    scrollView.setOnScrollChangedListener(mOnScrollChangedListener);
    scrollView.smoothScrollTo(0, 0);

    if (!mArticle.isRead()) {
        mMarkAsReadFab = (FloatingActionButton) ret.findViewById(R.id.fab_button_mark_as_read);
        mMarkAsReadFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mArticle.markAsRead();
                mMarkAsReadFab.hide();
            }
        });

        mMarkAsReadFab.show();
    }
    return ret;
}

private StickyParallaxNotifyingScrollView.OnScrollChangedListener mOnScrollChangedListener = new StickyParallaxNotifyingScrollView.OnScrollChangedListener() {
    public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
        if (mMarkAsReadFab != null)
            if (!who.canScrollVertically(1)) {
                mMarkAsReadFab.show();
            } else if (t < oldt) {
                mMarkAsReadFab.show();
            } else if (t > oldt) {
                mMarkAsReadFab.hide();
            }
    }
};

@Override
public void onDestroy() {
    super.onDestroy();
    Picasso.with(mContext).cancelTag(TAG);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mActionBar.setElevation(mOriginalElevation);
    }
}

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    if (enter) {
        return AnimationUtils.loadAnimation(mContext, R.anim.move_in_from_bottom);
    } else {
        return AnimationUtils.loadAnimation(mContext, R.anim.move_out_to_bottom);
    }
}
}

1 个答案:

答案 0 :(得分:0)

我选择仅针对不包含导航抽屉的片段进行活动。但显然这并没有回答这个问题,它只是一个相对类似的行为。