0

私のアプリケーションでは、同じフラグメントの複数のインスタンスActivity_FragmentがLinearLayoutに表示されています。各フラグメントには、activity_textのテキストビューとedit_buttonがあります。フラグメント内でボタンを押すと、同じフラグメント内のテキストビューのテキストが変更されます。ただし、フラグメントの複数のインスタンスが追加されると、いずれかのフラグメントでedit_buttonが押されると、LinearLayoutに追加された最初のフラグメント内のテキストビューが変更されます(activity_text)。これは、すべてのフラグメントが同一で、子ビューのIDが同じであるためです。各断片は互いに独立して行動するべきである。フラグメント内で押されたedit_buttonは、そのフラグメントにのみ影響します。findViewById()を個々のフラグメントの範囲内に制限できますか?

findViewById()メソッドを個々のフラグメントのスコープに限定することで、あるフラグメントが別のフラグメントのイベントの影響を受けないようにすることができますか、または各ビューのIDを何とか変更する必要があります創造時の断片で?ここで

アクティビティフラグメント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"/> 

はActivityFragmentクラスと私のMainActivity.javaです。 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"); 
    } 
} 

答えて

1

あなたの問題はfindViewById()スキャンという範囲を制限することによって解決することができます。特定のフラグメント内を見たい場合は、myFragment.getView().findViewById()(アクティビティのfindViewById()を使用するのではなく)を呼び出すことをお勧めします。

どうすればいいですか?さて、物事を設定する方法は簡単ではありません。 属性を使用してクリックリスナーを設定しているので、アクティビティの内部から処理する必要があります。

のでを使用しないでください。

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.your_fragment, container, false); 

    Button button = (Button) root.findViewById(R.id.edit_button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      TextView activityText = (TextView) getView().findViewById(R.id.activity_text); 
      activityText.setText("success"); 
     } 
    }); 

    return root; 
} 
+0

ありがとう:

代わりに、あなたのフラグメントのonCreateView()メソッドの内部で、このような何かを書きます!私はAndroidから始まったばかりです。Androidデベロッパーガイドを使用していたので、変更した内容のほとんどは新しく、一部はわかりません。主に '@ Override'、' @ Nullable'、 'root'、' .OnClickListener'です。これについては後でAndroidトレーニングまたはAPIガイドで説明しますか?あなたはリンクを持っていますか、これについて私がどこでもっと読むことができるか知っていますか? –

+0

'@ Override'と' @ Nullable'は無視できます。それらは、あなたが壊れないコードを書くのに役立つ注釈ですが、よく書かれたプログラムの動作には影響しません。 'root'は' Fragment.onCreateView() 'で使用するのが好きな変数名です(断片チュートリアルで読むことができればうれしいです)。 'View.OnClickListener'は' android:onClick'属性と同じコンセプトですが、別のやり方で行われます。 Buttonのドキュメント(https://developer.android.com/reference/android/widget/Button.html)で少し読むことができます。結局、これはすべて第二の性質になります。 –

+0

よろしくお願い致します。 –

0

アクティビティからのメソッドの代わりにview.findViewById(R.id.activity_text)を使用することができます。