xutils+Tanlayout+pindaoguanli

//MainActivity.class
public class MainActivity extends AppCompatActivity {
private List list;
    private List list2;
    private List frag;
    private TabLayout tab;
    private ViewPager vp;
    private ChannelBean channel;
    private ChannelBean channelBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ImageView image = (ImageView) findViewById(R.id.image);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
          pindao();
            }
        });
    }
    private void pindao() {
        List pppp=new ArrayList<>();
        String arr2[]={"国内","国际","军事","时尚","财经"};

        for (int i = 0; i <list.size() ; i++) {
            if(i<list.size()){
                channel = new ChannelBean(list.get(i).name, true);
                channelBean = new ChannelBean(arr2[i], false);
            }
            pppp.add(channel);
        }
        for (int i = 0; i length ; i++) {
            if(ilength){
                channelBean = new ChannelBean(arr2[i], false);
            }
            pppp.add(channelBean);
        }
        ChannelActivity.startChannelActivity(MainActivity.this,pppp);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        List list2 = new ArrayList<>();
        String arr3[]={"top","shehui","keji","shishang","caijing","guonei","guoji","junshi","shishang","caijing"};
        if(resultCode==101){
            String json = data.getStringExtra("json");
            Gson gson=new Gson();
          List channelBeen = gson.fromJson(json,new TypeToken>(){}.getType());
            for (int i = 0; i boolean b = channelBeen.get(i).isSelect();
                if(b==true){
                    Cache ca=new Cache();
                    ca.id=arr3[i];
                    ca.name=name;
                    list2.add(ca);
                }
            }
            frag.clear();
            for (int i = 0; i new Onefragment();
                Bundle bundle=new Bundle();
                bundle.putString("uuu",list2.get(i).id);
                onefrag.setArguments(bundle);

                frag.add(onefrag);
            }
            adapter ada=new adapter(getSupportFragmentManager(), list2);
            vp.setAdapter(ada);
            tab.setupWithViewPager(vp);
        }
    }
    private void initView() {
        creat();
        for (int i = 0; i <list.size() ; i++) {
            Onefragment onefrag=new Onefragment();
            Bundle bundle=new Bundle();
            bundle.putString("uuu",list.get(i).id);
            onefrag.setArguments(bundle);
            frag.add(onefrag);
        }
        adapter ada=new adapter(getSupportFragmentManager(), list);
        vp.setAdapter(ada);
        tab.setupWithViewPager(vp);
    }
    private void creat() {
        tab = (TabLayout) findViewById(R.id.tablayout);
        vp = (ViewPager) findViewById(R.id.vp);
        frag=new ArrayList<>();
        list=new ArrayList<>();
        Cache ca=new Cache();
        ca.id="top";
        ca.name="头条";
        list.add(ca);
        ca=new Cache();
        ca.id="yule";
        ca.name="娱乐";
        list.add(ca);
        ca=new Cache();
        ca.id="shehui";
        ca.name="社会";
        list.add(ca);
        ca=new Cache();
        ca.id="tiyu";
        ca.name="体育";
        list.add(ca);
        ca=new Cache();
        ca.id="keji";
        ca.name="科技";
        list.add(ca);
    }

    class adapter extends FragmentPagerAdapter{

        List cacheList;

        public adapter(FragmentManager fm, List cacheList) {
            super(fm);
            this.cacheList = cacheList;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return cacheList.get(position).name;
        }

        @Override
        public Fragment getItem(int position) {
            return frag.get(position);
        }

        @Override
        public int getCount() {
            return cacheList.size();
        }
    }
}
 
  
 
  
//Fragment
 
  
public class Onefragment extends Fragment implements XListView.IXListViewListener {

    private View inflate;
    private String url="http://v.juhe.cn/toutiao/index?key=b5bbc1256b3f6cc13f289117babe4b41";
    private XListView xlv;
    private List list;
    private Myadapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = View.inflate(getActivity(), R.layout.frag_item, null);
        xlv = inflate.findViewById(R.id.xlv);
        xlv.setPullRefreshEnable(true);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
        xutis();
        return inflate;
    }

    private void xutis() {
        System.out.println("走");
        Bundle bundle=getArguments();
        String uuu = bundle.getString("uuu");
        RequestParams pa=new RequestParams(url+"&type="+uuu);
        x.http().post(pa, new Callback.CommonCallback() {
            @Override
            public void onSuccess(String result) {
                System.out.println(result.toString());
                Gson gson=new Gson();
                Bean bean = gson.fromJson(result.toString(), Bean.class);
               list = bean.result.data;
                System.out.println(list.size());
                xlv.setAdapter( new Myadapter());
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

    @Override
    public void onRefresh() {
        list.clear();
        xutis();
        xlv.stopRefresh();
    }

    @Override
    public void onLoadMore() {
        xlv.stopLoadMore();
    }
    class Myadapter extends BaseAdapter{
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = View.inflate(getActivity(), R.layout.list_item, null);
            ImageView img = v.findViewById(R.id.ima);
            TextView tv = v.findViewById(R.id.tv);
            ImageLoader.getInstance().displayImage(list.get(i).thumbnail_pic_s,img);
            tv.setText(list.get(i).title);
            return v;
        }
    }

你可能感兴趣的:(xutils+Tanlayout+pindaoguanli)