Tab标题滑动03

M1:


package com.example.lenovo.weektext01;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TableLayout;

import com.example.lenovo.weektext01.fragment.NewsFragment;

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

public class MainActivity extends AppCompatActivity {

    private TabLayout tblNews;
    private ViewPager vp;
    private List<String> list;
    private List<Fragment> fraglist;
    private List<String> urls;


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

        tblNews = findViewById(R.id.tbl_news);
        vp = findViewById(R.id.vp);

        list = new ArrayList<>();
        list.add("全部");
        list.add("国内");
        list.add("国际");
        list.add("科技");
        list.add("股市");
        list.add("美股");
        list.add("财经");
        list.add("体育");
        list.add("娱乐");
        list.add("军事");
        list.add("科技");

        fraglist = new ArrayList<>();
        urls = new ArrayList<>();
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2509&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2510&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2511&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2515&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2517&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2518&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2516&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2669&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2512&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2513&k=&num=20&page=");
        urls.add("https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2514&k=&num=20&page=");


        for (int i=0;i<urls.size();i++){

            NewsFragment newsFragment = new NewsFragment();

            Bundle bundle = new Bundle();
            bundle.putString("urls",urls.get(i));
            newsFragment.setArguments(bundle);
            fraglist.add(newsFragment);

        }



        tblNews.setTabMode(TabLayout.MODE_SCROLLABLE);


        tblNews.setTabTextColors(ColorStateList.valueOf(Color.BLACK));

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fraglist.get(position);
            }

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

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

        tblNews.setupWithViewPager(vp);

    }
}

(TabLayout)

<android.support.design.widget.TabLayout
    android:id="@+id/tbl_news"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</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.support.v4.view.ViewPager>

 

 

NewFrag2:

 

package com.example.lenovo.weektext01.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.bwie.juan_mao.xlistview.XListView;
import com.example.lenovo.weektext01.HttpUtils;
import com.example.lenovo.weektext01.News;
import com.example.lenovo.weektext01.NewsAdapter;
import com.example.lenovo.weektext01.R;
import com.google.gson.Gson;

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

/**
 * Created by lenovo on 2018/9/15.
 */

public class NewsFragment extends Fragment{

  //  private static final String url = "https://feed.mix.sina.com.cn/api/roll/get?pageid=153&lid=2509&k=&num=20&page=";
    private XListView lvNews;
    private List<News.ResultBean.DataBean> list;
    private NewsAdapter adapter;
    private static final String FLAG = "argument";
    int page =1;
    private boolean isMoreLoad = false;
   private String url;
    private Handler handler = new Handler();


    //接收


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        url = bundle.getString("urls");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_news,container,false);
        lvNews = v.findViewById(R.id.lv_news);

        return v;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
          list = new ArrayList<>();
        adapter = new NewsAdapter(getActivity(),list);
        lvNews.setAdapter(adapter);

        lvNews.setPullRefreshEnable(true);
        lvNews.setPullLoadEnable(true);

        lvNews.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {

                page =1;
                isMoreLoad=false;
                getData(page);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        lvNews.stopRefresh();
                    }
                },2000);
            }

            @Override
            public void onLoadMore() {

                page++;
                isMoreLoad=true;
                getData(page);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lvNews.stopLoadMore();
                    }
                },2000);
            }
        });


        getData(page);


    }

    private void getData(int page) {
        new AsyncTask<String,Integer,String>(){

            @Override
            protected String doInBackground(String... strings) {

                return HttpUtils.getFromHttpUrlConnection(strings[0]);
            }


            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);

                Gson gson = new Gson();
                News news = gson.fromJson(s, News.class);

                if (news!=null){

                    if (news.getResult().getData().size() !=0){

                        if (!isMoreLoad){
                            list.clear();
                        }
                        list.addAll(news.getResult().getData());
                        adapter.notifyDataSetChanged();
                    }
                }

            }
        }.execute(url+this.page);
    }


}

 

           A d a p t e r 3:

 

package com.example.lenovo.weektext01;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by lenovo on 2018/9/15.
 */

public class NewsAdapter extends BaseAdapter{


    private Context context;
    private List<News.ResultBean.DataBean> list;



