这次的实现的目的其实跟viewpager+view实现的功能一样的,唯一不同的是:这里的viewpager加载的是Fragment。
其中xml代码与之前的完全一样,所以这里就不贴了。这里我只贴下MainActivity的代码:
package com.example.android_tab3;
import java.util.ArrayList;
import java.util.List;
import com.example.Fragment.AddressFragment;
import com.example.Fragment.FriendFragment;
import com.example.Fragment.SettingFragment;
import com.example.Fragment.WeixinFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends FragmentActivity implements OnClickListener{
private ViewPager viewpager = null;
private List<Fragment> list = null;
private FragmentPagerAdapter adapter = null;
private ImageButton imagebutton_weixin = null;
private ImageButton imagebutton_friend = null;
private ImageButton imagebutton_address = null;
private ImageButton imagebutton_setting = null;
private LinearLayout linear_weixin = null;
private LinearLayout linear_friend = null;
private LinearLayout linear_address = null;
private LinearLayout linear_setting = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initview();
initEvents();
}
private void initview()
{
viewpager = (ViewPager) findViewById(R.id.viewpager);
imagebutton_weixin = (ImageButton) findViewById(R.id.imagebutton_weixin);
imagebutton_friend = (ImageButton) findViewById(R.id.imagebutton_friend);
imagebutton_address = (ImageButton) findViewById(R.id.imagebutton_address);
imagebutton_setting = (ImageButton) findViewById(R.id.imagebutton_setting);
linear_weixin = (LinearLayout) findViewById(R.id.linear_weixin);
linear_friend = (LinearLayout) findViewById(R.id.linear_friend);
linear_address = (LinearLayout) findViewById(R.id.linear_address);
linear_setting = (LinearLayout) findViewById(R.id.linear_setting);
list = new ArrayList<Fragment>();
Fragment weixinFragment = new WeixinFragment();
Fragment friendFragment = new FriendFragment();
Fragment addressFragment = new AddressFragment();
Fragment settingFragment = new SettingFragment();
list.add(weixinFragment);
list.add(friendFragment);
list.add(addressFragment);
list.add(settingFragment);
adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
public int getCount() {
return list.size();
}
public Fragment getItem(int position) {
return list.get(position);
}
};
viewpager.setAdapter(adapter);
viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
viewpager.setCurrentItem(position);
resetimage();
if(position == 0)
{
imagebutton_weixin.setImageResource(R.drawable.tab_weixin_pressed);
}
else if(position == 1)
{
imagebutton_friend.setImageResource(R.drawable.tab_find_frd_pressed);
}
else if(position == 2)
{
imagebutton_address.setImageResource(R.drawable.tab_address_pressed);
}
else if (position == 3)
{
imagebutton_setting.setImageResource(R.drawable.tab_settings_pressed);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
});
}
private void initEvents() {
linear_weixin.setOnClickListener(this);
Log.i("main", "2");
linear_friend.setOnClickListener(this);
linear_address.setOnClickListener(this);
linear_setting.setOnClickListener(this);
}
public void onClick(View v) {
resetimage();
switch(v.getId())
{
case R.id.linear_weixin:
{
viewpager.setCurrentItem(0);
imagebutton_weixin.setImageResource(R.drawable.tab_weixin_pressed);
break;
}
case R.id.linear_friend:
{
viewpager.setCurrentItem(1);
imagebutton_friend.setImageResource(R.drawable.tab_find_frd_pressed);
break;
}
case R.id.linear_address:
{
viewpager.setCurrentItem(2);
imagebutton_address.setImageResource(R.drawable.tab_address_pressed);
break;
}
case R.id.linear_setting:
{
viewpager.setCurrentItem(3);
imagebutton_setting.setImageResource(R.drawable.tab_settings_pressed);
break;
}
}
}
private void resetimage()
{
imagebutton_weixin.setImageResource(R.drawable.tab_weixin_normal);
imagebutton_friend.setImageResource(R.drawable.tab_find_frd_normal);
imagebutton_address.setImageResource(R.drawable.tab_address_normal);
imagebutton_setting.setImageResource(R.drawable.tab_settings_normal);
}
}