gpt4 book ai didi

android - fragment 未被替换

转载 作者:行者123 更新时间:2023-11-30 01:50:18 24 4
gpt4 key购买 nike

我有一个 FragmentActivity,它应该将前屏幕 fragment (带有按钮)换成单击按钮时按钮指向的 fragment 。

假设调用 SourceListFragment 的按钮被点击。 SourceListFragmentonCreate()onCreateView() 都被调用了,但是屏幕以某种方式保持不变,即前屏幕 fragment 不是被替换。

我想知道我做错了什么。这是我的代码。

public class MainActivity extends FragmentActivity implements MainFragment.OnFragmentInteractionListener {

...

@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);

// set uncaught exception handler for thread
// Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

setContentView(R.layout.activity_main);

FragmentManager fragmentManager = getSupportFragmentManager();

mMainFrame = MainFragment.newInstance();
fragmentManager.beginTransaction().add(R.id.container, mMainFrame).commit();
}

@Override
public void onFragmentInteraction(int id) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (id) {
case R.id.button_sources:
Fragment fragment = new SourceListFragment();
FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
fragmentTrans.replace(R.id.main_screen, fragment);
fragmentTrans.addToBackStack(null);
fragmentTrans.commit();
break;
default:
break;
}
}
}



public class SourceListFragment extends Fragment implements AbsListView.OnClickListener {
...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {

Log.d("SOURCELIST", "SourceListFragment onCreateView\n");

View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);

return view;
}


public static SourceListFragment newInstance(BluetoothAdapter adapter) {
SourceListFragment fragment = new SourceListFragment();
mBTAdapter = adapter;
return fragment;
}

// Container Activity must implement this interface
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(int id);
}
}

布局如下:

activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical"
android:id="@+id/container">

</FrameLayout>

fragment _main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_screen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="148dp">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="128dp"
android:background="#01579b"
android:paddingBottom="20dp"
android:paddingLeft="104dp" android:id="@+id/banner">
</RelativeLayout>

</RelativeLayout>

<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button_sources"
app:layout_widthPercent="30%"
app:layout_heightPercent="30%"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="15%"
app:layout_marginRightPercent="5%"/>
...

</android.support.percent.PercentRelativeLayout>
</LinearLayout>

fragment_sourceitem_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent"
/>

<com.gc.materialdesign.views.ProgressBarCircularIndeterminate
android:id="@+id/progressBarCircularIndeterminate"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#1E88E5" />

</FrameLayout>

我们将不胜感激。

谢谢

最佳答案

您将一个 fragment 添加到 ID 为 main_screen 的线性布局中,但该 fragment 应该添加到 FrameLayout 中。过渡在 LinearLayout 的情况下有效,但新添加的 Fragment 在显示之外。

关于android - fragment 未被替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33161397/

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