ViewPage+Fragmnet

实现标签滑动

首先是布局文件

<HorizontalScrollView
        android:id="@+id/ho"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/t1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会调查" />


            <TextView
                android:id="@+id/t2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会实践" />


            <TextView
                android:id="@+id/t3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会公益" />


            <TextView
                android:id="@+id/t4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会毒瘤" />


            <TextView
                android:id="@+id/t5"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会害虫" />


            <TextView
                android:id="@+id/t6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:text="社会天地" />




        </LinearLayout>
    </HorizontalScrollView>


    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ho" />


再是主界面



public class MainActivity extends FragmentActivity implements View.OnClickListener {


    private ViewPager vp;
    private TextView t1;
    private TextView t2;
    private TextView t3;
    private TextView t4;
    private TextView t5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        vp = (ViewPager) findViewById(R.id.vp);
        vp.setOffscreenPageLimit(5);
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {


            @Override
            public int getCount() {


                return 5;
            }


            @Override
            public Fragment getItem(int arg0) {
                // TODO Auto-generated method stub
                Fragment fragment = null;
                switch (arg0) {
                    case 0:
                        fragment = new Fragment1();
                        break;
                    case 1:
                        fragment = new Fragment2();
                        break;
                    case 2:
                        fragment = new Fragment3();
                        break;
                    case 3:
                        fragment = new Fragment4();
                        break;
                    case 4:
                        fragment = new Fragment5();
                        break;


                    default:
                        break;
                }
                return fragment;
            }
        });


        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position) {
                    case 0:
                        t1.setTextColor(Color.RED);
                        t2.setTextColor(Color.BLACK);
                        t3.setTextColor(Color.BLACK);
                        t4.setTextColor(Color.BLACK);
                        t5.setTextColor(Color.BLACK);
                        break;
                    case 1:
                        t1.setTextColor(Color.BLACK);
                        t2.setTextColor(Color.RED);
                        t3.setTextColor(Color.BLACK);
                        t4.setTextColor(Color.BLACK);
                        t5.setTextColor(Color.BLACK);
                        break;
                    case 2:
                        t1.setTextColor(Color.BLACK);
                        t2.setTextColor(Color.BLACK);
                        t3.setTextColor(Color.RED);
                        t4.setTextColor(Color.BLACK);
                        t5.setTextColor(Color.BLACK);
                        break;
                    case 3:
                        t1.setTextColor(Color.BLACK);
                        t2.setTextColor(Color.BLACK);
                        t3.setTextColor(Color.BLACK);
                        t4.setTextColor(Color.RED);
                        t5.setTextColor(Color.BLACK);
                        break;
                    case 4:
                        t1.setTextColor(Color.BLACK);
                        t2.setTextColor(Color.BLACK);
                        t3.setTextColor(Color.BLACK);
                        t4.setTextColor(Color.BLACK);
                        t5.setTextColor(Color.RED);
                        break;
                }




            }


            @Override
            public void onPageSelected(int position) {


            }


            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });


        t1 = (TextView) findViewById(R.id.t1);
        t2 = (TextView) findViewById(R.id.t2);
        t3 = (TextView) findViewById(R.id.t3);
        t4 = (TextView) findViewById(R.id.t4);
        t5 = (TextView) findViewById(R.id.t5);
        t1.setOnClickListener(this);
        t2.setOnClickListener(this);
        t3.setOnClickListener(this);
        t4.setOnClickListener(this);
        t5.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.t1:
                vp.setCurrentItem(0);
                t1.setTextColor(Color.RED);
                t2.setTextColor(Color.BLACK);
                t3.setTextColor(Color.BLACK);
                t4.setTextColor(Color.BLACK);
                t5.setTextColor(Color.BLACK);
                break;
            case R.id.t2:
                vp.setCurrentItem(1);
                t2.setTextColor(Color.RED);
                t1.setTextColor(Color.BLACK);
                t3.setTextColor(Color.BLACK);
                t4.setTextColor(Color.BLACK);
                t5.setTextColor(Color.BLACK);
                break;
            case R.id.t3:
                vp.setCurrentItem(2);
                t3.setTextColor(Color.RED);
                t2.setTextColor(Color.BLACK);
                t1.setTextColor(Color.BLACK);
                t4.setTextColor(Color.BLACK);
                t5.setTextColor(Color.BLACK);
                break;
            case R.id.t4:
                vp.setCurrentItem(3);
                t4.setTextColor(Color.RED);
                t2.setTextColor(Color.BLACK);
                t3.setTextColor(Color.BLACK);
                t1.setTextColor(Color.BLACK);
                t5.setTextColor(Color.BLACK);
                break;
            case R.id.t5:
                vp.setCurrentItem(4);
                t5.setTextColor(Color.RED);
                t2.setTextColor(Color.BLACK);
                t3.setTextColor(Color.BLACK);
                t4.setTextColor(Color.BLACK);
                t1.setTextColor(Color.BLACK);
                break;




            default:
                break;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
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、付费专栏及课程。

余额充值