javaandroidpaneslidingpanelayout

Pane is always opening in SlidingPaneLayout implementation


Problem with my code below is that the Pane always gets opened by default, no idea why. Obviously, I dont want the Pane (child) to be opened by default. Only when user clicks on lv_schedule, the pane should open. So when the fragment loads, I have to actually slide out the pane first to see the lv_schedule.

This is the 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" />

This is the JAVA file:

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();
    }
}

}

Solution

  • SlidingPaneLayout can be used for the master/detail pattern, but that's not its intended use. As such, by default, it wants to show the detail. You will need to alter that behavior in Java code.

    This sample project implements a SlidingPaneLayout for the master/detail pattern, with some activity code to handle the "pane" appropriately:

    /***
      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();
      }
    }
    

    Specifically: