两个碎片一个屏幕

时间:2013-01-08 02:15:11

标签: android android-listfragment

我已经实现了一个托管两个片段的Activity。我左边有一个片段,右边有一个片段。左侧的片段是要单击的项目列表。单击它会在右侧膨胀一个新片段。

左侧列表中的一个项目应该在右侧膨胀不同的列表。我有它工作得很好,除非你点击该项目它膨胀列表覆盖整个屏幕。我只希望它覆盖屏幕右侧的空间。

有没有办法可以让片段覆盖整个屏幕?所有其他人都停留在屏幕的右侧,只是这个没有。但这是唯一试图在右侧膨胀ListView的人。

以下是ListFragemnt的相关方法,该方法应该在右侧充气的右边:

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

这是夸大的XML布局,名为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"/>

以下是包含我希望将此ListView置于 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>

编辑:

加载片段的活动很简单,只包含对:

的调用
setContentView(R.layout.main_activity);

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

问题出在switch语句的showDetails()方法中。案例0和案例1工作正常并加载到我在屏幕右侧的窗格中。案例3是我遇到这个问题的地方。它加载UI但覆盖整个屏幕,而不是局限于窗格的右侧。

1 个答案:

答案 0 :(得分:0)

好的,我已经解决了问题。在我的onActivityCreated()方法中,我打电话给:

listView.setAdapter(adapter);

我回去比较了那个无效的片段。我在那些工作中找到了我称之为:

setListAdapter(adapter);

我将非工作片段更改为setListAdatpter(adapter)而不是listView.setAdapter(adapter)。现在,片段显示在屏幕的右侧,就像它应该的那样。感谢所有帮助过并且正在研究这一点的人。