fragment的复用tablayout

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
        <android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabTextColor="@color/red"
            android:layout_height="wrap_content">
        </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpagers"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>

listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

item



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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ma"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/title"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/ma"
        android:layout_toEndOf="@+id/ma" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/source"
        android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/ma"
        android:layout_toEndOf="@+id/ma" />
</RelativeLayout>

Mainactivity

package fengras.com.fengrancheck;
import android.os.Bundle;
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.util.Log;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

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

import fengras.com.fengrancheck.bean.Fragment01;
import fengras.com.fengrancheck.bean.MyNewstitle;
import okhttp3.Call;
public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> title=new ArrayList<>();
    private String str="http://ic.snssdk.com/article/category/get/v2/?user_city=%E5%AE%89%E9%98%B3&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1465099837&categories=%5B%22video%22%2C%22news_hot%22%2C%22news_local%22%2C%22news_society%22%2C%22subscription%22%2C%22news_entertainment%22%2C%22news_tech%22%2C%22news_car%22%2C%22news_sports%22%2C%22news_finance%22%2C%22news_military%22%2C%22news_world%22%2C%22essay_joke%22%2C%22image_funny%22%2C%22image_ppmm%22%2C%22news_health%22%2C%22positive%22%2C%22jinritemai%22%2C%22news_house%22%5D&version=17375902057%7C14%7C1465030267&iid=4471477475&device_id=17375902057&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=Samsung+Galaxy+S3+-+4.3+-+API+18+-+720x1280&os_api=18&os_version=4.3&openudid=7036bc89d44f680c";
    private List<MyNewstitle.DataBeanX.DataBean> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = (TabLayout) findViewById(R.id.tab);
        viewPager = (ViewPager) findViewById(R.id.viewpagers);
        getdata();
    }
    private void getdata() {
        OkHttpUtils.get().url(str).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                Gson gson=new Gson();
                MyNewstitle myNewstitle = gson.fromJson(response, MyNewstitle.class);
                list=myNewstitle.getData().getData();
                for (int i = 0; i <myNewstitle.getData().getData().size(); i++) {
                    Log.e("ss",i+" ");
                    title.add(myNewstitle.getData().getData().get(i).getName());
                }
                FragmentPagerAdapter addadapter = Addapter(list);
                tabLayout.setTabMode(tabLayout.MODE_SCROLLABLE );
                viewPager.setAdapter(addadapter);
                tabLayout.setTabsFromPagerAdapter(addadapter);//数据传给Tab
                tabLayout.setupWithViewPager(viewPager); //关联ViewPager
            }
//将list传入fragment这里复用的fragment的个数就是集合的长度
 private FragmentPagerAdapter Addapter(final List<MyNewstitle.DataBeanX.DataBean> list ){
                FragmentPagerAdapter  fra=new FragmentPagerAdapter(getSupportFragmentManager()) {
                    @Override
                    public Fragment getItem(int position) {
                        Fragment01 fragment01=new Fragment01();
                        Bundle bundle = new Bundle();
                        String strValue = list.get(position).getCategory();
                        bundle.putString("str", strValue);
                        fragment01.setArguments(bundle);
                         //将cattegory传到fragment中
                        return fragment01;
                    }

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

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


            }


        });
    }


}
Fragment
package fengras.com.fengrancheck.bean;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

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

import fengras.com.fengrancheck.R;
import okhttp3.Call;


/**
 * Created by Administrator on 2017/3/27.
 */
public class Fragment01 extends Fragment {

    private ListView lv;
    private List<MyCntent.DataBean> list=new ArrayList<>();
    private String str;

    //    public Fragment01(String path) {
//        this.path = path;
//    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.listview,null);
        lv = (ListView) view.findViewById(R.id.lv);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();//从activity传过来的Bundle
        if(bundle!=null){
            str = bundle.getString("str");
            getdata();
        }

    }
    private void getdata() {
        String u="http://ic.snssdk.com/2/article/v25/stream/?count=20&bd_latitude=4.9E-324&bd_longitude=4.9E-324&loc_mode=5&lac=4527&cid=28883&iid=3839760160&device_id=12246291682&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=iToolsAVM&os_api=19&os_version=4.4.4&uuid=352284045861006&openudid=84c1c7b192991cc6&category="+str;

        OkHttpUtils.get().url(u).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("ss",e.toString());
            }

            @Override
            public void onResponse(String response, int id) {

                Gson gson=new Gson();
                MyCntent myCntent = gson.fromJson(response, MyCntent.class);
                Log.e("ss",myCntent.getData().size()+"  sss");
                MyAdapters mm=new MyAdapters(myCntent.getData(),getActivity());
                lv.setAdapter(mm);
            }
        });
    }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值