android新手,如何为片段内的元素初始化事件处理程序

时间:2014-09-12 09:58:38

标签: java android android-fragments

(免责声明:非常新的android)所以我试图使用Android Studio创建一个标签式应用程序,并在线上的示例我已经能够使用操作栏来设置2个标签和片段,如下所示:

MainActivity.java

中的

    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setTitle("My App Test");

    Tab tabSearch = bar.newTab().setText("Search").setIcon(android.R.drawable.ic_menu_search);
    Tab tabReport = bar.newTab().setText("Report").setIcon(android.R.drawable.ic_menu_report_image);

    Fragment stfrag = new SearchTabFragment();
    Fragment rptfrag = new ReportFragment();

    tabSearch.setTabListener(new MyTabsListener(stfrag));
    tabReport.setTabListener(new MyTabsListener(rptfrag));


    bar.addTab(tabSearch);
    bar.addTab(tabReport);
ReportFragment / SearchTabFragment .java

中的

public class SearchTabFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){

         return inflater.inflate(R.layout.fragment_1,container,false);

    }
 }

我基本上只是直接从我发现的教程中复制了 ActionBar监听器的示例:

class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;

    public MyTabsListener(Fragment fragment){
        this.fragment = fragment;
    };

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft){
        ft.replace(R.id.wrap,fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft){

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft){

    }


}

现在我的问题是,如何将eventHandlers附加到我的fragments.xml中定义的元素?以前在"单一活动"我玩的应用程序我只是在onCreate事件之后将它放在主java文件中。例如:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    (TextView) tv = (TextView) findViewByid(R.id.textviewidhere);
    //now I attach the handlers as I need

提前致谢! (为清晰起见编辑)

3 个答案:

答案 0 :(得分:1)

你应该读一点fragments。你需要覆盖onViewCreated和" catch"在那里观看:

 public class MyFragment extends Fragment {
    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.my_fragment, container,
                false);
        return rootView;

    }

//and you use rootView to call findViewById
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button myButton = (Button) rootView.findViewById(R.id.mybutton);
        //or you can set some other listener, or "catch" some different view -checkbox,          //textview etc
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do something you want
            }
        });
    }

答案 1 :(得分:1)

示例:

public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){

     View root = inflater.inflate(R.layout.fragment_1,container,false);
     TextView text = (TextView)root.findViewById(R.id.mybutton);

    return root;
 }

}

这将使用片段内的TextView。

答案 2 :(得分:1)

修改过的代码直接从the docs解除:

情境:

我们假设您有一个Fragment,里面有一个按钮。您希望将活动作为事件侦听器附加到片段内的该按钮。然后,

public static class ButtonFragment extends Fragment implements OnClickListener {

    private OnClickListener mListener;

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

        // inflate the corresponding fragment XML
        View v = inflater.inflate(R.layout.example_fragment, container, false);

        // grab the button and attach this fragment as its listener
        ((Button)v.findViewById(R.id.btn)).setOnClickListener(this);

        return v;
    }

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

        try {

            // once this fragment is attached to its activity, check to see
            // if it implements OnClickListener
            mListener = (OnClickListener) activity;

        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " doesn't implement OnClickListener");
        }
    }

    ...
    public void onClick(View v) {
        if(mListener){
            // once the button inside fragment is clicked,
            // notify the activity about the same
            mListener.onClickListener(v);
        }
    }


}