I have implemented an Activity that hosts two fragments. I have one fragment on the left and one on the right. The fragment on the left is a list of items to click. When you click on it inflates a new fragment on the right.
One item on the left list is supposed to inflate a different list on the right. I have it working good except when you click that item it inflates the list to cover the entire screen. And I only want it to cover the space on the right side of the screen.
Is there a way I can keep the fragment from covering the entire screen? All the others stay on the right side of the screen, its just this one that doesn’t. But this is the only one trying to inflate a ListView on the right side.
Here are the relevant methods of the ListFragemnt that is supposed to be on the right inflated on the right side:
public void onActivityCreated(Bundle inState) {
super.onActivityCreated(inState);
//setContentView(R.layout.results_list_activity);
//mIntent = getIntent();
mActivity = getActivity();
mArrayListRecipe = generateList();
ListView listView = (ListView) mActivity.findViewById(android.R.id.list);
ArrayAdapter<Recipe> adapter = new ArrayAdapter<Recipe>(mActivity,
android.R.layout.simple_list_item_1, mArrayListRecipe);
listView.setAdapter(adapter);
//listView.setOnItemClickListener((OnItemClickListener) this);
}
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.results_list_activity, container, false);
return layout;
}
And here is the XML layout that is inflated called results_list_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
And here is the layout that contains the frame that I want this ListView placed in main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_layout_land"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_alignParentBottom="true">
<!-- Left side fragment -->
<fragment class="com.jasoncrosby.app.recipeorganizer.MainFragment" android:id="@+id/left_fragment"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
<!-- Right side fragment -->
<FrameLayout android:id="@+id/right_fragment" android:layout_width="0dp" android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
EDIT:
The activity that loads the fragment is simple and only consists of a call to:
setContentView(R.layout.main_activity);
Here is MainFragment.java:
public class MainFragment extends ListFragment {
//*******
// Fields
//*******
/** Indicated weather there is enough room on the screen to use 2 panes. */
private boolean mDualPane;
/** Indicates which item in the main list was clicked. */
private int mCurCheckPosition = 0;
/** The name of the save button. */
private String mStringSaveItemName;
/** The name of the search button. */
private String mStringSearchItemName;
/** The name of the browse button. */
private String mStringBrowseItemName;
/** The name of the random button. */
private String mStringRandomItemName;
/** The {@code Activity} associated with this {@code Fragment}. */
private Activity mActivity;
//*************
// Constructors
//*************
/**
* This constructor is only provided to avoid a compiler warning. It should not be used to crate a new
* {@code MainFragment} object.
*/
public MainFragment(){}
/**
* Used to create a new {@code MainFragment} object with the id.
*
* @param id
*/
//public MainFragment(int id) {
//mIntFragmentId = id;
//mContext = context;
//}
//*******************
// Overridden methods
//*******************
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
mActivity = getActivity();
mStringSaveItemName = mActivity.getString(R.string.save_button);
mStringSearchItemName = mActivity.getString(R.string.search_button);
mStringBrowseItemName = mActivity.getString(R.string.browse_button);
mStringRandomItemName = mActivity.getString(R.string.random_button);
// Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
new String[] {mStringSaveItemName, mStringSearchItemName, mStringBrowseItemName,
mStringRandomItemName}));
// Check to see if we have a frame in which to embed the details
// fragment directly in the containing UI.
View detailsFrame = mActivity.findViewById(R.id.right_fragment);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (mDualPane) {
// In dual-pane mode, list view highlights selected item.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Make sure our UI is in the correct state.
showDetails(mCurCheckPosition);
}
}
@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
showDetails(pos);
}
//****************
// Private methods
//****************
private void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
switch (mCurCheckPosition) {
case 0 : inflateFirstUi(); break;
case 1 : inflateSecondUi(); break;
case 2 : inflateThirdUi(); break;
case 3 : inflateFourthUi(); break;
default : inflateFirstUi(); break;
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
//intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
private void inflateFirstUi() {
SaveFragment details = new SaveFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.right_fragment, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
private void inflateSecondUi() {
SearchFragment details = new SearchFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.right_fragment, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
private void inflateThirdUi() {
ResultsListFragment details = new ResultsListFragment(Constants.BROWSE_REQUEST);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.right_fragment, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
private void inflateFourthUi() {
ResultsListFragment details = new ResultsListFragment(Constants.RANDOM_REQUEST);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.right_fragment, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
The problem is in the showDetails() method in the switch statement. Case 0 and case 1 work just fine and load in the pane I have on the right side of the screen. Case 3 is where I am running into this problem. It loads the UI but its covering the entire screen and not confined to the right side in the pane.
Ok I have the problem solved. In my
onActivityCreated()method I made a call to:I went back and compared the fragments that are working with this one that is not working. I found in the ones that work I called this instead:
I changed the non working fragment to call
setListAdatpter(adapter)and notlistView.setAdapter(adapter). And now the fragment is being shown in the right side of the screen like it should be. Thanks to everyone who helped and was looking into this.