1

我正在尝试为有关导航抽屉的片段和活动实现后按功能,但它不起作用。有谁知道我做错了什么/缺少什么以及需要做什么才能解决这个问题?

活动课

public class BakerlooHDNActivity extends AppCompatActivity {

    //save our header or result
    private Drawer result = null;

    // Declaring Views and Variables
    ViewPager pager;
    BakerlooHDNViewPagerAdapter adapter;
    BakerlooHDNSlidingTabLayout bakerloohdntabs;
    int Numboftabs = 2;

    private int getFactorColor(int color, float factor) {
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);
        hsv[2] *= factor;
        color = Color.HSVToColor(hsv);
        return color;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bakerloo_hdn);


        final String actionBarColor = "#B36305";

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

        if(getSupportActionBar()!=null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(false);
            getSupportActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>" + getResources().getString(R.string.hdn) + "</font>"));
            getSupportActionBar().setSubtitle(Html.fromHtml("<font color='#FFFFFF'>" + getResources().getString(R.string.zone_3) + "</font>"));

            final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
            upArrow.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
            getSupportActionBar().setHomeAsUpIndicator(upArrow);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(getFactorColor(Color.parseColor(actionBarColor), 0.8f));
        }

        // start of navigation drawer
        headerResult = new AccountHeaderBuilder()
                .withActivity(getActivity())
                .withCompactStyle(true)
                .withHeaderBackground(R.color.bakerloo)
                .withProfileImagesVisible(false)
                .withTextColor(Color.parseColor("#FFFFFF"))
                .withSelectionListEnabled(false)

                .addProfiles(
                        new ProfileDrawerItem().withName(getString(R.string.hdn)).withEmail(getString(R.string.hello_world))
                )
                .build();

        result = new DrawerBuilder()
                .withActivity(getActivity())
                .withAccountHeader(headerResult)
                .withTranslucentStatusBar(false)
                .withActionBarDrawerToggle(false)
                .withSelectedItem(-1)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName(R.string.hello_world).withIdentifier(1).withCheckable(false)
                )
                .build();
        // end of navigation drawer
    }


    @Override
    public void onBackPressed() {
        if (result.isDrawerOpen()) {
            result.closeDrawer();
        } else {
            super.onBackPressed();
        }
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(new Intent("BACKPRESSED_TAG"));
    }
}

片段类

public class FragmentBakerlooHDN extends android.support.v4.app.Fragment {

    public FragmentBakerlooHDN() {
        // Required empty constructor
    }

    BroadcastReceiver onNotice = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // do stuff when back in activity is pressed
             result.closeDrawer();
        }
    };

    // Declaring navigation drawer
    private AccountHeader headerResult = null;
    private Drawer result = null;

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(onNotice, new IntentFilter("BACKPRESSED_TAG"));

        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_bakerloo_hdn, container, false);

        // start of navigation drawer
        headerResult = new AccountHeaderBuilder()
                .withActivity(getActivity())
                .withCompactStyle(true)
                .withHeaderBackground(R.color.bakerloo)
                .withProfileImagesVisible(false)
                .withTextColor(Color.parseColor("#FFFFFF"))
                .withSelectionListEnabled(false)

                .addProfiles(
                        new ProfileDrawerItem().withName(getString(R.string.hdn)).withEmail(getString(R.string.hello_world))
                )
                .build();

        result = new DrawerBuilder()
                .withActivity(getActivity())
                .withAccountHeader(headerResult)
                .withTranslucentStatusBar(false)
                .withActionBarDrawerToggle(false)
                .withSelectedItem(-1)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName(R.string.hello_world).withIdentifier(1).withCheckable(false)
                )
                .build();
        // end of navigation drawer


        super.onCreate(savedInstanceState);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        View v = getView();

        super.onActivityCreated(savedInstanceState);
    }
}
4

3 回答 3

2

尝试这个:

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;

    @Override
    public void onCreate() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.navdrawer);
    }

    @Override
    public void onBackPressed() {
        if(mDrawerLayout.isDrawerOpen(mDrawerList)) mDrawerLayout.closeDrawer(mDrawerList);
        else super.onBackPressed();
    }

编辑:

您可以使用 LocalBroadcastManager 在按下活动返回时更新片段:

在片段中添加新的 BroadcastReceiver() 实例:

 BroadcastReceiver onNotice = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // do stuff when back in activity is pressed
        // headerResult.closeDrawer();
    }
};

并在 onCreate 方法中使用标记注册它:

LocalBroadcastManager.getInstance(this).registerReceiver(onNotice,
            new IntentFilter("BACKPRESSED_TAG"));

然后,在 Activity OnBackPressed 方法调用广播:

LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("BACKPRESSED_TAG"));
于 2015-08-04T21:33:46.160 回答
0

您的代码中有对接口的引用吗?看起来您正在直接调用该接口,因此会出现错误。尝试重命名该方法。它可能与超类的 onBackPressed 方法冲突。

于 2015-08-04T21:38:25.567 回答
-1

您的问题是您的界面被命名为您尝试使用的属性。

重命名它并使用一个实例。

@Override
public void onBackPressed() {
    OnBackPressedListener instance = getSettedListener();
    if (result.isDrawerOpen()) {
        result.closeDrawer();
    } else {
        return instance.onBackPressed();
    }
}

public interface OnBackPressedListener {
    boolean onBackPressed();
}

如果您还在代码上实现了 getSettedListener 方法(可能如下所示),则此代码将编译:

    public OnBackPressedListener getSettedListener() {
           return new OnBackPressedListener(){
                   boolean onBackPressed(){
                          if(shouldConsumeBack)
                               return consumeBack();
                          else return false;
                     };
         }
}

但是此代码可能会返回实现该方法的 Fragment。

于 2015-08-04T21:41:02.037 回答