第一个fragment的网络请求+下拉刷新

//布局


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f56f98"
    >


            android:id="@+id/fuli_gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="2"

        />




//java代码



public class AndroidFragment extends Fragment {


    private View view;
    private PullToRefreshListView pull;

    private int page = 1;
    private List list = new ArrayList<>();
    private MyBaseAdapter01 base;

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


        view = inflater.inflate(R.layout.android_shouye_fragment, null);
        //初始化页面
        initViews();
        return view;


    }


    private void initViews() {

        //获取id
        pull = view.findViewById(R.id.android_pull);
        //开始上拉刷新/下拉加载
        pull.setMode(PullToRefreshBase.Mode.BOTH);

        //设置监听
        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                //刷新
                page = 1;

                //获取网络数据
                getinitWL();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {

                page++;
                getinitWL();
            }


        });

    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //创建适配器
        base = new MyBaseAdapter01(getContext(), list);

        pull.setAdapter(base);
    }

    //懒汉式加载
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            getinitWL();
        }

    }

    //获取网络数据
    public void getinitWL() {

        HttpUtils httpUtils = HttpUtils.getHttpUtils();
        httpUtils.get(HttpConfig.URL1);

        httpUtils.setHttpListener(new HttpUtils.HttpListener() {
            @Override
            public void getSuccess(String json) {

                //进行解析数据
                Gson gson = new Gson();

                AndroidBean android = gson.fromJson(json, AndroidBean.class);


                if (page == 1) {
                    list.clear();
                }

                list.addAll(android.getResults());


                //刷新适配器
                base.notifyDataSetChanged();
                pull.onRefreshComplete();

            }

            @Override
            public void getError(String error) {

            }
        });
    }


    public static AndroidFragment create(String title) {

        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        AndroidFragment af = new AndroidFragment();
        af.setArguments(bundle);
        return af;
    }
}


//适配器de java代码

public class MyAndroidAdapter extends BaseAdapter{


    private Context context;
    private List list;

    public MyAndroidAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if(convertView==null){

            convertView=View.inflate(context,R.layout.item_android,null);

             holder = new ViewHolder();

            holder.img=convertView.findViewById(R.id.an_img);
            holder.text=convertView.findViewById(R.id.an_text);

           convertView.setTag(holder);
        }else{
            holder=(ViewHolder) convertView.getTag();
        }

        List pic = list.get(position).getImages();
        if(pic!=null&&pic.size()>0){
            String s = pic.get(0);
            ImageLoader.getInstance().displayImage(s,holder.img, MyApp.getOptions());
        }

        holder.text.setText(list.get(position).getDesc());
        return convertView;
    }


 class ViewHolder{
        ImageView img;
        TextView text;
  }

}

//适配器的布局


    android:layout_width="match_parent"
    android:layout_height="match_parent">


            android:id="@+id/android_img"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"
        />

            android:id="@+id/android_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="20dp"
        />




你可能感兴趣的:(第一个fragment的网络请求+下拉刷新)