gpt4 book ai didi

android - 两片一屏

转载 作者:行者123 更新时间:2023-12-05 00:07:32 25 4
gpt4 key购买 nike

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

左侧列表中的一个项目应该扩充右侧的另一个列表。我让它运行良好,除非您单击该项目时它会使列表膨胀以覆盖整个屏幕。我只希望它覆盖屏幕右侧的空间。

有什么方法可以防止 fragment 覆盖整个屏幕?所有其他人都留在屏幕的右侧,只有这个没有。但这是唯一一个试图在右侧膨胀 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 放置在 ma​​in_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>

编辑:

加载 fragment 的 Activity 很简单,只包含对以下内容的调用:

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 工作正常并加载到我在屏幕右侧的 Pane 中。案例 3 是我遇到这个问题的地方。它会加载 UI,但会覆盖整个屏幕,而不是局限于 Pane 的右侧。

最佳答案

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

listView.setAdapter(adapter);

我返回并比较了有效的 fragment 与无效的 fragment 。我发现在我称之为工作的那些中:

setListAdapter(adapter);

我将非工作 fragment 更改为调用 setListAdatpter(adapter) 而不是 listView.setAdapter(adapter)。现在该 fragment 正按应有的方式显示在屏幕右侧。感谢所有提供帮助并正在调查此事的人。

关于android - 两片一屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14207260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com