3

我已经实现了一个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;
}

这里是扩展名为 results_list_activity.xml 的 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();
}
}

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

4

1 回答 1

0

好的,我已经解决了问题。在我的onActivityCreated()方法中,我调用了:

listView.setAdapter(adapter);

我回去比较了与这个不起作用的片段一起工作的片段。我发现在那些我称之为的工作中:

setListAdapter(adapter);

我将非工作片段更改为 callsetListAdatpter(adapter)而不是listView.setAdapter(adapter)。现在片段正像它应该的那样显示在屏幕的右侧。感谢所有帮助并正在调查此事的人。

于 2013-01-08T04:10:23.673 回答