Android編程心得-FragmentActivity與Fragment兩者互動方法簡介

來源:互聯網
上載者:User


public interface IBtnCallListener {       public void transfermsg();}

public class LaunchUIFragment extends Fragment implements IBtnCallListener{IBtnCallListener mbtnListener; private Button btn_Click;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {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("由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 iv_nav_indicator;private ImageView iv_nav_left;private ImageView iv_nav_right;private ViewPager mViewPager;private int indicatorWidth;public static String[] tabTitle = { "選項1", "選項2", "選項3", "選項4", "選項5" };//標題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)).performClick();}}@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_nav_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);//執行位移動畫iv_nav_indicator.startAnimation(animation);mViewPager.setCurrentItem(checkedId);//ViewPager 跟隨一起 切換//記錄當前 下標的距最左側的 距離currentIndicatorLeft = ((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().getDefaultDisplay().getMetrics(dm);indicatorWidth = dm.widthPixels / 4;LayoutParams cursor_Params = iv_nav_indicator.getLayoutParams();cursor_Params.width = indicatorWidth;// 初始化滑動下標的寬iv_nav_indicator.setLayoutParams(cursor_Params);mHsv.setSomeParam(rl_nav, iv_nav_left, iv_nav_right, this);//擷取布局填充器mInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);//另一種方式擷取//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 onAttachFragment(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("由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;}}*/}

靈活互動


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.