我想用FragmentA替換FragmentB。片段A由listView和FragmentB組成,其中包含一些TextView。在單擊列表視圖位置0處,FragmentB應替換FragmentA。在替換之前,MainActivity中的所有兩個片段都顯示正常。替換後FragmentA出現在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);
}
}
可能是背景透明度導致此問題。 –
而不是添加'fragmentTransaction.add(R.id.fragment2,fragment);'嘗試替換像'fragmentTransaction.replace(R.id.fragment2,fragment);' –
什麼將是可能的解決方案? –