片段未被点击替换

时间:2016-11-21 08:47:55

标签: java android android-layout android-fragments android-activity

我有Activity,其中包含FragmentFragment有两个ViewGroups(LinearLayouts),我想用它们来改变非常Fragment的布局。

我面临两个问题:
 1.即使设置了clickable="true"android:background="?attr/selectableItemBackground",两个LinearLayouts也不会提供任何视觉触摸反馈(涟漪动画)。
 2.即使FragmentFragment工作,onClickListener也不会被新的LinearLayout取代(使用Snackbar进行检查)。
 3.在fragmentManager.beginTransaction().add(R.id.fragment_linearlayout,sb_qa_defaultFragment).commit();语句中,我读到,添加的第一个参数不应该是fragment本身的ID,而是container的{​​{1}}。这是真的吗?因为我将fragment或其fragment的行为设置ID设置为第一个参数。

MainActivity

container

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

    }  

如果有帮助,请在<?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 &amp; 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> 启动Fragment及其sb_qa_default_layout.xml时将Activity设置为默认值的XML布局 Fragment也是如此,它会在点击默认Fragment上的LinearLayout时替换默认FragmentJava code及其Layout

提前致谢。

1 个答案:

答案 0 :(得分:0)

使用以下代码:

 android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragments fragment = new ExampleFragments();
fragmentTransaction.replace(R.id.frag, fragment);
fragmentTransaction.commit();