【发布时间】: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 的底部