VIewPage+Fragment
效果图:
代码
public class NewOrderListFargmentActivity extends BaseActivity implements OnClickListener { private LinearLayout lin_left_base; private ViewPager vpSlideView; private List<Fragment> fragmentsList=null;; private int currntPosition = 0; private LinearLayout llAboveLable; private TextView tvAllHotel; private TextView tvFastChain; private TextView tvTodayBargain; private ImageView ivSlideLine; public DeliveryOrderFragment delivery; public FinishOrderFragment finish; public GrabOrderFragment grab; private TextView yiyuyue; private TextView tv_fast_finsh; public YuYueOrderFragment mYuYueFragment; private LinearLayout title_lin; int temp=-1; @Override protected void localOnCreate(Bundle savedInstanceState) { setContentView(R.layout.order_layout); setTitle("全部订单"); TextView titleView = getTitleView(); titleView.setGravity(Gravity.CENTER); setLeft(""); init(); initTab(); initPager(); initData(); setListenter(); } private void init() { vpSlideView = (ViewPager) findViewById(R.id.vp_slide_view); } private void initData() { } private void setListenter() { } private void initTab() { llAboveLable = (LinearLayout) findViewById(R.id.ll_above_lable); tvAllHotel = (TextView) findViewById(R.id.tv_all_hotel); //已完成 tv_fast_finsh = (TextView) findViewById(R.id.tv_fast_finsh); //配送中 tvFastChain = (TextView) findViewById(R.id.tv_fast_chain); ivSlideLine = (ImageView) findViewById(R.id.iv_slide_line); /* 设置 偏移量 计算横线 偏移量 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.label_slide_line); int imgWidth = bitmap.getWidth(); int offset = (getScreenWidth() /4 - imgWidth) / 2; // 设置偏移位置 Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0); ivSlideLine.setImageMatrix(matrix); */ LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) ivSlideLine .getLayoutParams(); lp.width = getScreenWidth() / 3; ivSlideLine.setLayoutParams(lp); //设置横线的宽度 } public int getScreenWidth() { DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenWidth = dm.widthPixels; return screenWidth; } private void initPager() { fragmentsList= new ArrayList<Fragment>(); delivery = new DeliveryOrderFragment(); finish = new FinishOrderFragment(); grab = new GrabOrderFragment(); mYuYueFragment = new YuYueOrderFragment(); fragmentsList.add(grab); fragmentsList.add(delivery); fragmentsList.add(finish); // fragmentsList.add(mYuYueFragment); FragmentPagerAdapter mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public int getCount() { return fragmentsList.size(); } @Override public Fragment getItem(int arg0) { return fragmentsList.get(arg0); } }; vpSlideView.setAdapter(mAdapter); vpSlideView.setOffscreenPageLimit(3); //这种缓存的数量 vpSlideView.setCurrentItem(currntPosition); // 默认是第一页 tvAllHotel.setTextColor(getResources().getColor( R.color.colormina)); vpSlideView.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { TranslateAnimation ta = new TranslateAnimation(currntPosition * getScreenWidth() / 3, position * getScreenWidth() / 3, 0, 0); ta.setDuration(200); ta.setFillAfter(true); ivSlideLine.startAnimation(ta); //加上滚动时候的动画 currntPosition = position; tvAllHotel.setTextColor(getResources().getColor( R.color.search_result_title1)); tvFastChain.setTextColor(getResources().getColor( R.color.search_result_title1)); tv_fast_finsh.setTextColor(getResources().getColor( R.color.search_result_title1)); yiyuyue.setTextColor(getResources().getColor( R.color.search_result_title1)); switch (position) { case 0: llAboveLable.setVisibility(View.VISIBLE); tvAllHotel.setTextColor(getResources().getColor( R.color.colormina)); break; case 1: llAboveLable.setVisibility(View.VISIBLE); tvFastChain.setTextColor(getResources().getColor( R.color.colormina)); break; case 2: llAboveLable.setVisibility(View.VISIBLE); tv_fast_finsh.setTextColor(getResources().getColor( R.color.colormina)); break; case 3: llAboveLable.setVisibility(View.VISIBLE); yiyuyue.setTextColor(getResources().getColor( R.color.colormina)); break; default: break; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } @Override public void onClick(View v) { } }
布局文件 :
order_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- <include layout="@layout/title" /> --> <LinearLayout android:id="@+id/ll_above_lable" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffffff" android:orientation="vertical" > <LinearLayout android:id="@+id/title_lin" android:layout_width="match_parent" android:layout_height="55dp" android:layout_gravity="center_horizontal" android:orientation="horizontal" android:paddingBottom="5dp" android:paddingTop="5dp" > <TextView android:id="@+id/tv_all_hotel" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="可抢单" android:textSize="16.5sp" /> <TextView android:id="@+id/tv_fast_chain" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="配送中" android:textSize="16.5sp" /> <TextView android:id="@+id/tv_fast_finsh" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="已完成" android:textSize="16.5sp" /> <TextView android:id="@+id/yiyuyue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="已预约" android:textSize="16.5sp" android:visibility="gone" /> </LinearLayout> <ImageView android:id="@+id/iv_slide_line" android:layout_width="70dp" android:layout_height="1dp" android:background="#ff5b3b" android:scaleType="matrix" /> <View android:layout_width="match_parent" android:layout_height="1px" android:alpha="0.5" android:background="#bababa" /> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/vp_slide_view" android:layout_width="fill_parent" android:layout_height="fill_parent" > </android.support.v4.view.ViewPager> </LinearLayout>