怎么有一个碎片没改变

YXTS122 2019-02-01 01:39:24




<LinearLayout 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"
tools:context="com.example.demofragmentex.MainActivity"
android:orientation="vertical"
android:id="@+id/LinearLayout_All" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/LinearLayout_One"
android:orientation="horizontal"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/FrameLayout_One"
android:layout_weight="1"
>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.demofragmentex.FragmentNothing" />

</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/FrameLayout_Two"
android:layout_weight="1"
>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.demofragmentex.FragmentNothing" />

</FrameLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/LinearLayout_Two"
android:orientation="horizontal"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/FrameLayout_Three"
android:layout_weight="1"
>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.demofragmentex.FragmentNothing" />

</FrameLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/FrameLayout_Four"
android:layout_weight="1"
>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.demofragmentex.FragmentNothing" />

</FrameLayout>

</LinearLayout>


</LinearLayout>
...全文
118 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2019-02-01
  • 打赏
  • 举报
回复
YXTS122 2019-02-01
  • 打赏
  • 举报
回复
引用 2 楼 m-oj 的回复:
你这是静态定义的fragment,你需要使用FragmentManager.findFragmentById方法获取fragment,直接new是不行的


public class MainActivity extends FragmentActivity {
Fragment current,curr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

f_onCreate();
}

private void f_onCreate(){
FragmentNumber fragmentNumber=new FragmentNumber();
FragmentNothing fragmentNothing=new FragmentNothing();
FragmentManager fragmentManager=getSupportFragmentManager();
curr=fragmentManager.findFragmentById(R.id.frag);
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
current=new Fragment();
if (current==fragmentNumber)
return;
if (!fragmentNumber.isAdded())
{
fragmentTransaction.hide(curr);
fragmentTransaction.add(R.id.FrameLayout_One,fragmentNumber);
fragmentTransaction.commit();
}
else{
fragmentTransaction.hide(curr);
fragmentTransaction.show(fragmentNumber);
fragmentTransaction.commit();
}
current=fragmentNumber;
}
}

改好了
m-oj 应用层 2019-02-01
  • 打赏
  • 举报
回复
你这是静态定义的fragment,你需要使用FragmentManager.findFragmentById方法获取fragment,直接new是不行的
YXTS122 2019-02-01
  • 打赏
  • 举报
回复
public class MainActivity extends FragmentActivity {
Fragment current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

f_onCreate();
}

private void f_onCreate(){
FragmentNumber fragmentNumber=new FragmentNumber();
FragmentNothing fragmentNothing=new FragmentNothing();
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
current=new Fragment();
if (current==fragmentNothing)
{
fragmentTransaction.hide(fragmentNothing);
fragmentTransaction.add(R.id.FrameLayout_One,fragmentNumber);
fragmentTransaction.commit();
}
current=fragmentNumber;
}
}

public class FragmentNothing extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_nothing, container, false);

return view;
}

public FragmentNothing(){
//怎样可以避免转屏后不闪退
}
}

public class FragmentNumber extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_number, container, false);
//mTextView=(TextView)view.findViewById(R.id.TextView_Number);
//int iNumber=SystemRuntime.m_iGroupIndex*4+1;
//mTextView.setText(iNumber);
return view;
}

public FragmentNumber(){
//怎样可以避免转屏后不闪退
}

TextView mTextView;
}

<?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="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Nothing"
/>
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextView_Number"
android:text="-11111111111111111"
/>
</LinearLayout>

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