2017-10-15 3 views
0

Ich habe ein Bild in der Haupttätigkeit und wenn dieses Bild angeklickt wird, ich möchte das FragmentA anzeigen, die eine Listenansicht hat.Ersetzen Fragment A mit FragmentB Problem

Wenn ein Element dieser ListView geklickt wird, möchte ich das FragmentA durch das FragmentB ersetzen, das eine Textansicht hat, und ich möchte in dieser Textansicht den Text anzeigen, der dem angeklickten Listenelement zugeordnet ist.

So in Haupttätigkeit Ich habe dies:

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

Hinweis: Der Toast in addText() erscheint mit dem richtigen Text an dieser Stelle, so dass der Text der Listenansicht wird erhalten in Aktivität mit Erfolg.

Frage: Wie ersetzt man fragmentA durch fragmentB und zeigt die Textansicht mit dem empfangenen Text in der Aktivität, anstatt die Listenansicht des fragmentA anzuzeigen?

Unten ist das vollständige Beispiel.

fragmenta Klasse:

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

Haupttätigkeit 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> 

Fragment ein 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> 

Fragment 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

meine Antwort Überprüfen Sie [hier] (https://stackoverflow.com/a/46707510/8244632), können Sie einen ViewPager mit zwei Fragmenten verwenden, um das Scrollen zu begrenzen berühre und verwende einfach die gelieferte Lösung auf einem ListView-Objektklick. Das Side-Scrolling wird Ihrer Benutzeroberfläche einen schönen Effekt geben, anstatt –

Antwort

0

Dies ist, wie Sie die gewünschte Aufgabe wird erreicht lösen. machen Sie entsprechend Ihrer Klasse entsprechende Änderungen. 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> 
1

Sie Fragment A in folgenden Funktion anstelle von Fragment B

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

es sollte platzieren sein

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

Wenn Sie ein Fragment durch ein anderes ersetzen möchten, verwenden Sie einfach denselben Container erstellen Sie nicht separat (R.id.containerFragmentB, R.id.containerFragmentA) Container.

+0

zu ersetzen. Danke. Aber jetzt, wenn ich in einen Listeneintrag klicke, erscheint "App hält weiter". Es scheint, dass wegen der sendDataToFragmentB Methode, die ich in der Frage aktualisieren werde. – JonD

+0

posten Sie zurück in einer min. –

+0

hinzugefügt eine andere Antwort überprüfen Sie es aus. funktioniert gut für mich –

1

Sie benötigen

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

zu

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

und activity_main zu ändern.xml sollte

<?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> 

EDIT

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

Listener

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

Dies wird Ihr Problem

+0

Danke, aber es funktioniert auch nicht, wenn ich in den Listeneintrag klicken es erscheint "App hält weiter". Ich denke es ist, weil die Methode AddFragmentB oder sendDataToFragmentB. Wissen Sie, wo die AddFragmentB() platziert werden soll? – JonD

+0

@JonD Ihre Daten werden nicht übergeben, weil Sie eine neue Instanz übergeben, während Sie Ihr Fragment committen Sie sollten das Objekt übergeben – UltimateDevil

+0

Ich ändere bereits neue FragmentB() nur Fragment aber das Problem wird fortgesetzt. Ich stelle fest, dass ich nicht die AddFragmentB -Funktion so Ich füge es bei der onCreate-Methode hinzu, aber ich führe die App aus, die mit dieser Methode AddFragmentB auf der onCreate-Hauptaktivitätsmethode ausgeführt wird. – JonD

Verwandte Themen