窗格始终在SlidingPaneLayout实现中打开

时间:2014-10-22 10:04:46

标签: java android pane slidingpanelayout

下面的代码问题是窗格默认情况下会打开,不明白为什么。显然,我不希望默认打开Pane(子)。仅当用户单击lv_schedule时,才会打开窗格。因此,当片段加载时,我必须首先滑出窗格以查看lv_schedule。

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout     xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scheduledblocks_sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
    android:id="@+id/ll_left_pane_scheduled"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv_schedule"
        android:layout_width="1000dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:layout_marginRight="0dp"
        />

</LinearLayout>

<!-- Framelayout to display Fragments -->

<FrameLayout
    android:id="@+id/frame_container_scheduledblocksslidingpanedetailview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="20dp" />

这是JAVA文件:

public class Fragment_allScheduledBlocks extends Fragment implements
    View.OnClickListener, OnItemClickListener {

private Handler handler;

private ActionBarHelper mActionBar;

private Thread thread = new Thread();

public Fragment_allScheduledBlocks() {
}

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    mLogger.printToLog(Constants.LOG_ENTRY,
            " onCreateView of Fragment_scheduled_blocks.java",
            Constants.TAG_ALL);

    v = inflater.inflate(R.layout.fragment_scheduled_blocks, container,
            false);

    mSlidingLayout = (SlidingPaneLayout) v
            .findViewById(R.id.scheduledblocks_sliding_pane_layout);
    mSlidingLayout.setPanelSlideListener(new SliderListenerSchBlocks());
    mSlidingLayout.openPane();


    lv_scheduled = (ListView) v.findViewById(R.id.lv_scheduled);
    lv_scheduled.setOnItemClickListener(this);

    mActionBar = createActionBarHelper();
    mActionBar.init();

    return v;
}


/**
 * This panel slide listener updates the action bar accordingly for each
 * panel state.
 */
private class SliderListenerSchBlocks extends
        SlidingPaneLayout.SimplePanelSlideListener {
    @Override
    public void onPanelOpened(View panel) {
        mActionBar.onPanelOpened();
        Log.v(Constants.TAG, "in onPanelOPened inside SliderListener");
        getActivity().setTitle("LISTVIEW BLOCKS");

        mPrefs.printAllMySharedPrefs(getActivity().getApplicationContext());
    }

    @Override
    public void onPanelClosed(View panel) {
        mActionBar.onPanelClosed();
        Log.v(Constants.TAG, "in onPanelClosed inside SliderListener");
        mPrefs.printAllMySharedPrefs(getActivity().getApplicationContext());
    }

    @Override
    public void onPanelSlide(View panel, float slideOffset) {
        // TODO Auto-generated method stub
        super.onPanelSlide(panel, slideOffset);
    }
}




@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

private class ActionBarHelper {
    public void init() {
    }

    public void onPanelClosed() {
    }

    public void onPanelOpened() {
    }

    public void onFirstLayout() {
    }

    public void setTitle(CharSequence title) {
    }
}

/*
 * Create a compatible helper that will manipulate the action bar if
 * available.
 */
private ActionBarHelper createActionBarHelper() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // return new ActionBarHelperICS();
        return new ActionBarHelper();
    } else {
        return new ActionBarHelper();
    }
}

}

2 个答案:

答案 0 :(得分:3)

如果您想通过导航图标打开窗格,则可以使用:

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

    layout = (SlidingPaneLayout) findViewById(R.id.sliding_pane_layout);

    getToolbar().setNavigationIcon(R.drawable.ic_menu_white_24dp);
    getToolbar().setNavigationOnClickListener(this); 
}

@Override
public void onClick(View view) {
    if (layout != null) {
        if (!layout.isOpen()) {
            layout.openPane();
        } else {
            layout.closePane();
        }
    }
}

干杯,快乐的编码。 ;)

答案 1 :(得分:1)

SlidingPaneLayout 可以用于主/详细模式,但这不是它的预期用途。因此,默认情况下,它希望显示详细信息。您需要在Java代码中更改该行为。

This sample project为主/详细模式实现SlidingPaneLayout,并使用一些活动代码来适当处理“窗格”:

/***
  Copyright (c) 2008-2013 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.eu4you4;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SlidingPaneLayout;

public class EU4You extends Activity implements
    CountriesFragment.Contract {
  private DetailsFragment details=null;
  private SlidingPaneLayout panes=null;

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

    details=
        (DetailsFragment)getFragmentManager().findFragmentById(R.id.details);
    panes=(SlidingPaneLayout)findViewById(R.id.panes);
    panes.openPane();
  }

  @Override
  public void onBackPressed() {
    if (panes.isOpen()) {
      super.onBackPressed();
    }
    else {
      panes.openPane();
    }
  }

  @Override
  public void onCountrySelected(Country c) {
    details.loadUrl(getString(c.url));
    panes.closePane();
  }
}

具体做法是:

  • 当我们启动

  • 时,它会打开窗格(即主人)
  • 当用户点击主页中的项目时,它会关闭窗格,以显示详细信息

  • 如果用户单击“返回”按钮,并且窗格关闭,则会打开窗格(返回到主窗格),而不是执行正常的“返回”按钮处理