用fragmentB问题替换fragmentA

问题描述:

我在主要活动中有一个图像,当单击此图像时,我想显示具有列表视图的fragmentA。用fragmentB问题替换fragmentA

当此的ListView的项目被点击我想有一个TextView的fragmentB更换fragmentA,我想在这个展示的TextView相关的点击列表项的文本。

所以在主要活动我有这样的:

public class MainActivity extends AppCompatActivity implements Listener{ 
    private ImageView img; 
    private String text; 
    FragmentManager fragmentManager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     fragmentManager = getFragmentManager(); 
     img = (ImageView) findViewById(R.id.imageView); 
    } 
    public void AddFragmentA(View view) { 
     FragmentA fragmentA = new FragmentA(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(R.id.containerFragmentA, new FragmentA(), "fragA"); 
     transaction.commit(); 
    } 
    public void AddFragmentB() { 
     FragmentB fragment = new FragmentB(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(R.id.containerFragmentB, new FragmentA(), "fragB"); 
     transaction.commit(); 
    } 
    @Override 
    public void addText(String text) { 
     this.text = text; 
     Toast.makeText(this, "Text received in Activity:" +text, Toast.LENGTH_SHORT).show(); 
     sendDataToFragmentB(); 
    } 

public void sendDataToFragmentB(){ 

      FragmentB fragmentB = (FragmentB) fragmentManager.findFragmentByTag("fragB"); 
      fragmentB.addText(text); 
     } 
} 

注:在addText()的面包出现在这一点正确的文本,所以列表视图的文本被接收成功的活动。

问题:现在如何用fragmentB替换fragmentA,并在活动中显示接收文本的textview,而不是显示fragmentA的列表视图?

以下是所有完整的示例。

FragmentA类:

public class FragmentA extends Fragment{ 
    private ListView listItems; 
    private String[] items = { 
      "item1", 
      "item2", 
      "item3", 
      "item4" 
    }; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment, container, false); 
     return view; 
    } 
    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     listItems = (ListView) getView().findViewById(R.id.listviewInFragment); 

     ArrayAdapter<String> adapter = 
       new ArrayAdapter<String>(getActivity().getApplicationContext(), 
         android.R.layout.simple_list_item_1, android.R.id.text1, items); 

     listItems.setAdapter(adapter); 
     listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       int positionCode = i; 

       String clickedValue = (String) adapterView.getItemAtPosition(i); 
       Listener listener = (Listener) getActivity(); 
       listener.addText(clickedValue); 
      } 
     }); 
    } 

} 

FramentB:

public class FragmentB extends Fragment { 
    private TextView tv; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_b, container, false); 
     tv = (TextView) getView().findViewById(R.id.textView); 
     return view; 
    } 
    public void addText(String text) { 
     String result = text; 
     tv.setText(result); 
    } 
} 

主要活动的xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/image" 
     android:onClick="AddFragmentA" 
     tools:layout_editor_absoluteX="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" /> 
    <FrameLayout 
     android:id="@+id/containerFragmentA" 
     android:layout_width="0dp" 
     android:layout_height="300dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     tools:layout_editor_absoluteY="1dp"> 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/containerFragmentB" 
     android:layout_width="0dp" 
     android:layout_height="300dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     tools:layout_editor_absoluteY="1dp"> 
    </FrameLayout> 
</android.support.constraint.ConstraintLayout> 

片段的XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#0ff"> 

    <ListView 
     android:layout_width="368dp" 
     android:layout_height="495dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="16dp" 
     android:id="@+id/listviewInFragment"/> 

</android.support.constraint.ConstraintLayout> 

片段B的xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ff0"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="300dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     /> 
</android.support.constraint.ConstraintLayout> 
+0

检查我的答案[这里](https://*.com/a/46707510/8244632),您可以使用带有两个片段的ViewPager,通过限制滚动触摸并在ListView的项目点击中使用提供的解决方案。横向滚动会给你的用户界面带来不错的效果,而不是取代 –

这是你将如何实现所需的任务。 相应地在下面给你的班级打电话。 MainActivity.java

public class MainActivity extends AppCompatActivity implements FragmentA.ClickListener{ 
    private Button button; 
    public static String EXTRA_STRING = "extraString"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     button = (Button)findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       AddFragment(FragmentA.getInstance()); 
      } 
     }); 
    } 
    public void AddFragment(Fragment fragment) { 
     getSupportFragmentManager().beginTransaction().replace(R.id.containerFragmentA, fragment).commit(); 
    } 

    @Override 
    public void onClick(String data) { 
     AddFragment(FragmentB.getInstance(data)); 
    } 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:id="@+id/containerFragmentA" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_editor_absoluteY="1dp"/> 

    <Button 
     android:id="@+id/button" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:layout_margin="8dp" 
     android:layout_alignParentBottom="true" 
     android:text="Add Fragment B" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

FragmentA.java

public class FragmentA extends Fragment { 
private ListView listItems; 
private String[] items = {"item1", "item2", "item3", "item4"}; 
private ClickListener clickListener; 

public static FragmentA getInstance() { 
    return new FragmentA(); 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    clickListener = (ClickListener)context; 
} 

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

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    listItems = (ListView) getView().findViewById(R.id.listviewInFragment); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1, items); 
    listItems.setAdapter(adapter); 
    listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      String clickedValue = items[i]; 
      clickListener.onClick(clickedValue); 
     } 
    }); 
} 

