2016-06-22 5 views
1

をロードフラグメントが、私は(負荷)に移動するボタンを持っているこの新しいフラグメントロードビュー

buttonToFragment1.setOnClickListener(
       new OnClickListener() { 
        @Override 
        public void onClick(View arg0) { 

         // return inflater.inflate(R.layout.fragment_one, container, false); 
         Fragment fr = new FragmentOne(); 
         FragmentManager fm = getFragmentManager(); 
         FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
         fragmentTransaction.replace(R.id.fragment_awal, fr); 
         fragmentTransaction.commit(); 


        } 
       } 
     ); 

現在の断片(R.id.fragment_awal)レイアウト(fragment_one.xml)を有する:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#00c4ff"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="Ini fragment 1" 
     android:textStyle="bold" /> 

</LinearLayout> 

およびクラスがある:

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

私の質問は、私は、このような操作を行うことができるように、このTextView1をロードする方法である:

基本的に新しいロードされたフラグメント内のビューにテキストを設定し
TextView textFragment = (TextView)findViewById(R.id.textView1); 
textFragment.setText(" new text"); 

EDIT:誰がこの質問に早く回答したのですか?基本的に彼はこの正解に答えました、私はちょっと混乱しています。彼はちょうど答えを削除しました。私は彼の答えを受け入れたい。

+0

@JuanCruzSoler、私はそれを受け入れるようにしたいあなたの答えを削除の取り消してください、あなたは正しいだため、私はアンドロイドの開発 –

+0

に新しいいる時にちょうどそう混乱しています大丈夫です。ありがとうございました –

答えて

-1

あなたはレイアウトを膨らませるために、その後、あなたがTextViewを参照できる必要があります。

@Override 
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { 
    //Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_one, container, false); 

    TextView textFragment = (TextView) view.findViewById(R.id.textView1); 
    textFragment.setText(" new text"); 

    return view; 
} 
+0

こんにちは、この 'TextView'をボタンイベントで取得/呼び出すにはどうすればいいですか?このビューを読み込んでテキストを設定するのですか? –

1

Fragmentsは、ビューの階層が含まれているコンテナを、本質的に表示されています。フラグメントがビュー階層に挿入されるとき、そのルートとしてアクティビティーを持たなければなりません。任意の時点で0個以上のフラグメントをアクティブにすることができます。

緩い言葉では、現在のビューを別のフラグメント(FragmentOne)のビューに置き換えます。

textView1TextViewにアクセスするには、findViewByIdメソッドをフラグメントの現在のビューで修飾する必要があります。

にあなたのフラグメントのコードを変更し

public class FragmentOne extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { 
     //Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_one, container, false); 

     // The findViewById method returns child views from either a Context, 
     // an Activity or another View itself. 
     TextView textFragment = (TextView) view.findViewById(R.id.textView1); 
     textFragment.setText(" new text"); 

     return view; 
    } 
} 
関連する問題