public class MainActivity extends FragmentActivity implements OnClickListener{
private ViewPager mViewPager;
private FragmentPagerAdapter mAdapter;
private List<Fragment>mFragments;
private LinearLayout mTabStadium;
private LinearLayout mTabFind;
private LinearLayout mTabMy;
private ImageButton mButtonStadium;
private ImageButton mButtonFind;
private ImageButton mButtonMy;
//初始化ListView
private ListView listView;
//private ArrayAdapter<String>arr_adapter;
private SimpleAdapter simple_adapter;
private List<Map<String,Object>>dataList;
// LayoutInflater inflater = getLayoutInflater();
/**
*
private Fragment mTab1;
private Fragment mTab2;
private Fragment mTab3;
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.listView1);
// ListView listView1 = (ListView) (inflater.inflate(R.layout.list, null)).findViewById(R.id.listView1);
//1.新建一个数据适配器
//ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
//SimpleAdapter()
/**
* context:上下文
* data: 数据源,一个Map所组成的List集合
* 每一个Map都会对应ListView列表中的一行
* 每一个Map(键-值对)中的键必须包含所有在from中所指定的键
* resource:列表项中的布局文件ID
* from: Map中的键名
* to: 绑定数据视图中的ID,与from成对应关系
*
*/
//2、适配器加载数据源
//String[]arr_data={"场地1","场地2","场地3","场地4"};
dataList=new ArrayList<Map<String,Object>>();
//arr_adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr_data);
simple_adapter=new SimpleAdapter(this, getData(), R.layout.tab1, new String[]{"pic","text"}, new int[]{R.id.pic,R.id.text});
//3.视图(ListView)加载适配器
// listView.setAdapter(arr_adapter);
listView.setAdapter(simple_adapter);
initView();
initEvents();
setSelect(0);
}
private void initEvents() {
// TODO Auto-generated method stub
mTabStadium.setOnClickListener(this);
mTabFind.setOnClickListener(this);
mTabMy.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
private void initView() {
// TODO Auto-generated method stub
mViewPager=(ViewPager) findViewById(R.id.id_viewPager);
mTabStadium=(LinearLayout) findViewById(R.id.id_tab_stadium);
mTabFind=(LinearLayout) findViewById(R.id.id_tab_find);
mTabMy=(LinearLayout) findViewById(R.id.id_tab_my);
mButtonStadium=(ImageButton) findViewById(R.id.imagebutton_stadium);
mButtonFind=(ImageButton) findViewById(R.id.imagebutton_find);
mButtonMy=(ImageButton) findViewById(R.id.imagebutton_my);
mFragments=new ArrayList<Fragment>();
Fragment mTab1=new StadiumFragment();
Fragment mTab2=new FindFragment();
Fragment mTab3=new MyFragment();
mFragments.add(mTab1);
mFragments.add(mTab2);
mFragments.add(mTab3);
mAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
// TODO Auto-generated method stub
return mFragments.size();
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return mFragments.get(arg0);
}
};
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener()
{
@Override
public void onPageSelected(int arg0)
{
int currentItem = mViewPager.getCurrentItem();
setTab(currentItem);
}
@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 v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.id_tab_stadium:
setSelect(0);
break;
case R.id.id_tab_find:
setSelect(1);
break;
case R.id.id_tab_my:
setSelect(2);
break;
default:
break;
}
}
private void setSelect(int i) {
//切换内容区域
setTab(i);
mViewPager.setCurrentItem(i);
}
private void setTab(int i)
{
resetImgs();
// 设置图片为亮色
// 切换内容区域
switch (i)
{
case 0:
mButtonStadium.setImageResource(R.drawable.tab_stadium_pressed);
break;
case 1:
mButtonFind.setImageResource(R.drawable.tab_find_pressed);
break;
case 2:
mButtonMy.setImageResource(R.drawable.tab_my_pressed);
break;
}
}
/**
* 切换图片至暗色
*/
private void resetImgs()
{
mButtonStadium.setImageResource(R.drawable.tab_stadium_normal);
mButtonFind.setImageResource(R.drawable.tab_find_normal);
mButtonMy.setImageResource(R.drawable.tab_my_normal);
}
//ListView:
private List<Map<String,Object>> getData(){
for(int i=0;i<=20;i++){
Map<String,Object>map=new HashMap<String,Object>();
map.put("pic", R.drawable.ic_launcher);
map.put("text", "慕课网"+i);
dataList.add(map);
}
return dataList;
}
}