2017-03-31 9 views
-1

ボタンをクリックすると、別のフラグメント内からフラグメントを開くときに問題が発生しています。すべてが意味をなさないように思えますし、私は自分のコード(レイアウトの変更、断片の置き換えなど)を試してみましたが、何も動いていません。フラグメントからフラグメントを開くボタンをクリックしたとき

は、ここに私のRoleFragment.java(ボタンを含む断片)

public class RolesFragment extends Fragment implements View.OnClickListener { 

GridView gridView; 
ArrayList<Players> playersList; 
MyAdapter adapter; 

@Override 
public View onCreateView(LayoutInflater inflater,ViewGroup container, 
         Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_viewroles, container, false); 

    gridView = (GridView) view.findViewById(R.id.gv_players); 
    Button nightround = (Button) view.findViewById(R.id.buttonNightRound); 

    nightround.setOnClickListener(this); 

    DatabaseHelper databaseHelper = new DatabaseHelper(getActivity()); 
    playersList = new ArrayList<Players>(); 

    playersList = databaseHelper.getPlayers(); 
    adapter = new MyAdapter(getActivity(), playersList); 
    gridView.setAdapter(adapter); 

    return view; 
} 


@Override 
public void onClick(View v) { 
    Fragment fragment = null; 
    switch (v.getId()) { 
     case R.id.buttonNightRound: 
      fragment = new NightRound(); 
      replaceFragment(fragment); 
      break; 

    } 

} 

public void replaceFragment(Fragment someFragment) { 
    FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
    transaction.replace(R.id.fragment_container, someFragment); 
    transaction.addToBackStack(null); 
    transaction.commit(); 
} 

} 

であり、これは私のfragment_viewroles.xmlファイルです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.tuss.mafia.GameActivity" > 

<Button 
    android:id="@+id/buttonNightRound" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Night Round" 
    android:onClick="FragmentNightRoundClick" 
    android:clickable="true" 
    android:layout_weight="2"/> 


<GridView 
    android:id="@+id/gv_players" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:numColumns="auto_fit" 
    android:stretchMode="columnWidth" 
    android:columnWidth="150dp" 
    android:horizontalSpacing="10dp" 
    android:verticalSpacing="10dp" 
    android:gravity="center" 
    android:layout_below="@id/buttonNightRound"> 
</GridView> 

</RelativeLayout> 

問題は、ボタンをクリックしても何も起こりません。

答えて

0

あり、ここでいくつかの問題。 最初に、FrameLayout のようなフラグメント内にid R.id.fragment_containerのコンテナを追加して、新しいフラグメントを保存する必要があります。

フラグメントを新しいスクリーンとして開く場合は、そのフラグメントを新しいアクティビティ内に配置する必要があります。フラグメントは画面の一部であり、アクティビティやポケットベルを表示しないで使用しないでください。

は、Android deverlopersページを見てください:http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

は基本的に、あなたは、フラグメントAのインターフェイスを定義し、あなたの活動は、そのインタフェースを実装してみましょう。これで、Fragmentのインターフェースメソッドを呼び出すことができ、アクティビティはイベントを受け取ります。今アクティビティで、2番目のFragmentを呼び出して、受け取った値でテキストビューを更新することができます。

// You Activity implements your interface 
public class YourActivity implements FragmentA.TextClicked{ 
    @Override 
    public void sendText(String text){ 
     // Get Fragment B 
     FraB frag = (FragB) 
      getSupportFragmentManager().findFragmentById(R.id.fragment_b); 
     frag.updateText(text); 
    } 
} 


// Fragment A defines an Interface, and calls the method when needed 
public class FragA extends Fragment{ 

    TextClicked mCallback; 

    public interface TextClicked{ 
     public void sendText(String text); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallback = (TextClicked) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
       + " must implement TextClicked"); 
     } 
    } 

    public void someMethod(){ 
     mCallback.sendText("YOUR TEXT"); 
    } 

    @Override 
    public void onDetach() { 
     mCallback = null; // => avoid leaking, thanks @Deepscorn 
     super.onDetach(); 
    } 
} 

// Fragment B has a public method to do something with the text 
public class FragB extends Fragment{ 

    public void updateText(String text){ 
     // Here you have it 
    } 
} 
0

は、次のようなものを試してみてください:

 Fragment fragment = OtherFragment.newInstance(); 
      android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); 
      transaction.replace(R.id.container_layout, fragment, "OtherFragment");// give your fragment container id in first parameter 
      transaction.addToBackStack(null); // if written, this transaction will be added to backstack 
      transaction.commit(); 
関連する問題