-2

我有一个Activity它由Fragment组成。 Fragment有两个ViewGroups(LinearLayouts),我想用它来像按钮一样更改Fragment的布局。片段没有被点击取代

我面对这个两个问题:
1.两个LinearLayouts不放弃,即使clickable="true"android:background="?attr/selectableItemBackground"设置任何视觉触摸反馈(纹波动画)。
2.即使onClickListener用于两个LinearLayout工程(使用Snackbar检查),Fragment也不会被新的Fragment所取代。
3.在声明fragmentManager.beginTransaction().add(R.id.fragment_linearlayout,sb_qa_defaultFragment).commit();中,我读到SO上第一个参数add不应该是fragment本身的ID,而是fragmentcontainer。这是真的吗?因为我获得了相同的行为设置ID fragment或其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(); 
      } 
     }); 

    } 

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 &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的XML布局我设置为默认值时,Activity启动sb_qa_default_layout.xml及其Java code
与相同这将取代默认Fragment单击默认Fragment上的LinearLayoutLayout及其Java Code

在此先感谢。

+2

这里是你的问题http://stackoverflow.com/questions/27632443/replacing-a-fragment-does-not-replace-the-previous-fragment解决方案-entirely-why-so –

+0

@MikeM。所以,你的意思是动态改变片段,我将不得不在运行时添加片段,然后将其替换? – Yankee

+1

是的,这是正确的。 –

回答

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(); 
+1

这并不能提供问题的答案,因为它正是OP所做的,只有这里是多行代码 –