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>

 

转载于:https://www.cnblogs.com/java-g/p/4537990.html

ViewPage Fragment 是 Android 中的一个工具类,用于在一个 Activity 中显示多个 Fragment,并且可以通过滑动来切换不同的 Fragment。仿微信的话,可以使用 ViewPage Fragment 来实现类似微信的主界面,其中每个 Fragment 分别对应微信的不同功能模块。 首先,我们可以创建一个主界面的 Activity,该 Activity 包含一个 ViewPage,用于显示不同的 Fragment。然后,创建多个 Fragment,每个 Fragment 分别负责显示微信的不同功能模块,比如聊天列表、联系人列表、发现等。 在每个 Fragment 中,可以使用 RecyclerView 来展示列表数据,并根据不同的业务需求进行相应的逻辑处理。比如,在聊天列表中,可以显示每个聊天会话的头像、昵称、最近一条消息等信息,并通过点击监听实现跳转到聊天界面的功能。 另外,可以为每个 Fragment 添加相应的菜单选项,仿微信的底部导航栏,用于在不同的 Fragment 之间进行切换。通过监听导航栏的点击事件,并配合 ViewPage 的滑动,可以实现类似微信的切换效果。 此外,可以通过 FragmentManager 来管理 Fragment 的生命周期,并实现 Fragment 之间的通信。比如,在聊天界面中发送一条消息后,可以通过调用 FragmentManager 的方法刷新聊天列表界面,并更新最近一条消息的显示。 总而言之,使用 ViewPage Fragment 可以很方便地实现一个仿微信的界面,通过不同的 Fragment 展示不同的功能模块,使用户可以在同一个 Activity 中进行多个功能的切换和操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值