私はFragment
からなるActivity
を持っています。 Fragment
には、ViewGroups(LinearLayouts)という2つのビューグループがあり、ボタンのようにそのレイアウトを変更したいと思っています。Fragment
1. 2 LinearLayoutsがclickable="true"
とandroid:background="?attr/selectableItemBackground"
が設定されていても、任意の視覚的なタッチフィードバック(リップルアニメーション)を与えていない:私はこの2つの問題に直面していますクリック時にフラグメントが置換されない
。
2つのLinearLayout
作品(Snackbar
でチェックされています)についてonClickListener
であっても、Fragment
は新しいFragment
に置き換えられません。
3. fragmentManager.beginTransaction().add(R.id.fragment_linearlayout,sb_qa_defaultFragment).commit();
というステートメントでは、addの最初の引数はfragment
のIDではなく、fragment
のcontainer
である必要があります。本当ですか?私はfragment
またはそのcontainer
の同じ動作設定IDを最初の引数として取得しています。 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>
の
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();
}
});
}
レイアウトここは、助けActivity
はsb_qa_default_layout.xmlとを起動したとき、私はデフォルトとして設定Fragment
のXMLレイアウトである場合にはその0113905663531402と同じはFragment
をデフォルトのFragment
のLinearLayout
をクリックしたときに置き換えます。 LayoutおよびそのJava Code
ありがとうございます。コードの下
ここでは、あなたの問題http://stackoverflow.com/questions/27632443/replacing-a-fragment-does-not-replace-the-previous-fragmentためのソリューションです-entirely-why-so –
@MikeM。ですから、フラグメントを動的に変更することを意味します。実行時にフラグメントを追加して置き換える必要がありますか? – Yankee
はい、そうです。 –