【问题标题】:Fragment being replaced cannot removed被替换的片段无法移除
【发布时间】:2016-08-12 05:13:20
【问题描述】:

我想用 FragmentA 替换 FragmentB。 Fragment A 由 listView 组成, FragmentB 由一些 TextView 组成。在单击列表视图位置 0 时,FragmentB 应替换 FragmentA。在替换之前,两个片段在 MainActivity 中都显示正常。替换后 FragmentA 出现在 FragmentB 的底部。这意味着 FragmentB 的内容不会消失,但应该会消失。

片段A:

public class FragmentA extends ListFragment implements AdapterView.OnItemClickListener {

    Get get;
    public interface Get {
        void getData(int s);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            get = (Get) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayList<String> arrayList = new ArrayList<>();
        for(int i = 0; i<10; i++) {

            arrayList.add("a");
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        get.getData(position);
    }
}

片段B:

public class FragmentB extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_b, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
}

主活动:

public class MainActivity extends AppCompatActivity implements FragmentA.Get{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void getData(int s) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        if(s == 0) {




FragmentA fragment = new FragmentA();
        fragmentTransaction.replace(R.id.fragment2, fragment);
        fragmentTransaction.commit();
        Toast.makeText(this, String.valueOf(s), Toast.LENGTH_LONG).show();
        }
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.zohaibsiddique.listfragment.MainActivity">

    <fragment
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:name="com.example.zohaibsiddique.listfragment.FragmentA"
        android:id="@+id/fragment" />

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.zohaibsiddique.listfragment.FragmentB"
        android:id="@+id/fragment2"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/fragment"
        android:layout_toEndOf="@+id/fragment" />
</RelativeLayout>

fragment_a.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="vertical" >

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

</LinearLayout>

fragment_b.xml

package com.example.zohaibsiddique.listfragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

/**
 * Created by Zohaib Siddique on 12/08/2016.
 */
public class FragmentB extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_b, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
}

【问题讨论】:

  • 可能是背景透明度导致了这个问题。
  • 不要添加fragmentTransaction.add(R.id.fragment2, fragment);,而是尝试替换fragmentTransaction.replace(R.id.fragment2, fragment);
  • 可能的解决方案是什么?
  • fragmentTransaction.replace(R.id.fragment2, fragment);
  • 我更新了代码,但是 FragmentA 出现在 FragmentB 的底部

标签: android android-fragments


【解决方案1】:

问题在于您的活动布局。 FragmentB 位于 Fragment A 之上。对此的简单解决方案是在活动布局中创建一个 LinearLayout。喜欢:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.zohaibsiddique.listfragment.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@id/fragment_container"/>

</RelativeLayout>

然后在onCreate()的activity中,添加第一个片段:

FragmentA fragmentA = new FragmentA();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

然后创建一个添加FragmentB的方法,需要的时候调用这个方法。

public void addFragmentB() {
    FragmentB fragmentB = new FragmentB();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}

【讨论】:

  • 太棒了。我也找到了这个解决方案。
【解决方案2】:

使用此代码fragmentTransaction.replace() 而不是fragmentTransaction.add()

替换已添加到容器中的现有片段。这本质上与为所有当前添加的片段调用 remove(Fragment) 相同,这些片段使用相同的 containerViewId 添加,然后使用此处给出的相同参数调用 add(int, Fragment, String)。

【讨论】:

    【解决方案3】:
       If(null!=fragment)
       fragment=null;
       switch(currentFragment){
       case A:
        fragment = new FragmentA();
        break;
        case B:
       fragment= new FragmentB();
       break;}
        fragmentTransaction.replace(R.id.frame_layout, fragment);
        fragmentTransaction.commit();
        Toast.makeText(this, String.valueOf(s), Toast.LENGTH_LONG).show();
    

    要进行的更改:1。从您的 activity_main 中删除片段标签并具有单个框架布局 2.在fragmentTransaction.replace中添加你的activity_main的framelayout各自的id。

    PS:您可以如图所示为当前片段实现 switch case,也可以拥有自己的逻辑..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      相关资源
      最近更新 更多