2017-07-12 73 views
1

當按下按鈕時,我有一個按鈕添加片段的實例ActivityFragment。片段中有一個edit_button,當按下該片段時,會更改片段中的activity_text文本視圖。但是,當添加ActivityFragment的多個實例時,任何片段中的任何edit_button只會影響添加的第一個ActivityFragment。我相信這是因爲每個片段共享相同的ID,但是是否有任何方法可以解決此問題,以便每個edit_button隻影響它所在的​​片段,最好在創建時不更改片段ID?影響同一片段的其他實例的片段按鈕

這裏是Activity Fragment 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="wrap_content" 
    android:id="@+id/activity_fragment"> 

    <CheckBox 
     android:id="@+id/checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="32dp" 
     android:layout_marginTop="16dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <TextView 
     android:id="@+id/activity_text" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="5dp" 
     android:layout_marginStart="16dp" 
     android:layout_marginTop="16dp" 
     android:text="Edit Activity" 
     android:textSize="18sp" 
     app:layout_constraintBaseline_toBaselineOf="@+id/checkbox" 
     app:layout_constraintLeft_toRightOf="@+id/checkbox" 
     app:layout_constraintRight_toLeftOf="@+id/edit_button"/> 

    <Button 
     android:id="@+id/edit_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:layout_marginEnd="16dp" 
     android:layout_marginTop="16dp" 
     android:onClick="editActivity" 
     android:text="Edit Activity" 
     app:layout_constraintBaseline_toBaselineOf="@+id/checkbox" 
     app:layout_constraintRight_toRightOf="parent"/> 

</android.support.constraint.ConstraintLayout> 

這是我MainActivity.javaActivityFragment類。對於edit_button的onclick方法是在最底層:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.AppCompatActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public static class ActivityFragment extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup 
     container, Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      return inflater.inflate(R.layout.activity_fragment, container, 
      false); 
     } 
    } 

    public void addActivity(View view) { 
     ActivityFragment fragment1 = new ActivityFragment(); 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment_container, fragment1).commit(); 
    } 

    //This is the editButton method 
    public void editActivity(View view) { 
     TextView activityText = (TextView) findViewById(R.id.activity_text); 
     activityText.setText("success"); 
    } 

} 

*這是我第一次張貼問題,對不起,如果我錯誤地做任何事情。

回答

0

發生這種情況是因爲您正在爲視圖添加大量碎片。你沒有注意到,因爲它們都是相等的。我認爲你必須改變這一點:

public void addActivity(View view) { 
    ActivityFragment fragment1 = new ActivityFragment(); 
    getSupportFragmentManager().beginTransaction() 
      .add(R.id.fragment_container, fragment1).commit(); 
} 

這樣:

public void addActivity(View view) { 
    ActivityFragment fragment1 = new ActivityFragment(); 
    getSupportFragmentManager().beginTransaction() 
      .replace(R.id.fragment_container, fragment1).commit(); 
} 

你要記住的是,當你將所有的意見都附加和findViewById將找到的第一個。您可以從片段中刪除靜態

+0

我對android比較陌生,所以對不起,如果我誤解了這一點,但我有我的佈局設置的方式,每個片段添加到可滾動的linearlayout,低於以前添加片段。我希望用戶能夠添加多個片段,然後分別與每個片段進行交互。目前,我可以添加多個片段,並且它們彼此堆疊在一起沒有問題。當我使用.replace時,只能顯示片段,因爲之前添加的片段會被替換。 –

+0

我明白了。因此,請閱讀Android文檔中的[如何創建列表和卡片](https://developer.android.com/training/material/lists-cards.html)文章。你現在正在學習如何做基本的事情,這很酷,但如果你在列表中思考,你必須使用RecyclerView來達到更安全的代碼。最後,回答你的問題,你將不得不使用TAG命名你添加到不同名稱的每個視圖(也許是一個計數器來幫助你?view1,view2 ...)並找到正確的視圖,你將使用findViewByTag觀看課堂,明白了嗎? –

+0

我的不好,使用'findViewWithTag'代替 –

0

通過使用這樣的:

getSupportFragmentManager().beginTransaction() 
     .add(R.id.fragment_container, fragment1).commit(); 

您最多可疊加片段彼此之上。更改爲

getSupportFragmentManager().beginTransaction() 
     .replace(R.id.fragment_container, fragment1).commit(); 
+0

我的佈局設置的方式是,每個片段被添加到可滾動的線性佈局中,在先前添加的片段下面。我希望用戶能夠添加多個片段,然後分別與每個片段進行交互。目前,我可以添加多個片段,並且它們彼此堆疊在一起沒有問題。當我使用.replace時,只能顯示片段,因爲之前添加的片段會被替換。問題是在片段的新實例中按下的按鈕僅影響線性佈局頂部的片段,而不影響其自身。 –

相關問題