package com.bwie.yuekao;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
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.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.TextView;
import com.bwie.fragment.Myfragment;
public class MainActivity extends FragmentActivity implements OnClickListener {
private LinearLayout liner;
private RadioButton zixun;
private RadioButton redian;
private RadioButton boke;
private RadioButton tuijian;
// 创建url集合
String[] url_list = new String[] {
"http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=",
"http://www.oschina.net/action/api/news_list?catalog=4&show=week&pageIndex=",
/*"http://www.oschina.net/action/api/news_list?catalog=4&show=week&pageIndex=",
"http://www.oschina.net/action/api/news_list?catalog=4&show=week&pageIndex=",*/
"http://www.oschina.net/action/api/blog_list?type=latest&pageIndex=",
"http://www.oschina.net/action/api/blog_list?type=recommend&pageIndex=" };
private int width;
private List<TextView> list_textView;
private LinearLayout.LayoutParams params;
private ViewPager viewPager;
private List<Fragment> list_fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 得到屏幕的宽度
width = getWindowManager().getDefaultDisplay().getWidth();
init();
}
private void init() {
// 找到控件
liner = (LinearLayout) findViewById(R.id.liner);
zixun = (RadioButton) findViewById(R.id.zixun);
redian = (RadioButton) findViewById(R.id.redian);
boke = (RadioButton) findViewById(R.id.boke);
tuijian = (RadioButton) findViewById(R.id.tuijian);
viewPager = (ViewPager) findViewById(R.id.viewPager);
// 设置点击监听
zixun.setOnClickListener(this);
redian.setOnClickListener(this);
boke.setOnClickListener(this);
tuijian.setOnClickListener(this);
list_textView = new ArrayList<TextView>();
for (int i = 0; i < url_list.length; i++) {
TextView text = new TextView(this);
params = new LayoutParams(width / 4, LayoutParams.WRAP_CONTENT);
// 设置下划线图片
text.setBackgroundColor(Color.RED);
// 设置隐藏
text.setVisibility(View.INVISIBLE);
list_textView.add(text);
liner.addView(text, i, params);
}
// 得到管理者
FragmentManager fm = getSupportFragmentManager();
// 创建集合
list_fragment = new ArrayList<Fragment>();
for (int i = 0; i < url_list.length; i++) {
Myfragment f = new Myfragment(url_list[i],i);
list_fragment.add(f);
}
// 设置字体为红色
zixun.setTextColor(Color.RED);
viewPager.setAdapter(new MyAdapter(fm, list_fragment));
// 给viewPager设置监听
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
zixun.setTextColor(Color.RED);
redian.setTextColor(Color.BLACK);
boke.setTextColor(Color.BLACK);
tuijian.setTextColor(Color.BLACK);
list_textView.get(0).setVisibility(View.VISIBLE);
list_textView.get(1).setVisibility(View.INVISIBLE);
list_textView.get(2).setVisibility(View.INVISIBLE);
list_textView.get(3).setVisibility(View.INVISIBLE);
break;
case 1:
zixun.setTextColor(Color.BLACK);
redian.setTextColor(Color.RED);
boke.setTextColor(Color.BLACK);
tuijian.setTextColor(Color.BLACK);
list_textView.get(1).setVisibility(View.VISIBLE);
list_textView.get(0).setVisibility(View.INVISIBLE);
list_textView.get(2).setVisibility(View.INVISIBLE);
list_textView.get(3).setVisibility(View.INVISIBLE);
break;
case 2:
zixun.setTextColor(Color.BLACK);
redian.setTextColor(Color.BLACK);
boke.setTextColor(Color.RED);
tuijian.setTextColor(Color.BLACK);
list_textView.get(2).setVisibility(View.VISIBLE);
list_textView.get(1).setVisibility(View.INVISIBLE);
list_textView.get(0).setVisibility(View.INVISIBLE);
list_textView.get(3).setVisibility(View.INVISIBLE);
break;
case 3:
zixun.setTextColor(Color.BLACK);
redian.setTextColor(Color.BLACK);
boke.setTextColor(Color.BLACK);
tuijian.setTextColor(Color.RED);
list_textView.get(3).setVisibility(View.VISIBLE);
list_textView.get(1).setVisibility(View.INVISIBLE);
list_textView.get(2).setVisibility(View.INVISIBLE);
list_textView.get(0).setVisibility(View.INVISIBLE);
break;
}
}
@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
}
});
}
@Override
public void onClick(View arg0) {
// 判断
switch (arg0.getId()) {
case R.id.zixun:
viewPager.setCurrentItem(0);
break;
case R.id.redian:
viewPager.setCurrentItem(1);
break;
case R.id.boke:
viewPager.setCurrentItem(2);
break;
case R.id.tuijian:
viewPager.setCurrentItem(3);
break;
}
}
}