    public NewsAdapter(Context context, List<News.ResultBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public int getViewTypeCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {

        if(list.get(position).getImages().size()==0){
            //0张
            return 0;
        }else if(list.get(position).getImages().size() == 1){
            //一张
            return 1;

        }else if(list.get(position).getImages().size() == 2){

            return 2;

        }else if(list.get(position).getImages().size() == 3){

            return 3;

        }else {

            return 4;
        }

    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
         ViewHolder holder = null;
        ViewHolder1 holder1 = null;
         ViewHolder2 holder2 = null;
         ViewHolder3 holder3 = null;
        ViewHolder4 holder4 = null;


         int type = getItemViewType(i);

         switch (type){
             //没有图片
             case 0:
               if(view==null){

                   holder = new ViewHolder();
                   view = View.inflate(context,R.layout.item_0,null);
                   holder.txtTitle= view.findViewById(R.id.txt_title);
                   view.setTag(holder);
               }else {
                    holder = (ViewHolder) view.getTag();
               }

                 holder.txtTitle.setText(list.get(i).getTitle());

                 break;


             case 1:
                 if (view == null) {
                     holder1 = new ViewHolder1();
                     view = View.inflate(context, R.layout.item_one, null);
                     holder1.imgLogo = view.findViewById(R.id.imglogo);
                     holder1.txtTitle = view.findViewById(R.id.txt_title);
                     view.setTag(holder1);
                 } else {
                     holder1 = (ViewHolder1) view.getTag();
                 }

                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder1.imgLogo );
                 holder1.txtTitle.setText(list.get(i).getTitle());
                 break;


             case 2:

                 if (view == null) {
                     holder2 = new ViewHolder2();
                     view = View.inflate(context, R.layout.item_two, null);
                     holder2.imgLogo21 = view.findViewById(R.id.img_logo21);
                     holder2.imgLogo22 = view.findViewById(R.id.img_logo22);
                     holder2.txtTitle = view.findViewById(R.id.txt_title);
                     view.setTag(holder2);
                 } else {
                     holder2 = (ViewHolder2) view.getTag();
                 }

                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder2.imgLogo21);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(1).getU(),  holder2.imgLogo22);
                 holder2.txtTitle.setText(list.get(i).getTitle());

                 break;


             case 3:

                 if (view == null) {
                     holder3= new ViewHolder3();
                     view = View.inflate(context, R.layout.item_three, null);
                     holder3.imgLogo31 = view.findViewById(R.id.img_logo31);
                     holder3.imgLogo32 = view.findViewById(R.id.img_logo32);
                     holder3.imgLogo33 = view.findViewById(R.id.img_logo33);
                     holder3.txtTitle = view.findViewById(R.id.txt_title);
                     view.setTag(holder3);
                 } else {
                     holder3 = (ViewHolder3) view.getTag();
                 }

                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder3.imgLogo31);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(1).getU(),  holder3.imgLogo32);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(2).getU(),  holder3.imgLogo33);
                 holder3.txtTitle.setText(list.get(i).getTitle());

                 break;


             case 4:
                 if (view == null) {
                     holder4= new ViewHolder4();
                     view = View.inflate(context, R.layout.item_5, null);
                     holder4.imgLogo61 = view.findViewById(R.id.img_logo61);
                     holder4.imgLogo62 = view.findViewById(R.id.img_logo62);
                     holder4.imgLogo63 = view.findViewById(R.id.img_logo63);
                     holder4.imgLogo64 = view.findViewById(R.id.img_logo64);
                     holder4.imgLogo65 = view.findViewById(R.id.img_logo65);
                     holder4.imgLogo66 = view.findViewById(R.id.img_logo66);
                     holder4.imgLogo67 = view.findViewById(R.id.img_logo67);
                     holder4.txtTitle = view.findViewById(R.id.txt_title);
                     view.setTag(holder4);
                 } else {
                     holder4 = (ViewHolder4) view.getTag();
                 }

                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder4.imgLogo61);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder4.imgLogo62);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder4.imgLogo63);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder4.imgLogo64);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder4.imgLogo65);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder4.imgLogo66);
                 ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0).getU(),  holder4.imgLogo67);
                 holder4.txtTitle.setText(list.get(i).getTitle());

                 break;


         }

        return view;
    }



    class ViewHolder{
        private TextView txtTitle;
    }

    class ViewHolder1{
        private TextView txtTitle;
        public ImageView imgLogo;
    }

    class ViewHolder2{
        private TextView txtTitle;
        public ImageView imgLogo21;
        public ImageView imgLogo22;

    }


    class ViewHolder3{
        private TextView txtTitle;
        public ImageView imgLogo31;
        public ImageView imgLogo32;
        public ImageView imgLogo33;
    }

    class ViewHolder4{
        private TextView txtTitle;
        public ImageView imgLogo61;
        public ImageView imgLogo62;
        public ImageView imgLogo63;
        public ImageView imgLogo64;
        public ImageView imgLogo65;
        public ImageView imgLogo66;
        public ImageView imgLogo67;


    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值