正在替换的片段无法移除

时间:2016-08-12 05:13:21

标签: android android-fragments

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

FragmentA:

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);
    }
}

FragmentB:

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);

    }
}

MainActivity:

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);

    }
}

3 个答案:

答案 0 :(得分:0)

使用此代码select a.buildingNo,b.buildingName,b.buildingCity ,a.max_room_price from (select buildingNo,max(roomPrice) as max_room_price from table1 GROUP BY buildingNo) as a LEFT JOIN (select buildingNo, buildingName,buildingCity from table2)as b on a.buildingNo = b. buildingNo 代替fragmentTransaction.replace()

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

答案 1 :(得分:0)

header('location : your_php_page.php');

要做出的改变:1。从您的activity_main中删除片段标记并具有单个帧布局 2.在fragmentTransaction.replace中添加您的activity_main的framelayout的相应ID。

PS:您可以为当前片段实现切换案例,如图所示,或者拥有自己的逻辑..

答案 2 :(得分:0)

问题在于您的活动布局。 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()中,添加第一个片段:

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();
}