从按钮单击的片段打开片段

时间:2017-03-31 17:54:13

标签: android android-fragments

我无法通过点击按钮从另一个片段中打开片段。一切似乎都有意义(对我而言),我尝试过使用我的代码(更改布局,替换片段等),但没有任何工作。

这是我的 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>

麻烦的是,当我点击按钮时没有任何反应。

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

这里有一些问题。 首先,您必须在片段中添加ID为R.id.fragment_container的容器,例如FrameLayout 这将存储你的新片段。

如果您想要将片段作为新屏幕打开,则必须将其放入新活动中。碎片是一块屏幕,不应在没有活动或查看寻呼机的情况下使用。

查看Android deverlopers页面:http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

基本上,您在Fragment A中定义了一个接口,并让您的Activity实现该接口。现在,您可以调用Fragment中的接口方法,并且您的Activity将接收该事件。现在,在您的活动中,您可以调用第二个片段以使用收到的值

更新文本视图
// 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
    }
}

答案 2 :(得分:0)

使用此代码:

    function onEdit(e){
  var range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getRange(10,2,12,4);
  var rangee = e.range;
  rangee.setBackgroundColor("#FF6633");
  Utilities.sleep(5000);    
  rangee.setBackgroundColor("#434343");   
}