时间:2021-09-09 09:52:19 | 栏目:Android代码 | 点击:次
大家在参考本地代码的时候要根据需要适当的修改,里面有冗余代码小编没有删除。好了,废话不多说了,一切让代码说话吧!
关键代码如下所示:
.java里面的主要代码
public class MainActivity extends BaseActivity implements TabChangeListener { private Fragment[] fragments; private FragZaiXianYuYue fragZaiXianYuYue; private FragDaoLuJiuYuan fragDaoLuJiuYuan; private FragJiFenShangCheng fragJiFenShangCheng; private FragMe fragMe; private ImageView img_right; private ImageView[] imagebuttons; private ImageView img_me_notity; private TextView[] textviews; private int index; private int currentTabIndex;// 当前fragment的index private int keyBackClickCount = 0; private int indexChange; private boolean isChangeTab = false; @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub super.onCreate(arg0); setContentView(R.layout.mainactivity); initView(); initTabView(); FragBYRecord.setTabChangeListener(this); FragWXRecord.setTabChangeListener(this); registerMessageReceiver(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); isForeground = true; requestMaintenance(); if (isChangeTab) { if (indexChange == 0) { viewTitle.setZhongJianText(R.string.frag_zxyy); tv_left.setVisibility(View.GONE); } else if (indexChange == 0) { viewTitle.setZhongJianText(R.string.frag_dljy); tv_left.setVisibility(View.GONE); } changeTab(indexChange); isChangeTab = false; } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); isForeground = false; } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if(null != mMessageReceiver) unregisterReceiver(mMessageReceiver); } private void initView() { viewTitle = (ViewTitle) findViewById(R.id.title_bar); img_right = viewTitle.getYoubianView(); img_right.setVisibility(View.VISIBLE); tv_left = viewTitle.getZuobianTextView(); viewTitle.getZuobianView().setVisibility(View.GONE); tv_left.setText("XXXX"); img_right.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(MainActivity.this, MineActivity.class); MineActivity.mFragValue = MineActivity.FRAG_SYSTEMMESSAGE; startActivity(intent); } }); tv_left.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { fragMe.addNewCar(); } }); } private void initTabView() { fragZaiXianYuYue = new FragZaiXianYuYue(); fragDaoLuJiuYuan = new FragDaoLuJiuYuan(); fragJiFenShangCheng = new FragJiFenShangCheng(); fragMe = new FragMe(); fragments = new Fragment[] { fragZaiXianYuYue, fragDaoLuJiuYuan, fragJiFenShangCheng, fragMe }; imagebuttons = new ImageView[4]; imagebuttons[0] = (ImageView) findViewById(R.id.ib_zzyy); imagebuttons[1] = (ImageView) findViewById(R.id.ib_dljy); imagebuttons[2] = (ImageView) findViewById(R.id.ib_jfsc); imagebuttons[3] = (ImageView) findViewById(R.id.ib_me); img_me_notity = (ImageView) findViewById(R.id.ib_me_notity); imagebuttons[0].setSelected(true); viewTitle.setZhongJianText(R.string.frag_zxyy); textviews = new TextView[4]; textviews[0] = (TextView) findViewById(R.id.tv_ib_zzyy); textviews[1] = (TextView) findViewById(R.id.tv_dljy); textviews[2] = (TextView) findViewById(R.id.tv_jfsc); textviews[3] = (TextView) findViewById(R.id.tv_me); textviews[0] .setTextColor(getResources().getColor(R.color.color_yellow)); // 添加显示第一个fragment getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, fragZaiXianYuYue) .add(R.id.fragment_container, fragDaoLuJiuYuan) .add(R.id.fragment_container, fragJiFenShangCheng) .add(R.id.fragment_container, fragMe).hide(fragDaoLuJiuYuan) .hide(fragJiFenShangCheng).hide(fragMe).show(fragZaiXianYuYue) .commit(); } public void onTabClicked(View view) { switch (view.getId()) { case R.id.re_zzyy: index = 0; if (fragZaiXianYuYue != null) { } viewTitle.setZhongJianText(R.string.frag_zxyy); tv_left.setVisibility(View.GONE); changeTab(index); break; case R.id.re_dljy: index = 1; viewTitle.setZhongJianText(R.string.frag_dljy); tv_left.setVisibility(View.GONE); changeTab(index); break; case R.id.re_jfsc: if(SharePreferUtil.getBoolean(CommonString.HAVESERVICE, false)){ index = 2; viewTitle.setZhongJianText(R.string.frag_jfsc); tv_left.setVisibility(View.GONE); fragJiFenShangCheng.requestJudgeCar(); changeTab(2); if(SharePreferUtil.getBoolean("isFristGoShop", true)){ SharePreferUtil.putBoolean("isFristGoShop", false); fragJiFenShangCheng.mengcengDialog(); } }else{ showAddServiceDialog(); } break; case R.id.re_me: index = 3; viewTitle.setZhongJianText(R.string.frag_me); tv_left.setVisibility(View.VISIBLE); changeTab(index); break; } } public void changeTab(int index) { if (currentTabIndex != index) { FragmentTransaction trx = getSupportFragmentManager() .beginTransaction(); trx.hide(fragments[currentTabIndex]); if (!fragments[index].isAdded()) { trx.add(R.id.fragment_container, fragments[index]); } trx.show(fragments[index]).commit(); } imagebuttons[currentTabIndex].setSelected(false); // 把当前tab设为选中状态 imagebuttons[index].setSelected(true); textviews[currentTabIndex].setTextColor(getResources().getColor( R.color.white)); textviews[index].setTextColor(getResources().getColor( R.color.color_yellow)); currentTabIndex = index; } mainactivity.xml: <include layout="@layout/layout_bottom"/> layout_bottom.xml: <?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="wrap_content" android:layout_alignParentBottom="true" android:background="@color/black" android:orientation="vertical" > <!-- 普通底部导航 --> <LinearLayout android:id="@+id/main_bottom" android:layout_width="match_parent" android:layout_height="54dp" android:gravity="center_vertical" android:orientation="horizontal" > <RelativeLayout android:id="@+id/re_zzyy" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onTabClicked" android:padding="3dp" > <RelativeLayout android:layout_width="60dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center" > <ImageView android:id="@+id/ib_zzyy" android:layout_width="30dp" android:layout_height="28dp" android:layout_centerHorizontal="true" android:focusable="false" android:scaleType="centerInside" android:src="@drawable/tab_zaixianyuyue" /> <TextView android:id="@+id/tv_ib_zzyy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ib_zzyy" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textColor="@color/white" android:text="@string/frag_zxyy" android:textSize="12sp" /> </RelativeLayout> </RelativeLayout> <RelativeLayout android:id="@+id/re_dljy" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onTabClicked" android:padding="3dp" > <RelativeLayout android:layout_width="60dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center" > <ImageView android:id="@+id/ib_dljy" android:layout_width="30dp" android:layout_height="28dp" android:layout_centerHorizontal="true" android:focusable="false" android:scaleType="centerInside" android:src="@drawable/tab_daolujiuyuan" /> <TextView android:id="@+id/tv_dljy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ib_dljy" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textColor="@color/white" android:text="@string/frag_dljy" android:textSize="12sp" /> </RelativeLayout> </RelativeLayout> <RelativeLayout android:id="@+id/re_jfsc" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onTabClicked" android:padding="3dp" > <RelativeLayout android:layout_width="60dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center" > <ImageView android:id="@+id/ib_jfsc" android:layout_width="30dp" android:layout_height="28dp" android:layout_centerHorizontal="true" android:focusable="false" android:scaleType="centerInside" android:src="@drawable/tab_jifenshangcheng" /> <TextView android:id="@+id/tv_jfsc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ib_jfsc" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textColor="@color/white" android:text="@string/frag_jfsc" android:textSize="12sp" /> </RelativeLayout> </RelativeLayout> <RelativeLayout android:id="@+id/re_me" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onTabClicked" android:padding="3dp" > <RelativeLayout android:layout_width="60dp" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:gravity="center" > <ImageView android:id="@+id/ib_me" android:layout_width="30dp" android:layout_height="28dp" android:layout_centerHorizontal="true" android:focusable="false" android:scaleType="centerInside" android:src="@drawable/tab_me" /> <ImageView android:id="@+id/ib_me_notity" android:layout_width="5dp" android:layout_height="5dp" android:layout_alignTop="@+id/ib_me" android:layout_alignRight="@+id/ib_me" android:scaleType="centerInside" android:src="@drawable/app_34_icon_point_normal" android:visibility="gone" /> <TextView android:id="@+id/tv_me" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/ib_me" android:layout_centerHorizontal="true" android:layout_marginTop="3dp" android:textColor="@color/white" android:text="@string/frag_me" android:textSize="12sp" /> </RelativeLayout> </RelativeLayout> </LinearLayout> </LinearLayout>