代码片Android ViewPager Fragment实现选项卡和fragment
2015-01-03 09:44
387 查看
public class TestFragment extends Fragment { private static final String TAG = "TestFragment"; private String hello;// = "hello android"; private String defaultHello = "default value"; private Map maplist; static TestFragment newInstance(String s, Map map) { TestFragment newFragment = new TestFragment(); // Bundle bundle = new Bundle(); // bundle.putString("hello", s); // newFragment.setArguments(bundle); final SerializableMap myMap=new SerializableMap(); myMap.setMap(map); Bundle bundle = new Bundle(); bundle.putSerializable("map", myMap); newFragment.setArguments(bundle); return newFragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "TestFragment-----onCreate"); Bundle args = getArguments(); // hello = args != null ? args.getString("hello") : defaultHello; Bundle bundle = getArguments(); SerializableMap serializableMap = (SerializableMap) bundle.get("map"); maplist =serializableMap.getMap(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { Log.d(TAG, "TestFragment-----onCreateView"); View view = inflater.inflate(R.layout.lay1, container, false); // TextView viewhello = (TextView) view.findViewById(R.id.tv_hello); //viewhello.setText(maplist.get("userid")+"time"); ListView lv = (ListView) view.findViewById(R.id.listView3); ContactAdapter hc = new ContactAdapter(getActivity().getApplicationContext(),getContact()); lv.setAdapter(hc); lv.setCacheColorHint(0); return view; } private ArrayList getContact(){ ArrayList hcList = new ArrayList(); for(int i=0;i<10;i++) { Contact c0 = new Contact(); c0.setTxPath(R.drawable.more_game+""); c0.setName(maplist.get("userid")+" 年龄:"+maplist.get("age")); hcList.add(c0); } return hcList; } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "TestFragment-----onDestroy"); } } public class MyFragmentPagerAdapter extends FragmentPagerAdapter { private ArrayList fragmentsList; public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } public MyFragmentPagerAdapter(FragmentManager fm, ArrayList fragments) { super(fm); this.fragmentsList = fragments; } @Override public int getCount() { return fragmentsList.size(); } @Override public Fragment getItem(int arg0) { return fragmentsList.get(arg0); } @Override public int getItemPosition(Object object) { return super.getItemPosition(object); } } public class MainActivity extends FragmentActivity { private static final String TAG = "MainActivity"; private ViewPager mPager; private ArrayList fragmentsList; private ImageView ivBottomLine; private TextView tvTabActivity, tvTabGroups, tvTabFriends, tvTabChat; private int currIndex = 0; private int bottomLineWidth; private int offset = 0; private int position_one; private int position_two; private int position_three; private Resources resources; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); resources = getResources(); InitWidth(); InitTextView(); InitViewPager(); } private void InitTextView() { tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity); tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups); tvTabFriends = (TextView) findViewById(R.id.tv_tab_friends); tvTabChat = (TextView) findViewById(R.id.tv_tab_chat); tvTabActivity.setOnClickListener(new MyOnClickListener(0)); tvTabGroups.setOnClickListener(new MyOnClickListener(1)); tvTabFriends.setOnClickListener(new MyOnClickListener(2)); tvTabChat.setOnClickListener(new MyOnClickListener(3)); } private void InitViewPager() { mPager = (ViewPager) findViewById(R.id.vPager); fragmentsList = new ArrayList(); Map paramMap = new HashMap(); paramMap.put("userid","小洪"); paramMap.put("age",23); Map paramMap2 = new HashMap(); paramMap2.put("userid","vatty"); paramMap2.put("age",24); Map paramMap3 = new HashMap(); paramMap3.put("userid","小明"); paramMap3.put("age",25); Map paramMap4 = new HashMap(); paramMap4.put("userid","hongshengpeng.com"); paramMap4.put("age",26); Fragment activityfragment = TestFragment.newInstance("Hello Activity.",paramMap); Fragment groupFragment = TestFragment.newInstance("Hello Group.",paramMap2); Fragment friendsFragment=TestFragment.newInstance("Hello Friends.",paramMap3); Fragment chatFragment=TestFragment.newInstance("Hello Chat.",paramMap4); fragmentsList.add(activityfragment); fragmentsList.add(groupFragment); fragmentsList.add(friendsFragment); fragmentsList.add(chatFragment); mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList)); mPager.setCurrentItem(0); mPager.setOnPageChangeListener(new MyOnPageChangeListener()); } private void InitWidth() { ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line); bottomLineWidth = ivBottomLine.getLayoutParams().width; Log.d(TAG, "cursor imageview width=" + bottomLineWidth); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW = dm.widthPixels; offset = (int) ((screenW / 4.0 - bottomLineWidth) / 2); Log.i("MainActivity", "offset=" + offset); position_one = (int) (screenW / 4.0); position_two = position_one * 2; position_three = position_one * 3; } public class MyOnClickListener implements View.OnClickListener { private int index = 0; public MyOnClickListener(int i) { index = i; } @Override public void onClick(View v) { mPager.setCurrentItem(index); } }; public class MyOnPageChangeListener implements OnPageChangeListener { @Override public void onPageSelected(int arg0) { Animation animation = null; switch (arg0) { case 0: if (currIndex == 1) { animation = new TranslateAnimation(position_one, 0, 0, 0); tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite)); } else if (currIndex == 2) { animation = new TranslateAnimation(position_two, 0, 0, 0); tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite)); } else if (currIndex == 3) { animation = new TranslateAnimation(position_three, 0, 0, 0); tvTabChat.setTextColor(resources.getColor(R.color.lightwhite)); } tvTabActivity.setTextColor(resources.getColor(R.color.white)); break; case 1: if (currIndex == 0) { animation = new TranslateAnimation(0, position_one, 0, 0); tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite)); } else if (currIndex == 2) { animation = new TranslateAnimation(position_two, position_one, 0, 0); tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite)); } else if (currIndex == 3) { animation = new TranslateAnimation(position_three, position_one, 0, 0); tvTabChat.setTextColor(resources.getColor(R.color.lightwhite)); } tvTabGroups.setTextColor(resources.getColor(R.color.white)); break; case 2: if (currIndex == 0) { animation = new TranslateAnimation(0, position_two, 0, 0); tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite)); } else if (currIndex == 1) { animation = new TranslateAnimation(position_one, position_two, 0, 0); tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite)); } else if (currIndex == 3) { animation = new TranslateAnimation(position_three, position_two, 0, 0); tvTabChat.setTextColor(resources.getColor(R.color.lightwhite)); } tvTabFriends.setTextColor(resources.getColor(R.color.white)); break; case 3: if (currIndex == 0) { animation = new TranslateAnimation(0, position_three, 0, 0); tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite)); } else if (currIndex == 1) { animation = new TranslateAnimation(position_one, position_three, 0, 0); tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite)); } else if (currIndex == 2) { animation = new TranslateAnimation(position_two, position_three, 0, 0); tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite)); } tvTabChat.setTextColor(resources.getColor(R.color.white)); break; } currIndex = arg0; animation.setFillAfter(true); animation.setDuration(300); ivBottomLine.startAnimation(animation); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } } } <?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
相关文章推荐
- Android ViewPager Fragment实现选项卡
- Android ViewPager Fragment实现选项卡和fragment
- Android使用ViewPager+Fragment实现定制TabHost选项卡效果
- android典型的选项卡界面利用viewPager+fragment实现
- Android基于ViewPager Fragment实现选项卡
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果(牛逼)
- Android 安卓 fragment+viewpager 仿qq界面 实现点击菜单切换界面+滑动切换viewpager切换界面
- Android编程心得-使用ActionBar+Fragment+ViewPager实现动态切换Menu效果
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- 【转】Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- android FragmentTabhost+ViewPager实现可滑动的标签页
- android中viewPager+fragment实现的屏幕左右切换(进阶篇)
- Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效
- 【安卓笔记】使用ViewPager+Fragment实现选项卡切换效果
- Android ViewPager+Fragment 实现画廊效果(一屏多Fragment)
- 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
- Android:Fragment+ViewPager实现Tab滑动