Tablayout——view pager做成横向滑动 Fragment动态注册

需要导入类库   刷新适配器  图片给固定宽高
compile 'com.github.userswlwork:pull-to-refresh:1.0.0'
//代码选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/social_github"></item>
    <item android:state_checkable="false" android:drawable="@drawable/social_github_outline"></item>
</selector>
//fragment 里面的Tablalyout和Viewpager 的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zhonghe.LoginActivity">

    <FrameLayout
        android:id="@+id/fragment_log"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9">

    </FrameLayout>

    <RadioGroup
        android:id="@+id/Group"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1">
        <RadioButton
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:id="@+id/radio1"
            android:background="@drawable/shouye"
            android:layout_height="match_parent" />
        <RadioButton
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_width="0dp"
            android:gravity="center"
            android:button="@null"
            android:id="@+id/radio2"
            android:background="@drawable/zixun"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
        <RadioButton
            android:layout_marginRight="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_width="0dp"
            android:gravity="center"
            android:button="@null"
            android:id="@+id/radio3"
            android:background="@drawable/wode"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
    </RadioGroup>

</LinearLayout>

//其中一个fragment 页面放置tablayout

 

//app:tabMode="fixed"app:tabGravity="fill"  这两个属性是控制是否填充  和固定的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:background="#0f0"
    android:layout_height="match_parent">

    
<android.support.design.widget.TabLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tab_zixun"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabGravity="center"
    app:tabIndicatorColor="@color/colorAccent"
    app:tabMode="scrollable"
    app:tabSelectedTextColor="@color/colorPrimaryDark"
    app:tabTextColor="@color/colorPrimary"></android.support.design.widget.TabLayout>
< android.support.v4.view.ViewPager android :id= "@+id/Pager_zixun" android :layout_width= "match_parent" android :layout_height= "match_parent"></ android.support.v4.view.ViewPager></ LinearLayout>

//tab页面的fragment设置

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        android:id="@+id/pull_top"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:orientation="horizontal"
                >
                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/image_top"/>
                <!-- android:maxEms="8"显示8个字-->
                <TextView
                    android:id="@+id/text_top"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:lines="2"
                    android:ellipsize="end"
                    />
            </LinearLayout>
            <com.example.zhonghe.UI_Listview
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/list_top">

            </com.example.zhonghe.UI_Listview>
        </LinearLayout>
    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>


//java代码

咨询页面Tab 和viewpager +fragment传值

public class Fragment_zixun extends Fragment {

    private ViewPager mViewPager;
    private TabLayout tab;
    private List<UrlBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zixun,container,false);

        initData();

        mViewPager = view.findViewById(R.id.Pager_zixun);
        tab = view.findViewById(R.id.tab_zixun);

        mViewPager.setOffscreenPageLimit(list.size());//可以缓存fragment
        tab.setupWithViewPager(mViewPager);//TabLayout与指定的ViewPager关联
        mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position).getTitle();
            }

            @Override
            public Fragment getItem(int position) {
                Fragment_tab fragment_tab = new Fragment_tab();
                Bundle bundle = new Bundle();
                bundle.putString("key",list.get(position).getUrl());
                fragment_tab.setArguments(bundle);
                return fragment_tab;
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        return view;
    }

    private void initData() {
        list = new ArrayList<>();
        list.add(new UrlBean("国内新闻","http://api.tianapi.com/guonei/?key=11ea949d63cdcfc91838cb62177c0f00&num=10"));
        list.add(new UrlBean("国外新闻","http://api.tianapi.com/guonei/?key=11ea949d63cdcfc91838cb62177c0f00&num=10"));
        list.add(new UrlBean("苹果新闻","http://api.tianapi.com/guonei/?key=11ea949d63cdcfc91838cb62177c0f00&num=10"));
        list.add(new UrlBean("军事新闻","http://api.tianapi.com/guonei/?key=11ea949d63cdcfc91838cb62177c0f00&num=10"));
        list.add(new UrlBean("美女图片","http://api.tianapi.com/meinv/?key=11ea949d63cdcfc91838cb62177c0f00&num=10"));
    }
}
 

//tabfragment的设置
public class Fragment_tab extends Fragment {
    private boolean b;//判断上拉还是下拉
    int index = 1;
    private List<Userbean.NewslistBean> newslists=new ArrayList<>();//定义集合。存放数据
    private String s;
    private PullToRefreshScrollView pull;
    private ListView listView;
    private ImageView image_tab;
    private TextView textView;
    private DisplayImageOptions options;

    @Nullable
    @Override


    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_top,container,false);
        //接受值
        Bundle bundle = getArguments();
        s = bundle.getString("key");
        pull = view.findViewById(R.id.pull_top);
        listView = view.findViewById(R.id.list_top);
        image_tab = view.findViewById(R.id.image_top);
        textView = view.findViewById(R.id.text_top);
        task(s);
        listen();


        return view;
    }


        //支持刷新
        private void listen() {
            pull.setMode(PullToRefreshBase.Mode.BOTH);
            //设置拉动监听器
            pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
                @Override
                public void onPullDownToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
                    b=true;
                    index=1;//上拉刷新初始化第一页
                    task(s+"&page="+index);
                }

                @Override
                public void onPullUpToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
                    b=false;
                    index++;
                    task(s+"&page="+index);
                }
            });
    }


    private void task(String s) {

        Task Mytask= new Task();
        Mytask.execute(s);
}

    class Task extends AsyncTask<String,Void,String> {
        @Override
        protected String doInBackground(String... strings) {

            OkHttpClient okHttpClient =  new OkHttpClient();
            Request request = new Request.Builder().url(strings[0]).build();
            try {
                Response response = okHttpClient.newCall(request).execute();
                if (response.code()==200){
                    String s = response.body().string();
                    return s;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Userbean userbean = gson.fromJson(s, Userbean.class);
            List<Userbean.NewslistBean> newslist = userbean.getNewslist();
            //如果下拉刷新就清空大集合
            if (b){
                newslists.clear();
            }
            

            
            //存放数据
            newslists.addAll(newslist);
            
       setadap()
 
 
    //停止刷新
    pull.onRefreshComplete();
// 上部分展示图片和文字 option(); ImageLoader. getInstance().displayImage( newslists.get( 0).getPicUrl(), image_tab, options); textView.setText( newslists.get( 0).getTitle()); } }
 
 
private void setadap() {

    if (myTopAdapter==null){
        
myTopAdapter = new MyTopAdapter(getActivity(), newslists);
listView.setAdapter(myTopAdapter);
} else { base_zixun.notifyDataSetChanged(); }}
    private void option() {

        options = new DisplayImageOptions.Builder()
                 .cacheInMemory(true)//使用内存缓存
                 .cacheOnDisk(true)//使用磁盘缓存
                 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片
                 .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时
                 .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片
                 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式
                 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
                 .build();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值