如图样式,
页面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ManagerActivity">
<com.example.library.TitleLayout
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="38dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="添加图书"
android:textSize="14sp"
android:textColor="#B9B9B9"
android:gravity="center"/>
<TextView
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="借阅归还"
android:textSize="14sp"
android:textColor="#B9B9B9"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#ffffff">
<ImageView
android:id="@+id/line_tv1"
android:src="@drawable/blind_gray"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/line_tv2"
android:visibility="invisible"
android:src="@drawable/blind_gray"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#F5F5F5"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
draw able的blind_gray
<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:shape="rectangle"
tools:ignore="MissingDefaultResource">
<size
android:width="15dp"
android:height="3dp" />
<solid android:color="#B9B9B9" />
<corners android:radius="2dp" />
</shape>
java中使用
public class ManagerActivity extends AppCompatActivity {
private TextView text;
private TextView tv1;
private TextView tv2;
private ImageView lineTv1;
private ImageView lineTv2;
private ViewPager pager;;
private List<TextView> tvs = new ArrayList<>();
private List<ImageView> tvlines = new ArrayList<>();
private List<View> views;
private List<Fragment> mFragments = new ArrayList<>();
private BookFragment fragment01;
private BorrowFragment fragment02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager);
ActionBar actionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.hide();
}
init();
text.setText("图书管理员");
initTextView();
// initView();
initViewPager();
}
public void initViewPager() {
// TODO Auto-generated method stub
SectionsPagerAdapter sectionsPagerAdapter;
pager = findViewById(R.id.pager);
fragment01 = new BookFragment();
fragment02 = new BorrowFragment();
mFragments.add(fragment01);
mFragments.add(fragment02);
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), mFragments);
pager.setAdapter(sectionsPagerAdapter);
pager.setOffscreenPageLimit(mFragments.size());
pager.setCurrentItem(0);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int index) {
// TODO Auto-generated method stub
for (int i = 0; i < tvs.size(); i++) {
if (i == index) {
tvs.get(i).setTextColor(Color.parseColor("#B9B9B9"));
tvlines.get(i).setVisibility(View.VISIBLE);
} else {
tvs.get(i).setTextColor(Color.parseColor("#333333"));
tvlines.get(i).setVisibility(View.INVISIBLE);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
//切换
private void initTextView() {
tv1.setOnClickListener(new MyClickListener(0));
tv2.setOnClickListener(new MyClickListener(1));
lineTv1.setOnClickListener(new MyClickListener(0));
lineTv2.setOnClickListener(new MyClickListener(1));
tvs.add(tv1);
tvs.add(tv2);
tvlines.add(lineTv1);
tvlines.add(lineTv2);
}
// private void initView() {
// LayoutInflater li = getLayoutInflater();
// views = new ArrayList<>();
// views.add(li.inflate(R.layout.fragment_item, null));
// views.add(li.inflate(R.layout.fragment_item, null));
// }
private void init(){
tv1=findViewById(R.id.tv1);
tv2=findViewById(R.id.tv2);
lineTv1=findViewById(R.id.line_tv1);
lineTv2=findViewById(R.id.line_tv2);
text=findViewById(R.id.title1);
}
private class MyClickListener implements View.OnClickListener {
private int index;
public MyClickListener(int index) {
// TODO Auto-generated constructor stub
this.index = index;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//改变ViewPager当前显示页面
pager.setCurrentItem(index);
}
}
@Override
protected void onRestart() {
super.onRestart();
}
}
SectionPagerAdapter
package com.example.library;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int i) {
return fragments.get(i);
}
@Override
public int getCount() {
return fragments.size();
}
}