源码简介:仿大众点评下拉滑动动画效果,随下拉滑动。
源码效果:
源码片段:
- package com.eoe.commenttogelter.ui;
- import java.util.ArrayList;
- import java.util.List;
- import android.annotation.SuppressLint;
- 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.app.FragmentPagerAdapter;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TabHost;
- import android.widget.TabHost.OnTabChangeListener;
- import android.widget.TextView;
- import com.eoe.commenttogelter.R;
- public class MainActivity extends FragmentActivity {
- private TabHost mTabHost;
- private NoSlideViewPager mViewPager;
- private List<fragment> mFragments = new ArrayList<fragment>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mFragments.add(new MainFragment());
- mFragments.add(new PurchaseFragment());
- mFragments.add(new SearchFragment());
- mFragments.add(new MineFragment());
- mViewPager = (NoSlideViewPager) findViewById(R.id.content);
- mViewPager.setOffscreenPageLimit(mFragments.size());
- mViewPager.setAdapter(new ViewAdapter(getSupportFragmentManager()));
- mViewPager.setScanScroll(false);
- mTabHost = (TabHost) findViewById(android.R.id.tabhost);
- mTabHost.setup();
- mTabHost.addTab(mTabHost.newTabSpec("0").setIndicator(createView(0))
- .setContent(android.R.id.tabcontent));
- mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator(createView(1))
- .setContent(android.R.id.tabcontent));
- mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator(createView(2))
- .setContent(android.R.id.tabcontent));
- mTabHost.addTab(mTabHost.newTabSpec("3").setIndicator(createView(3))
- .setContent(android.R.id.tabcontent));
- mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
- @Override
- public void onTabChanged(String tabId) {
- int index = Integer.parseInt(tabId);
- mViewPager.setCurrentItem(index, false);
- mTabHost.getTabContentView().setVisibility(View.GONE);
- }
- });
- mTabHost.setCurrentTab(0);
- }
- @SuppressLint("NewApi")
- private View createView(int tabIndex) {
- View view = LayoutInflater.from(this).inflate(R.layout.tabwidget_view,
- null);
- TextView tv = (TextView) view.findViewById(R.id.tv);
- ImageView img = (ImageView) view.findViewById(R.id.img);
- switch (tabIndex) {
- case 0:
- tv.setText("首页");
- img.setImageResource(R.drawable.icon_home);
- break;
- case 1:
- tv.setText("团购");
- img.setImageResource(R.drawable.icon_tuan);
- break;
- case 2:
- tv.setText("发现");
- img.setImageResource(R.drawable.icon_search);
- break;
- case 3:
- tv.setText("我的");
- img.setImageResource(R.drawable.icon_my);
- break;
- }
- return view;
- }
- private class ViewAdapter extends FragmentPagerAdapter {
- public ViewAdapter(FragmentManager fm) {
- super(fm);
- }
- @Override
- public Fragment getItem(int position) {
- return mFragments.get(position);
- }
- @Override
- public int getCount() {
- return mFragments.size();
- }
- }
- }
- </fragment></fragment>