在Fragment中如何使用Toolbar
阿新 • • 發佈:2019-01-03
這個Toolbar實在折磨了我好幾天。。。知道看到下面這個提醒然後出去吹了吹風,終於弄出來了....其實跟提醒無關。我卡在這句上了....
toolbar.inflateMenu(R.menu.menu_main);
程式碼中還包含 fragment+viewpager+radiobutton實現的仿微信主介面效果。還是要提醒:
在NoActionBar的主題中
1. onCreateOptionsMenu方法不會執行
2. 即使在關聯ActionBar後也不會觸發onCreateOptionsMenu,此監聽只對ActionBar有效
FragmentStore.java
//匯入fragment的menu檔案 toolbar.inflateMenu(R.menu.menu_main); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: mActivity.finish(); case R.id.overflow: Intent intent=new Intent(); intent.setClass(mActivity, SelectActivity.class); startActivity(intent); //設定切換動畫,從右邊進入,左邊停留 mActivity.overridePendingTransition(R.anim.in_from_right, R.anim.stay); } return true; } }); return view; }}package com.example.demo.myapplication.fragment; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.demo.myapplication.R; import com.example.demo.myapplication.activity.SelectActivity; import com.example.demo.myapplication.activity.UpdatePhotoActivity; public class FragmentStore extends Fragment { Activity mActivity; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mActivity = getActivity(); //fragment的佈局檔案 View view = inflater.inflate(R.layout.fragment_special_offer, container, false); TextView content = (TextView) view.findViewById(R.id.content); content.setText("商城"); Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbar); <pre name="code" class="java"> ImageButton selectBtn= (ImageButton) view.findViewById(R.id.select); selectBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mActivity,"你點選了選擇按鈕",Toast.LENGTH_SHORT).show(); } });
MainActivity.java
package com.example.demo.myapplication.activity; import android.content.Intent; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import com.example.demo.myapplication.R; import com.example.demo.myapplication.adapter.MyFragmentPagerAdapter; import com.example.demo.myapplication.fragment.FragmentMine; import com.example.demo.myapplication.fragment.FragmentSpecialOffer; import com.example.demo.myapplication.fragment.FragmentStore; import com.example.demo.myapplication.fragment.MyFragment3; import java.util.ArrayList; import java.util.List; public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener { //UI Objects private RadioGroup rg_tab_bar; private RadioButton rb_channel; private RadioButton rb_message; private RadioButton rb_better; private RadioButton rb_setting; private ViewPager vpager; //幾個代表頁面的常量 public static final int PAGE_ONE = 0; public static final int PAGE_TWO = 1; public static final int PAGE_THREE = 2; public static final int PAGE_FOUR = 3; private MyFragmentPagerAdapter mAdapter; @Override protected void initView(Bundle savedInstanceState) { setContentView(R.layout.activity_main); bindViews(); rb_channel.setChecked(true); } private void bindViews() { rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar); rb_channel = (RadioButton) findViewById(R.id.rb_channel); rb_message = (RadioButton) findViewById(R.id.rb_message); rb_better = (RadioButton) findViewById(R.id.rb_better); rb_setting = (RadioButton) findViewById(R.id.rb_setting); rg_tab_bar.setOnCheckedChangeListener(this); //構造介面卡 List<Fragment> fragments=new ArrayList<Fragment>(); fragments.add(new FragmentSpecialOffer()); fragments.add(new FragmentStore()); fragments.add(new MyFragment3()); fragments.add(new FragmentMine()); mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),fragments); //設定介面卡 vpager = (ViewPager) findViewById(R.id.vpager); vpager.setAdapter(mAdapter); vpager.setCurrentItem(0); vpager.addOnPageChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb_channel: vpager.setCurrentItem(PAGE_ONE); break; case R.id.rb_message: vpager.setCurrentItem(PAGE_TWO); break; case R.id.rb_better: vpager.setCurrentItem(PAGE_THREE); break; case R.id.rb_setting: vpager.setCurrentItem(PAGE_FOUR); break; } } //重寫ViewPager頁面切換的處理方法 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { //state的狀態有三個,0表示什麼都沒做,1正在滑動,2滑動完畢 if (state == 2) { switch (vpager.getCurrentItem()) { case PAGE_ONE: rb_channel.setChecked(true); break; case PAGE_TWO: rb_message.setChecked(true); break; case PAGE_THREE: rb_better.setChecked(true); break; case PAGE_FOUR: rb_setting.setChecked(true); break; } } } }
MyFragmentPagerAdapter.java
package com.example.demo.myapplication.adapter;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
import java.util.List;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments;
public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
@Override
public Object instantiateItem(ViewGroup vg, int position) {
return super.instantiateItem(vg, position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
}
fragment_store.xml fragment的佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<include layout="@layout/fragment_special_offer_toolbar" />
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="呵呵"
android:textSize="20sp" />
</LinearLayout>
fragment_special_offer_toolbar.xml toolbar的佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:title="title"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:navigationIcon="@drawable/icon_diamond_press"
android:minHeight="?attr/actionBarSize">
<pre style="background-color:#ffffff;color:#000000;font-family:'Liberation Mono';font-size:10.5pt;"><pre name="code" class="html"> <ImageButton
android:id="@+id/select"
android:background="@drawable/icon_diamond"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="right"
android:layout_marginRight="4dp"/><pre style="background-color:#ffffff;color:#000000;font-family:'Liberation Mono';font-size:10.5pt;"><pre name="code" class="html"> </android.support.v7.widget.Toolbar>
</LinearLayout>
menu_main.xml <menu 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" tools:context=".MainActivity">
<item
android:id="@+id/overflow"
android:title="展開"
app:showAsAction="always"
android:orderInCategory="100">
</item>
</menu>
style.xml<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>