Context context;
ViewPager viewPager;
List<Fragment> listView;
private TextView yb1;
private TextView yb2;
private TextView yb3;
private TextView yb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
init();
}
private void init() {
//找控件
viewPager=(ViewPager) findViewById(R.id.viewPager);
TextView textView1=(TextView) findViewById(R.id.textView1);
TextView textView2=(TextView) findViewById(R.id.textView2);
TextView textView3=(TextView) findViewById(R.id.textView3);
TextView textView4=(TextView) findViewById(R.id.textView4);
//4个游标
yb1 = (TextView) findViewById(R.id.yb1);
yb2 = (TextView) findViewById(R.id.yb2);
yb3 = (TextView) findViewById(R.id.yb3);
yb4 = (TextView) findViewById(R.id.yb4);
//设置点击监听事件
textView1.setOnClickListener(this);
textView2.setOnClickListener(this);
textView3.setOnClickListener(this);
textView4.setOnClickListener(this);
setFragment();
FragmentManager fm = getSupportFragmentManager();
//给ViewPager设置适配器
viewPager.setAdapter(new MyPagerAdapter(fm,listView));
yb1.setSelected(true);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int arg0) {
setTabColor(arg0);
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.textView1:
viewPager.setCurrentItem(0);
break;
case R.id.textView2:
viewPager.setCurrentItem(1);
break;
case R.id.textView3:
viewPager.setCurrentItem(2);
break;
case R.id.textView4:
viewPager.setCurrentItem(3);
break;
}
}
/**
* 设置游标颜色
*/
private void setTabColor(int position) {
switch (position) {
case 0:
yb1.setSelected(true);
yb1.setVisibility(View.VISIBLE);
yb2.setVisibility(View.GONE);
yb3.setVisibility(View.GONE);
yb4.setVisibility(View.GONE);
break;
case 1:
yb2.setSelected(true);
yb2.setVisibility(View.VISIBLE);
yb1.setVisibility(View.GONE);
yb3.setVisibility(View.GONE);
yb4.setVisibility(View.GONE);
break;
case 2:
yb3.setSelected(true);
yb3.setVisibility(View.VISIBLE);
yb2.setVisibility(View.GONE);
yb1.setVisibility(View.GONE);
yb4.setVisibility(View.GONE);
break;
case 3:
yb4.setSelected(true);
yb4.setVisibility(View.VISIBLE);
yb2.setVisibility(View.GONE);
yb3.setVisibility(View.GONE);
yb1.setVisibility(View.GONE);
break;
}
}
/**
* 设置Fragment
*/
private void setFragment() {
listView=new ArrayList<Fragment>();
listView.add(new Fragment1());
listView.add(new Fragment2());
listView.add(new Fragment3());
listView.add(new Fragment4());
}
<---------------------------main.xml----------------------------->
<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" >
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="资讯"
/>
<TextView
android:id="@+id/yb1"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@drawable/youbiao"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="热点"
/>
<TextView
android:id="@+id/yb2"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@drawable/youbiao"
android:visibility="gone"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="博客"
/>
<TextView
android:id="@+id/yb3"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@drawable/youbiao"
android:visibility="gone"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="推荐"
/>
<TextView
android:id="@+id/yb4"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@drawable/youbiao"
android:visibility="gone"
/>
</LinearLayout>
</RadioGroup>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
></android.support.v4.view.ViewPager>
</LinearLayout>
<---------------------------youbiao.xml----------------------------->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true" android:drawable="@color/yb_select"></item>
<item android:state_focused="true" android:drawable="@color/yb_select"></item>
<item android:state_checked="true" android:drawable="@color/yb_select"></item>
<item android:state_pressed="true" android:drawable="@color/yb_select"></item>
<item android:drawable="@color/yb_normal"></item>
</selector>
<---------------------------colors.xml----------------------------->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yb_select">#7CFC00</color>
<color name="yb_normal">#7CFC00</color>
</resources>