1.activity_main.xml
1)图片选择器 main_girl.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_main_chat_selector" android:state_selected="true"/>
<item android:drawable="@drawable/ic_main_chat"/>
</selector>
2)字体颜色选择 main_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/redColor" android:state_selected="true"/>
<item android:color="@color/titleColor"/>
</selector>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@color/navigationColor"
android:gravity="center_vertical"
android:orientation="horizontal">
<Button
android:id="@+id/news"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:drawableTop="@drawable/main_news"
android:paddingBottom="2dp"
android:paddingTop="7dp"
android:text="@string/fragment_news"
android:textColor="@drawable/main_text"
android:textSize="12sp" />
<Button
android:id="@+id/video"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:drawableTop="@drawable/main_video"
android:paddingBottom="2dp"
android:paddingTop="7dp"
android:text="@string/fragment_video"
android:textColor="@drawable/main_text"
android:textSize="12sp" />
<Button
android:id="@+id/girl"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:drawableTop="@drawable/main_girl"
android:paddingBottom="2dp"
android:paddingTop="7dp"
android:text="@string/fragment_girl"
android:textColor="@drawable/main_text"
android:textSize="12sp" />
<Button
android:id="@+id/mine"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:drawableTop="@drawable/main_mine"
android:paddingBottom="2dp"
android:paddingTop="7dp"
android:text="@string/fragment_mine"
android:textColor="@drawable/main_text"
android:textSize="12sp" />
</LinearLayout>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_above="@id/bottom"
android:background="@color/dividerColor" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/divider" />
<com.huading.myvolley.widget.LoadStateLayout
android:id="@+id/ls_loadStateLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.huading.myvolley.widget.LoadStateLayout>
</RelativeLayout>
2.MainActivity
1)继承BaseActivity
2)fragment的创建
3)viewPager的适配器
4)viewPager的监听
5)按钮的点击事件
6)Handler的进程通讯
RootFragmentPagerAdapter
package com.huading.myvolley.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* 作者:LHZ on 2017/5/27 13:48
* 界面:
*/
public class RootFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> list;
public RootFragmentPagerAdapter(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();
}
}
MainActivity
package com.huading.myvolley;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.huading.myvolley.adapter.RootFragmentPagerAdapter;
import com.huading.myvolley.base.BaseActivity;
import com.huading.myvolley.ui.OneFragment;
import com.huading.myvolley.ui.TwoFragment;
import com.huading.myvolley.ui.ThreeFragment;
import com.huading.myvolley.ui.FourFragment;
import com.huading.myvolley.widget.LoadStateLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.InjectView;
import butterknife.OnClick;
import static com.huading.myvolley.R.id.viewPager;
public class MainActivity extends BaseActivity {
@InjectView(R.id.ls_loadStateLayout)
LoadStateLayout lsLoadStateLayout;
@InjectView(R.id.news)
Button mNews;
@InjectView(R.id.video)
Button mVideo;
@InjectView(R.id.girl)
Button mGirl;
@InjectView(R.id.mine)
Button mMine;
@InjectView(viewPager)
ViewPager mViewPager;
int currentTabPosition = 0;
@Override
protected int getContentViewLayoutID() {
return R.layout.activity_main;
}
@Override
protected void initView() {
Fragment oneFragment = new OneFragment();
Fragment twoFragment = new TwoFragment();
Fragment threeFragment = new ThreeFragment();
Fragment fourFragment = new FourFragment();
List<Fragment> fragments = new ArrayList<Fragment>();
fragments.add(oneFragment);
fragments.add(twoFragment);
fragments.add(threeFragment);
fragments.add(fourFragment);
// ViewPager设置适配器
mViewPager.setAdapter(new RootFragmentPagerAdapter(getSupportFragmentManager(), fragments));
// ViewPager滑动监听
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// 初始化tab
initTab();
switch (position) {
case 0:
mNews.setSelected(true);
break;
case 1:
mVideo.setSelected(true);
break;
case 2:
mGirl.setSelected(true);
break;
case 3:
mMine.setSelected(true);
break;
default:
//其他
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
// 初始化
mViewPager.setCurrentItem(currentTabPosition);
mNews.setSelected(true);
// showLoadingDialog("努力加载中");
lsLoadStateLayout.showLoading("拼命加载中");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Message msg = Message.obtain();
msg.what = 110;
msg.obj = "雄安";
mHandler.sendMessage(msg);
}
}, 3000);
}
@Override
protected void initData() {
}
@OnClick({R.id.news, R.id.video, R.id.girl, R.id.mine})
public void onClick(View view) {
switch (view.getId()) {
case R.id.news:
mViewPager.setCurrentItem(0, false);
break;
case R.id.video:
mViewPager.setCurrentItem(1, false);
break;
case R.id.girl:
mViewPager.setCurrentItem(2, false);
break;
case R.id.mine:
mViewPager.setCurrentItem(3, false);
break;
default:
break;
}
}
private void initTab() {
mNews.setSelected(false);
mVideo.setSelected(false);
mGirl.setSelected(false);
mMine.setSelected(false);
}
@Override
protected void onHandlerThread(Message msg) {
// hideLoadingDialog();
super.onHandlerThread(msg);
switch (msg.what) {
case 110:
String string = (String) msg.obj;
if (string.equals("雄安新区")) {
lsLoadStateLayout.showSuccess();
Toast.makeText(mContext, string, Toast.LENGTH_SHORT).show();
} else {
lsLoadStateLayout.showEmpty();
}
break;
}
}
}
注意地方:
Fragment oneFragment = new OneFragment();要用父类Fragment
mViewPager.setAdapter(new RootFragmentPagerAdapter(getSupportFragmentManager(), fragments));导包问题,一定要用v4包下的fragment
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
-----------------------------------------------------------------
1.增加修改颜色值Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#FF03A9F4</color>
<color name="colorPrimaryDark">#FF0288D1</color>
<color name="colorAccent">#FF4081</color>
<color name="transparent">#00000000</color> <!--透明 -->
<color name="white">#FFFFFF</color> <!--白色 -->
<color name="black">#000000</color> <!--黑色 -->
<color name="background">#F2F2F2</color> <!--背景色 -->
<color name="navigationColor">#F8F8F8</color> <!--状态导航栏背景色-->
<color name="dividerColor">#EEEEEE</color> <!--二级分割线-->
<color name="divider">#DDDDDD</color> <!--分割线-->
<color name="titleColor">#999999</color> <!--导航文字-->
<color name="textColor">#333333</color> <!--文字颜色-->
<color name="text">#666666</color> <!--次文字-->
<color name="blueColor">#00AAFF</color> <!--标准蓝-->
<color name="orangeColor">#FFAB19</color> <!--标准橙-->
<color name="orange">#FB7E00</color> <!--标准深橙-->
<color name="greenColor">#5ED125</color> <!--标准绿-->
<color name="redColor">#FF4343</color> <!--标准红-->
<color name="pinkColor">#DE1E7C</color> <!--标准粉红-->
<!--PagerSlidingTab的颜色值-->
<color name="background_tab_pressed">#BEBEBE</color>
<color name="slidingtab_indicatorcolor">#FF5055</color>
</resources>
2.更改styles
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!--隐藏ActionBar-->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
3.增加strings
<resources>
<string name="app_name">MyVolley</string>
<!--底部导航栏文字-->
<string-array name="main_titles">
<item>@string/fragment_news</item>
<item>@string/fragment_video</item>
<item>@string/fragment_girl</item>
<item>@string/fragment_mine</item>
</string-array>
<string name="fragment_news">新闻</string>
<string name="fragment_video">视频</string>
<string name="fragment_girl">发现</string>
<string name="fragment_mine">我的</string>
</resources>
4.图片状态选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_main_news_selector" android:state_selected="true"/>
<item android:drawable="@drawable/ic_main_news"/>
</selector>
5.文字状态选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_selected="true"/>
<item android:color="@color/titleColor"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@color/navigationColor"
android:gravity="center_vertical"
android:orientation="horizontal">
<Button
android:id="@+id/news"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/main_news"
android:paddingBottom="2dp"
android:paddingTop="7dp"
android:background="@null"
android:text="@string/fragment_news"
android:textColor="@color/titleColor"
android:textSize="12sp" />
<Button
android:id="@+id/video"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/main_video"
android:paddingBottom="2dp"
android:paddingTop="7dp"
android:background="@null"
android:text="@string/fragment_video"
android:textColor="@color/titleColor"
android:textSize="12sp" />
<Button
android:id="@+id/girl"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/main_girl"
android:paddingBottom="2dp"
android:paddingTop="7dp"
android:background="@null"
android:text="@string/fragment_girl"
android:textColor="@color/titleColor"
android:textSize="12sp" />
<Button
android:id="@+id/mine"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/main_mine"
android:paddingBottom="2dp"
android:paddingTop="7dp"
android:background="@null"
android:text="@string/fragment_mine"
android:textColor="@color/titleColor"
android:textSize="12sp" />
</LinearLayout>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_above="@id/bottom"
android:background="@color/dividerColor" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/divider" />
</RelativeLayout>