package com.ce.yuekao2;
import java.util.ArrayList;
import java.util.List;
import com.ce.adapter.VpAdapter;
import com.ce.fragment.F1;
import com.ce.fragment.F2;
import com.ce.fragment.F3;
import com.ce.fragment.F4;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
public class MainActivity extends FragmentActivity implements OnClickListener {
String[] urls = {
"http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=",
"http://www.oschina.net/action/api/news_list?catalog=4&pageIndex=",
"http://www.oschina.net/action/api/blog_list?type=latest&pageIndex=",
"http://www.oschina.net/action/api/blog_list?type=recommend&pageIndex=" };
private RadioButton b1;
private RadioButton b2;
private RadioButton b3;
private RadioButton b4;
private LinearLayout ll;
private ViewPager vp;
private int width;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
width = getWindowManager().getDefaultDisplay().getWidth();
initView();
}
private void initView() {
// 找到控件
b1 = (RadioButton) findViewById(R.id.r1);
b2 = (RadioButton) findViewById(R.id.r2);
b3 = (RadioButton) findViewById(R.id.r3);
b4 = (RadioButton) findViewById(R.id.r4);
ll = (LinearLayout) findViewById(R.id.ll);
vp = (ViewPager) findViewById(R.id.vp);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
// 给viewpager设置适配器
setViewpager();
// 设置游标
setIMG();
// 默认第一页展示的字体与游标
b1.setTextColor(Color.GREEN);
ll.getChildAt(0).setVisibility(View.VISIBLE);
vp.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
b1.setTextColor(Color.GREEN);
b2.setTextColor(Color.BLACK);
b3.setTextColor(Color.BLACK);
b4.setTextColor(Color.BLACK);
ll.getChildAt(0).setVisibility(View.VISIBLE);
ll.getChildAt(1).setVisibility(View.INVISIBLE);
ll.getChildAt(2).setVisibility(View.INVISIBLE);
ll.getChildAt(3).setVisibility(View.INVISIBLE);
break;
case 1:
b2.setTextColor(Color.GREEN);
b1.setTextColor(Color.BLACK);
b3.setTextColor(Color.BLACK);
b4.setTextColor(Color.BLACK);
ll.getChildAt(1).setVisibility(View.VISIBLE);
ll.getChildAt(0).setVisibility(View.INVISIBLE);
ll.getChildAt(2).setVisibility(View.INVISIBLE);
ll.getChildAt(3).setVisibility(View.INVISIBLE);
break;
case 2:
b3.setTextColor(Color.GREEN);
b2.setTextColor(Color.BLACK);
b1.setTextColor(Color.BLACK);
b4.setTextColor(Color.BLACK);
ll.getChildAt(2).setVisibility(View.VISIBLE);
ll.getChildAt(1).setVisibility(View.INVISIBLE);
ll.getChildAt(0).setVisibility(View.INVISIBLE);
ll.getChildAt(3).setVisibility(View.INVISIBLE);
break;
case 3:
b4.setTextColor(Color.GREEN);
b2.setTextColor(Color.BLACK);
b3.setTextColor(Color.BLACK);
b1.setTextColor(Color.BLACK);
ll.getChildAt(3).setVisibility(View.VISIBLE);
ll.getChildAt(1).setVisibility(View.INVISIBLE);
ll.getChildAt(2).setVisibility(View.INVISIBLE);
ll.getChildAt(0).setVisibility(View.INVISIBLE);
break;
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@SuppressLint("NewApi")
private void setIMG() {
for (int i = 0; i < urls.length; i++) {
// 创建imageview 控件
ImageView img = new ImageView(MainActivity.this);
img.setBackgroundResource(R.drawable.color);
LayoutParams params = new LayoutParams(width / 4,
LayoutParams.WRAP_CONTENT);
img.setVisibility(View.INVISIBLE);
ll.addView(img, params);
}
}
private void setViewpager() {
// 创建集合
List<Fragment> list = new ArrayList<Fragment>();
for (int i = 0; i < urls.length; i++) {
// 创建page页
F1 f1 = new F1(urls[0], i);
F2 f2 = new F2(urls[1], i);
F3 f3 = new F3(urls[2], i);
F4 f4 = new F4(urls[3], i);
list.add(f1);
list.add(f2);
list.add(f3);
list.add(f4);
}
FragmentManager fm = getSupportFragmentManager();
// 给viewpager设置适配器
vp.setAdapter(new VpAdapter(fm, list));
}
public void onClick(View v) {
// 当点击不同的radiobutton跳转不同的页面
switch (v.getId()) {
case R.id.r1:
b1.setTextColor(Color.GREEN);
vp.setCurrentItem(0);
break;
case R.id.r2:
vp.setCurrentItem(1);
break;
case R.id.r3:
vp.setCurrentItem(2);
break;
case R.id.r4:
vp.setCurrentItem(3);
break;
}
}
}
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"
tools:context=".MainActivity" >
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:button="@null"
android:text="资讯"
android:textSize="24dp" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:button="@null"
android:text="热点"
android:textSize="24dp" />
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:button="@null"
android:text="博客"
android:textSize="24dp" />
<RadioButton
android:id="@+id/r4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:button="@null"
android:text="推荐"
android:textSize="24dp" />
</RadioGroup>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>