如何从Activity中检测单击Fragment中哪个动态创建的按钮?

时间:2017-06-04 15:42:52

标签: java android android-fragments

我有一个Fragment类,可以动态创建一个Buttons网格。然后在显示网格的主活动中创建该片段的实例。这是我的代码:

Grid.java

public class Grid extends Fragment {

    LinearLayout myLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myLayout = new LinearLayout(getActivity());
        LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        myLayout.setLayoutParams(lparams);
        myLayout.setOrientation(LinearLayout.VERTICAL);

        for (int i = 0; i < 4; i++) {
            LinearLayout row = new LinearLayout(getActivity());
            LayoutParams rowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(rowParams);

            for (int j = 0; j < 4; j++) {
                final Button btnTag = new Button(getActivity());
                LayoutParams btnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

                btnTag.setLayoutParams(btnParams);
                btnTag.setText(" " + (j + 1 + (i * 4)));
                btnTag.setId(j + 1 + (i * 4));
                row.addView(btnTag);
            }

            myLayout.addView(row);
        }
        return myLayout;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_mainactivity);
        Grid grid = new Grid();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.grid, grid);
        transaction.commit();
    }
}

另外这里是我的content_mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_mainactivity"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.owner.tables.MainActivity"
    tools:showIn="@layout/activity_mainactivity">

    <FrameLayout
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:id="@+id/grid"
        android:layout_centerHorizontal="true">
    </FrameLayout>

</RelativeLayout>

所以我的问题是,如何检测活动中的片段中是否以及哪个按钮被点击?或者,如果我可以在片段中执行此操作,如何将信息传递给活动?

3 个答案:

答案 0 :(得分:0)

使用界面将click事件传递给您的活动。在OnClickListener中调用它的方法。

答案 1 :(得分:0)

在MainActivity中

public void doSomethingAtClick() {...}

片段

((MainActivity)getActivity()).doSomethingAtClick()

答案 2 :(得分:0)

创建一个界面:

public interface MyGridInterface  {
    void myAction(<Params...>);
}

在片段中设置监听器:

MainActivity mActivity = new MainActivity();
setEleListener(mActivity);

public void setMyListener(MyGridInterface listener) {
        this.gListener = listener;
    }

在MainActivity中实现监听器

public class MainActivity extends AppCompatActivity implements MyGridInterface{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_mainactivity);
        Grid grid = new Grid();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.grid, grid);
        transaction.commit();
    }


  @Override
    public void myAction(<Params..>) {

        //Here do whatever you want
    }

}