Android programming experience-Introduction to the interaction between FragmentActivity and Fragment

Source: Internet
Author: User

 

public interface IBtnCallListener {       public void transfermsg();}

Public class extends Fragment implements IBtnCallListener {IBtnCallListener mbtnListener; private Button btn_Click; @ Overridepublic View onCreateView (extends inflater, ViewGroup container, Bundle attributes) {View rootView = inflater. inflate (R. layout. fragment_selection_launch, container, false); btn_Click = (Button) rootView. findViewById (R. id. bt_click); btn_Click.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubmbtnListener. transfermsg () ;}}); return rootView ;}@ Overridepublic void onActivityCreated (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onActivityCreated (savedInstanceState);} @ Overridepublic void onAttach (Activity activity) {// TODO Auto-generated method stubtry {mbtnListener = (IBtnCallListener) activity;} catch (Exception e) {// TODO: handle exceptionthrow new ClassCastException (activity. toString () + "must implement mbtnListener");} super. onAttach (activity) ;}@ Overridepublic void transfermsg () {// TODO Auto-generated method stubSystem. out. println ("information transmitted by Activity ");}}

Public class MainActivity extends FragmentActivity implements IBtnCallListener {public static final String ARGUMENTS_NAME = "arg"; private RelativeLayout rl_nav; private SyncHorizontalScrollView mHsv; private RadioGroup rg_nav_content; private ImageView listener; private ImageView iv_nav_right; private ViewPager mViewPager; private int indicatorWidth; public static Stri Ng [] tabTitle = {"option 1", "option 2", "option 3", "option 4", "option 5"}; // Title private LayoutInflater mInflater; private TabFragmentPagerAdapter mAdapter; private int currentIndicatorLeft = 0; private Button Btn_Test; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (); initView (); setListener ();}/* private void setListener (){ MViewPager. setOnPageChangeListener (new OnPageChangeListener () {@ Overridepublic void onPageSelected (int position) {if (rg_nav_content! = Null & rg_nav_content.getChildCount ()> position) {(RadioButton) rg_nav_content.getChildAt (position )). extends mclick () ;}@ Overridepublic void onPageScrolled (int arg0, float arg1, int arg2) {}@ Overridepublic void onPageScrollStateChanged (int arg0 ){}}); rg_nav_content.setOnCheckedChangeListener (new OnCheckedChangeListener () {@ Overridepublic void onCheckedChanged (RadioGroup group, int checkedId) {if (rg_n Av_content.getChildAt (checkedId )! = Null) {TranslateAnimation animation = new TranslateAnimation (currentIndicatorLeft, (RadioButton) rg_nav_content.getChildAt (checkedId )). getLeft (), 0f, 0f); animation. setInterpolator (new LinearInterpolator (); animation. setDuration (100); animation. setFillAfter (true); // execute the displacement animation iv_nav_indicator.startAnimation (animation); mViewPager. setCurrentItem (checkedId); // switch with ViewPager // record the current distance from the Left to currentIndica TorLeft = (RadioButton) rg_nav_content.getChildAt (checkedId). getLeft (); mHsv. smoothScrollTo (checkedId> 1? (RadioButton) rg_nav_content.getChildAt (checkedId )). getLeft (): 0)-(RadioButton) rg_nav_content.getChildAt (2 )). getLeft (), 0) ;}});} private void initView () {DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); indicatorWidth = dm. widthPixels/4; LayoutParams cursor_Params = extract (); cursor_Params.width = indicatorWidth; // initialize the width of the sliding bottom object iv_nav_indicator.setLayoutParams (cursor_Params); mHsv. setSomeParam (rl_nav, iv_nav_left, iv_nav_right, this); // get the layout filler mInflater = (LayoutInflater) this. getSystemService (LAYOUT_INFLATER_SERVICE); // you can get it in another way. // LayoutInflater mInflater = LayoutInflater. from (this); initNavigationHSV (); mAdapter = new TabFragmentPagerAdapter (getSupportFragmentManager (); mViewPager. setAdapter (mAdapter); Btn_Test = (Button) findViewById (R. id. btn_test); Btn_Test.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubmBtnCallListener. transfermsg () ;}});} */private IBtnCallListener mBtnCallListener; @ Overridepublic void listener (Fragment fragment) {// TODO Auto-generated method stubtry {mBtnCallListener = (IBtnCallListener) fragment;} catch (Exception e) {// TODO: handle exception} super. onAttachFragment (fragment);} @ Overridepublic void transfermsg () {// TODO Auto-generated method stubSystem. out. println ("information transmitted by Fragment");}/* private void initNavigationHSV () {rg_nav_content.removeAllViews (); for (int I = 0; I <tabTitle. length; I ++) {RadioButton rb = (RadioButton) mInflater. inflate (R. layout. nav_radiogroup_item, null); rb. setId (I); rb. setText (tabTitle [I]); rb. setLayoutParams (new LayoutParams (indicatorWidth, LayoutParams. MATCH_PARENT); rg_nav_content.addView (rb) ;}} private void findViewById () {rl_nav = (RelativeLayout) findViewById (R. id. rl_nav); mHsv = (SyncHorizontalScrollView) findViewById (R. id. mHsv); rg_nav_content = (RadioGroup) findViewById (R. id. rg_nav_content); iv_nav_indicator = (ImageView) findViewById (R. id. iv_nav_indicator); iv_nav_left = (ImageView) findViewById (R. id. iv_nav_left); iv_nav_right = (ImageView) findViewById (R. id. iv_nav_right); mViewPager = (ViewPager) findViewById (R. id. mViewPager) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true;} public static class TabFragmentPagerAdapter extends FragmentPagerAdapter {public TabFragmentPagerAdapter (FragmentManager fm) {super (fm) ;}@ Overridepublic Fragment getItem (int arg0) {Fragment ft = null; switch (arg0) {case 0: ft = new LaunchUIFragment (); break; default: ft = new CommonUIFragment (); Bundle args = new Bundle (); args. putString (ARGUMENTS_NAME, tabTitle [arg0]); ft. setArguments (args); break;} return ft ;}@ Overridepublic int getCount () {return tabTitle. length ;}}*/}

Flexible Interaction


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.