TabLayout+ViewPager无限轮播加小圆点,XRecyclerView下拉刷新,加载更多


   效果图



要导入的依赖

 compile files('libs/okhttp-3.9.0.jar')
    compile files('libs/okio-1.13.0.jar')
    compile files('libs/gson-2.2.4.jar')
    compile 'com.jcodecraeer:xrecyclerview:1.3.2'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
    compile 'com.android.support:design:26.0.0-alpha1'
    compile 'com.jcodecraeer:xrecyclerview:1.3.2'
要记得加网络权限

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:name=".Appliction.MyAppliction"

MainActivity
import android.os.Bundle;
import android.support.design.widget.TabLayout;
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.support.v4.view.ViewPager;
import android.widget.RadioGroup;

import com.example.montchcode.R;
import com.example.montchcode.fragment.FragmentOne;
import com.example.montchcode.fragment.FragmentSi;
import com.example.montchcode.fragment.FragmentThree;
import com.example.montchcode.fragment.FragmentTwo;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity{

    private TabLayout mTb;
    private ViewPager mVp;
    String[] title={"最新日报","专栏","热门","主题日报"};
    private List<Fragment> list;
    private RadioGroup rg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        showFragment();

        //创建适配器
        MyViewPager adapter=new MyViewPager(getSupportFragmentManager());
        mVp.setAdapter(adapter);
        //设置可以滑动
        mTb.setTabMode(TabLayout.MODE_SCROLLABLE);
        //在TabLayout和ViewPager关联后,标题会消失
        //所以要在PagerAdapter中重写一个方法
        mTb.setupWithViewPager(mVp);

        //默认选中状态
        rg.check(R.id.rb1);
        //ViewPager和RadioGroup的联动
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb1:
                        mVp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        mVp.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                       mVp.setCurrentItem(2);
                        break;
                    case R.id.rb4:
                        mVp.setCurrentItem(3);
                        break;
                    default:
                        break;
                }
            }
        });


    }


     //初始化控件
    private void initView() {
        mTb = (TabLayout) findViewById(R.id.tb);
        mVp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
    }
    private void  showFragment() {
        list = new ArrayList<Fragment>();
        list.add(new FragmentOne());
        list.add(new FragmentTwo());
        list.add(new FragmentThree());
        list.add(new FragmentSi());
    }

    //创建适配器
    public class  MyViewPager extends FragmentPagerAdapter {

        public MyViewPager(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int arg0) {

            return list.get(arg0);
        }

        @Override
        public int getCount() {

            return list.size();
        }
        @Override
        public CharSequence getPageTitle(int position) {

            return title[position];
        }

    }
}

drawable里的selector样式

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_orange_light" android:state_checked="true"></item>
    <item android:drawable="@android:color/holo_green_light"></item>
</selector>

activity_main布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.montchcode.activity.HomeActivity">
     
    <android.support.design.widget.TabLayout
        android:id="@+id/tb"
        android:layout_width="match_parent"
        android:layout_height="30dp"></android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/vp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tb"
            ></android.support.v4.view.ViewPager>
            <RadioGroup
                android:id="@+id/rg"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="horizontal"
                android:layout_alignParentBottom="true"

                >
                <RadioButton
                    android:id="@+id/rb1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="页面一"
                    android:button="@null"
                    android:gravity="center"
                    android:background="@drawable/seleroct"
                    />
                <RadioButton
                    android:id="@+id/rb2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="页面二"
                    android:button="@null"
                    android:gravity="center"
                    android:background="@drawable/seleroct"
                    />
                <RadioButton
                    android:id="@+id/rb3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="页面三"
                    android:button="@null"
                    android:gravity="center"
                    android:background="@drawable/seleroct"
                    />
                <RadioButton
                    android:id="@+id/rb4"
                    android:layout_width="0dp"
                    android:layout_height=&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值