滚动带游标 xml布局
<?xml version="1.0"?>
<LinearLayout android:orientation="vertical" tools:context=".MainActivity" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/line_one"><RadioGroup android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio_group"><RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio_one" android:button="@null " android:text="资讯"/><RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio_two" android:button="@null" android:text="推荐"/><RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio_three" android:button="@null" android:text="新闻"/><RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio_four" android:button="@null" android:text="博客"/></RadioGroup></LinearLayout><LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent"><TextView android:layout_height="3dp" android:layout_width="60dp" android:id="@+id/tv1" android:layout_marginLeft="10dp" android:background="#0f0"/><TextView android:layout_height="3dp" android:layout_width="60dp" android:id="@+id/tv2" android:layout_marginLeft="30dp" android:background="#0f0" android:visibility="invisible"/><TextView android:layout_height="3dp" android:layout_width="60dp" android:id="@+id/tv3" android:layout_marginLeft="40dp" android:background="#0f0" android:visibility="invisible"/><TextView android:layout_height="3dp" android:layout_width="60dp" android:id="@+id/tv4" android:layout_marginLeft="30dp" android:background="#0f0" android:visibility="invisible"/></LinearLayout><android.support.v4.view.ViewPager android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/vp"/></LinearLayout>
Mainactivity 页面
public class MainActivity extends FragmentActivity {
private RadioGroup radio_group;
private List<Fragment> fragment=new ArrayList<Fragment>();
private List<String> str;
private ViewPager vp;
private String[] url={"http://www.oschina.net/action/api/news_list?catalog=1/pageSize=5/pageIndex=",
"http://www.oschina.net/action/api/news_list?catalog=4/pageSize=5/show=week/pageIndex=",
"http://www.oschina.net/action/api/blog_list/type=latest/pageSize=5/pageIndex=",
"http://www.oschina.net/action/api/blog_list/type=recommend/pageSize=5/pageIndex="};
private RadioButton but_one,but_two,but_three,but_four;
private TextView tv1,tv2,tv3,tv4;
private String []st=new String[]{"资讯","推荐","微博","博客"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radio_group=(RadioGroup) findViewById(R.id.radio_group);
but_one=(RadioButton) findViewById(R.id.radio_one);
but_two=(RadioButton) findViewById(R.id.radio_two);
but_three=(RadioButton) findViewById(R.id.radio_three);
but_four=(RadioButton) findViewById(R.id.radio_four);
vp=(ViewPager) findViewById(R.id.vp);
tv1=(TextView) findViewById(R.id.tv1);
tv2=(TextView) findViewById(R.id.tv2);
tv3=(TextView) findViewById(R.id.tv3);
tv4=(TextView) findViewById(R.id.tv4);
FragmentManager fm=getSupportFragmentManager();
for(int i=0;i<st.length;i++){
FragmentAc ff=new FragmentAc(url, i);
fragment.add(ff);
}
Fragadapter adapter=new Fragadapter(fm, fragment);
vp.setAdapter(adapter);
getcolor(vp.getCurrentItem());
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
getcolor(arg0);
getvisibility(arg0);
}
@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 getvisibility(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
tv1.setVisibility(View.VISIBLE);
tv2.setVisibility(View.INVISIBLE);
tv3.setVisibility(View.INVISIBLE);
tv4.setVisibility(View.INVISIBLE);
break;
case 1:
tv1.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.VISIBLE);
tv3.setVisibility(View.INVISIBLE);
tv4.setVisibility(View.INVISIBLE);
break;
case 2:
tv1.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.INVISIBLE);
tv3.setVisibility(View.VISIBLE);
tv4.setVisibility(View.INVISIBLE);
break;
case 3:
tv1.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.INVISIBLE);
tv3.setVisibility(View.INVISIBLE);
tv4.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
private void getcolor(int currentItem) {
// TODO Auto-generated method stub
switch (currentItem) {
case 0:
but_one.setTextColor(Color.GREEN);
but_two.setTextColor(Color.BLACK);
but_three.setTextColor(Color.BLACK);
but_four.setTextColor(Color.BLACK);
break;
case 1:
but_one.setTextColor(Color.BLACK);
but_two.setTextColor(Color.GREEN);
but_three.setTextColor(Color.BLACK);
but_four.setTextColor(Color.BLACK);
break;
case 2:
but_one.setTextColor(Color.BLACK);
but_two.setTextColor(Color.BLACK);
but_three.setTextColor(Color.GREEN);
but_four.setTextColor(Color.BLACK);
break;
case 3:
but_one.setTextColor(Color.BLACK);
but_two.setTextColor(Color.BLACK);
but_three.setTextColor(Color.BLACK);
but_four.setTextColor(Color.GREEN);
break;
default:
break;
}
}
}