public interface ClickListener{ 
    void onClick(String data); 
} 

}

fragment_a.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ListView 
     android:layout_width="368dp" 
     android:layout_height="495dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="16dp" 
     android:id="@+id/listviewInFragment"/> 

</android.support.constraint.ConstraintLayout> 

FragmentB.java

public class FragmentB extends Fragment { 
    private TextView tv; 
    private String data; 

    public static FragmentB getInstance(String data){ 
     Bundle bundle = new Bundle(); 
     bundle.putString(MainActivity.EXTRA_STRING,data); 
     FragmentB fragmentB = new FragmentB(); 
     fragmentB.setArguments(bundle); 
     return fragmentB; 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_b, container, false); 
     tv = view.findViewById(R.id.textView); 
     data = getArguments().getString(MainActivity.EXTRA_STRING); 
     addText(data); 
     return view; 
    } 
    public void addText(String text) { 
     String result = text; 
     tv.setText(result); 
    } 
} 

fragment_b.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:gravity="center" 
     android:textSize="24dp" 
android:textAppearance="@style/TextAppearance.AppCompat.Widget.PopupMenu.Header" 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

要放置片段A在下面函数而不是片段乙

public void AddFragmentB() { 
     FragmentB fragment = new FragmentB(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(R.id.containerFragmentB, new FragmentA(), "fragB"); 
     transaction.commit(); 
    } 

它应该是

public void AddFragmentB() { 
     FragmentB fragment = new FragmentB(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.replace(R.id.containerFragmentB, new FragmentB(), "fragB"); 
     transaction.commit(); 
    } 

如果您想用另一个替换片段只使用相同的容器 请勿创建单独的容器(R.id.containerFragmentB,R.id.containerFragmentA)容器。

+0

谢谢。但是现在当我点击一个列表项时,它会出现“应用程序停止”。这似乎是因为我会在问题中更新的sendDataToFragmentB方法。 – JonD

+0

在最后发表回复 –

+0

添加另一个答案检查出来。对我来说工作正常 –

你需要改变你的

public void AddFragmentA(View view) { 
    FragmentA fragmentA = new FragmentA(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.containerFragmentA, new FragmentA(), "fragA"); 
    transaction.commit(); 
} 
public void AddFragmentB() { 
    FragmentB fragment = new FragmentB(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.containerFragmentB, new FragmentA(), "fragB"); 
    transaction.commit(); 
} 

public void AddFragmentA(View view) { 
    FragmentA fragmentA = new FragmentA(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.container, fragmentA , "fragA"); 
    transaction.commit(); 
} 
public void AddFragmentB() { 
    FragmentB fragment = new FragmentB(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.container, fragment, "fragB"); 
    transaction.commit(); 
} 

和activity_main。XML应该

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/image" 
     android:onClick="AddFragmentList" 
     tools:layout_editor_absoluteX="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="8dp" /> 
    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="0dp" 
     android:layout_height="300dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     tools:layout_editor_absoluteY="1dp"> 
    </FrameLayout> 


</android.support.constraint.ConstraintLayout> 

编辑

listItems.setAdapter(adapter); 
    listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      int positionCode = i; 

      String clickedValue = (String) adapterView.getItemAtPosition(i); 
      Listener listener = (Listener) getActivity(); 
      listener.addText(clickedValue); 
     } 
    }); 

监听

public interface IScanner { 

void addText(String Text); 

} 

MainActivity

public class MainActivity extends AppCompatActivity implements Listener{ 
private ImageView img; 
private String text; 
FragmentManager fragmentManager; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    fragmentManager = getFragmentManager(); 
    img = (ImageView) findViewById(R.id.imageView); 
} 
public void AddFragmentA(View view) { 
    FragmentA fragmentA = new FragmentA(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.containerFragmentA, new FragmentA(), "fragA"); 
    transaction.commit(); 
} 

@Override 
public void addText(String text) { 
    this.text = text; 
    Toast.makeText(this, "Text received in Activity:" +text, Toast.LENGTH_SHORT).show(); 
    sendDataToFragmentB(text); 
} 

public void sendDataToFragmentB(String clickedValue){ 
    Bundle b = new Bundle(); 
    b.putString("clickedValue", clickedValue); 

    FragmentB fragment = new FragmentB(); 
    fragment.setArguments(b); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    transaction.add(R.id.containerFragmentB, new FragmentA(), "fragB"); 
    transaction.commit(); 
    } 
} 

FragmentB

public class FragmentB extends Fragment { 
private TextView tv; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_b, container, false); 
    tv = (TextView) getView().findViewById(R.id.textView); 
    return view; 
} 
Bundle bundle = this.getArguments(); 
if (bundle != null) { 
     String text = bundle.getString("clickedValue", ""); 
} 
} 

这将解决你的问题

+0

谢谢,但它也没有工作,当我点击列表中的项目时出现“应用程序停止”。我认为它是因为AddFragmentB或sendDataToFragmentB方法。你知道AddFragmentB()应该放在哪里吗? – JonD

+0

@JonD你的数据没有通过,因为你正在传递一个新的实例,同时提交你的片段,你应该通过该对象 – UltimateDevil

+0

我已经改变新的FragmentB()只是片段,但问题continue.I注意到我没有使用AddFragmentB函数我将它添加到onCreate方法中,但是我使用onCreate主要活动方法上的此方法AddFragmentB运行应用程序doesent。 – JonD