https://www.cnblogs.com/jerehedu/p/4663494.html
package main.com.pwj.testmvp.mvp.v2.view;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost;
import java.util.ArrayList;
import java.util.List;
import main.com.pwj.testmvp.R;
import main.com.pwj.testmvp.mvp.v2.MainContract;
import main.com.pwj.testmvp.mvp.v2.basemvp.BaseActivity;
import main.com.pwj.testmvp.mvp.v2.presenter.MainPresenter;
import main.com.pwj.testmvp.mvp.v2.until.TabDb;
//数据监测Activity
public class MainActivity extends BaseActivity<MainContract.IMainPresenter> implements MainContract.IMainView ,FragmentTabHost.OnTabChangeListener {
//数据采集,参数标定,故障诊断,程序下载,个人信息
private int[] selectTabRes = new int[]{R.drawable.tab_main, R.drawable.tab_personal};
private String[] title = new String[]{"采集", "我的"};
private Class fragmentArry[] = {SampleFragment.class, UserInfoFragment.class};
private SampleFragment sampleFragment;
private UserInfoFragment userInfoFragment;
private List<Fragment> fragmentList;
private FragmentTabHost tabHost;
private Toolbar toolbar;
//以下是通用的方法,通过抽象类来进行约束
@Override
protected void initLayout(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.fragment_tabhost_activity);
}
@Override
protected void initViews() {
//初始化TabHost文字颜色
tabHost=(FragmentTabHost)super.findViewById(android.R.id.tabhost);
tabHost.setup(this,super.getSupportFragmentManager()
,R.id.contentLayout);
tabHost.getTabWidget().setDividerDrawable(null);
tabHost.setOnTabChangedListener(this);
toolbar = (Toolbar) $(R.id.toolbar);
sampleFragment = new SampleFragment();
userInfoFragment = new UserInfoFragment();
fragmentList = new ArrayList();
toolbar.setTitle("法士特");
toolbar.setSubtitle("缓速器");
toolbar.inflateMenu(R.menu.toolbar_menu);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.action_blu:
startActivity(new Intent(getContext(), ScanListActivity.class));
break;
case R.id.action_blu_close:
Toast.makeText(getContext(),"蓝牙关闭",Toast.LENGTH_SHORT).show();
break;
case R.id.action_otherlink:
Toast.makeText(getContext(),"其他连接界面",Toast.LENGTH_SHORT).show();
break;
case R.id.action_remote:
Toast.makeText(getContext(),"远程登录界面",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
});
initTab();
// initListener();
}
private void initListener() {
}
private void initTab(){
String tabs[]= TabDb.getTabsTxt();
for(int i=0;i<tabs.length;i++){
TabHost.TabSpec tabSpec=tabHost.newTabSpec(tabs[i]).setIndicator(getTabView(i));
tabHost.addTab(tabSpec,TabDb.getFragments()[i],null);
tabHost.setTag(i);
}
}
private View getTabView(int idx){
View view=LayoutInflater.from(this).inflate(R.layout.footer_tabs,null);
((TextView)view.findViewById(R.id.tvTab)).setText(TabDb.getTabsTxt()[idx]);
if(idx==0){
((TextView)view.findViewById(R.id.tvTab)).setTextColor(Color.RED);
((ImageView)view.findViewById(R.id.ivImg)).setImageResource(TabDb.getTabsImgLight()[idx]);
}else{
// Log.i("序号", "getTabView: "+idx);
((ImageView)view.findViewById(R.id.ivImg)).setImageResource(TabDb.getTabsImg()[idx]);
}
return view;
}
/**
* 初始化Fragment并给ViewPager添加适配器
*/
@Override
protected void initData() {
}
// @Override
// protected void initData() {
// //mPresenter.handlerData();
//
// for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
// TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.tv_item);
// if (tabHost.getCurrentTab() == i) {//选中
// tv.setTextColor(Color.parseColor("#FF5959"));
// } else {//不选中
// tv.setTextColor(Color.parseColor("#777777"));
// }
// }
// }
//@Override
//public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// // getMenuInflater().inflate(R.menu.main, menu);
// return true;
//}
@Override
protected MainContract.IMainPresenter setPresenter() {
return new MainPresenter();
}
//以下是业务相关的方法
// @Override
// public void showDialog() {
// final ProgressDialog dialog=new ProgressDialog(getContext());
// dialog.show();
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// dialog.dismiss();
// }
// },1500);
// }
//
// @Override
// public void success(final String content){
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// Toast.makeText(getContext(),""+content,Toast.LENGTH_LONG).show();
// tvView.setText(content);
// }
// });
// }
@Override
public boolean onTouchEvent(MotionEvent arg0) {
return false;
}
@Override
public void updataRemoteStatue(boolean remoteStatue) {
}
@Override
public void updataBlueStatue(boolean blueStatue) {
}
@Override
public void updataProductType(String productType) {
}
@Override
public void onTabChanged(String tabId) {
updateTab();
}
private void updateTab(){
TabWidget tabw=tabHost.getTabWidget();
for(int i=0;i<tabw.getChildCount();i++){
View view=tabw.getChildAt(i);
ImageView iv=(ImageView)view.findViewById(R.id.ivImg);
if(i==tabHost.getCurrentTab()){
((TextView)view.findViewById(R.id.tvTab)).setTextColor(Color.RED);
iv.setImageResource(TabDb.getTabsImgLight()[i]);
}else{
((TextView)view.findViewById(R.id.tvTab)).setTextColor(getResources().getColor(R.color.colorAccent));
iv.setImageResource(TabDb.getTabsImg()[i]);
}
}
}
/**
* 适配器
* */
public class MyFragmentAdapter extends FragmentPagerAdapter {
List<Fragment> list;
public MyFragmentAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
}
网友评论