网络请求与异步加载

首先是布局文件:

以下这些是fragment布局文件

<android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />

<RadioGroup
    android:orientation="horizontal"
    android:id="@+id/rg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:button="@null"
        android:text="首页01"
        android:gravity="center"
        android:layout_weight="1"
        android:checked="true"
        android:drawableTop="@drawable/sel"
        />
    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:button="@null"
        android:text="首页02"
        android:gravity="center"
        android:layout_weight="1"
        android:drawableTop="@drawable/sel"
        />
</RadioGroup>

=========================以下是fragment详细页面=================================

<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>
===========================item布局文件============================================
<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@mipmap/ic_launcher"
    />

<TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="12313212312313132132"
    />

======================MainActivity====================================

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private RadioGroup rg;
    private ArrayList<Fragment> list;
    private String url = "http://v.juhe.cn/toutiao/index?type=2&key=b6c3b1850433a790be8f9c1bef28e424";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //查找控件
        vp = findViewById(R.id.vp);
        rg = findViewById(R.id.rg);
        //创建集合
        list = new ArrayList<>();
        list.add(new Fragment01());
        list.add(new Fragment02());

        //ViewPager适配器
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

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

        //滑动切换状态
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                rg.check(rg.getChildAt(i).getId());

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //点击切换
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        vp.setCurrentItem(1);
                        break;
                }
            }
        });
    }
}

======================Fragment详细代码======================================

public class Fragment01 extends Fragment {
    private String url = "http://v.juhe.cn/toutiao/index?type=2&key=b6c3b1850433a790be8f9c1bef28e424";
    private ListView lv;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment01,container,false);
        //查找控件
        lv = view.findViewById(R.id.lv);
        get(url);
        return view;
    }

     public void get(final String s){
             //第三个参数 决定 你重写的 方法中的 返回值
             new AsyncTask<String,Void,String>(){
                 @Override
                 protected String doInBackground(String... strings) {
                     try {
                         URL url =new URL(s);
                         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                         connection.setConnectTimeout(5000);
                         connection.setReadTimeout(5000);
                         connection.setRequestMethod("GET");
                         if(connection.getResponseCode()==200){
                             InputStream inputStream = connection.getInputStream();
                             String s1 = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
                             return s1;
                         }
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
                     return null;
                 }
                 //主线程中 回调 拿回
                 @Override
                 protected void onPostExecute(String s) {
                     super.onPostExecute(s);
                     Gson gson =new Gson();
                     jsonBean jsonBean = gson.fromJson(s, jsonBean.class);
                     List<jsonBean.ResultBean.DataBean> list = jsonBean.getResult().getData();
                     lv.setAdapter(new MyAdapter(getActivity(),list));
                 }
             }
                 // 开启
                 .execute();
         }
}

=========================Adapter===============================

public class MyAdapter extends BaseAdapter {

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

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

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(context, R.layout.item, null);
        ImageView image = convertView.findViewById(R.id.image);
        TextView tv1 = convertView.findViewById(R.id.tv1);
        jsonBean.ResultBean.DataBean dataBean = list.get(position);
        tv1.setText(dataBean.getTitle());
        Glide.with(context).load(dataBean.getThumbnail_pic_s02()).into(image);

        return convertView;
    }
}

===================最后的最后=========================

注意权限,导包,导依赖

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值