Ich habe eine Activity
, die eine Fragment
besteht. Die hat zwei ViewGroups (LinearLayouts), die ich wie Buttons verwenden möchte, um das Layout dieser sehr Fragment
zu ändern.Fragment nicht auf Klick
Ich bin vor zwei Probleme darin:
1. Die beiden LinearLayouts geben kein visuelles Touch-Feedback (Ripple-Animation), auch wenn clickable="true"
und android:background="?attr/selectableItemBackground"
eingestellt sind.
2. Die Fragment
wird nicht durch eine neue Fragment
ersetzt, obwohl onClickListener
für die beiden LinearLayout
funktioniert (überprüft mit einem Snackbar
).
3. In der Anweisung fragmentManager.beginTransaction().add(R.id.fragment_linearlayout,sb_qa_defaultFragment).commit();
, las ich auf SO, dass das erste Argument von add nicht die ID der fragment
selbst sein sollte, sondern die container
der fragment
. Ist es wahr ? Weil ich die gleiche Verhaltenseinstellungs-ID des fragment
oder sein container
als das erste Argument bekam.
MainActivity
public class sb_question extends AppCompatActivity{
private LinearLayout typeAnswerLL, recordAnswerLL ;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sb_question_new);
final FragmentManager fragmentManager = getSupportFragmentManager();
SB_QA_defaultFragment sb_qa_defaultFragment = new SB_QA_defaultFragment();
final SB_TextAnswerFragment sb_textAnswerFragment = new SB_TextAnswerFragment();
fragmentManager.beginTransaction().add(R.id.fragment_linearlayout,sb_qa_defaultFragment).commit();
typeAnswerLL = (LinearLayout) findViewById(R.id.type_answer_linearlayout);
typeAnswerLL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// THIS STATEMENT BELOW DOESN'T WORK AND DOES NOTHING
fragmentManager.beginTransaction().replace(R.id.fragment_linearlayout, sb_textAnswerFragment).commit();
// Snackbar snackbar = Snackbar.make(view, "Type Answer", Snackbar.LENGTH_SHORT);
// snackbar.show();
}
});
recordAnswerLL = (LinearLayout) findViewById(R.id.record_answer_linearlayout);
recordAnswerLL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar = Snackbar.make(view, "Record Answer", Snackbar.LENGTH_SHORT);
snackbar.show();
}
});
}
Layout MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/sb_ques_toolbar"
android:elevation="4dp"
android:background="@android:color/background_dark"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:textSize="18sp"
android:id="@+id/sb_ques_toolbar_title"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<LinearLayout
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"
android:background="@android:color/black"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.7"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:padding="@dimen/activity_horizontal_margin"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:id="@+id/sb_ques_text"
android:minHeight="70dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/fragment_linearlayout"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<fragment
android:id="@+id/text_record_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.yankee.cw.SB_QA_defaultFragment">
</fragment>
</LinearLayout>
<LinearLayout
android:gravity="center|bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:padding="@dimen/activity_horizontal_margin"
android:textAllCaps="true"
android:text="Save & Exit"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:padding="@dimen/activity_horizontal_margin"
android:textAllCaps="true"
android:text="Skip Question"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Wenn es hilft, ist hier die XML-Layout des Fragment
I als Standard festgelegt, wenn die Activity
sb_qa_default_layout.xml gestartet wird und seine
Gleiches für die Fragment
, die den Standard Fragment
beim Klicken auf die LinearLayout
auf der Standardeinstellung Fragment
ersetzt. Layout und seine Java Code
Vielen Dank im Voraus.
Hier ist die Lösung für Ihr Problem http://stackoverflow.com/questions/27632443/replacing-a-fragment-does-not-replace-the-previous-fragment -entirely-warum-so –
@MikeM. Also, Sie wollen Fragmente dynamisch ändern, ich muss zur Laufzeit Fragmente hinzufügen und sie dann ersetzen? – Yankee
Ja, das ist richtig. –