當按下按鈕時,我有一個按鈕添加片段的實例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.java
與ActivityFragment
類。對於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");
}
}
*這是我第一次張貼問題,對不起,如果我錯誤地做任何事情。
我對android比較陌生,所以對不起,如果我誤解了這一點,但我有我的佈局設置的方式,每個片段添加到可滾動的linearlayout,低於以前添加片段。我希望用戶能夠添加多個片段,然後分別與每個片段進行交互。目前,我可以添加多個片段,並且它們彼此堆疊在一起沒有問題。當我使用.replace時,只能顯示片段,因爲之前添加的片段會被替換。 –
我明白了。因此,請閱讀Android文檔中的[如何創建列表和卡片](https://developer.android.com/training/material/lists-cards.html)文章。你現在正在學習如何做基本的事情,這很酷,但如果你在列表中思考,你必須使用RecyclerView來達到更安全的代碼。最後,回答你的問題,你將不得不使用TAG命名你添加到不同名稱的每個視圖(也許是一個計數器來幫助你?view1,view2 ...)並找到正確的視圖,你將使用findViewByTag觀看課堂,明白了嗎? –
我的不好,使用'findViewWithTag'代替 –