xutils网络获取数据展示RecyclerView

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.myapplication.MainActivity">


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

    </android.support.v4.view.ViewPager>
    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout_id"
        android:layout_width="match_parent"
        android:layout_height="60dp">
    </android.support.design.widget.TabLayout>
</LinearLayout>

xinpin1

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

    <TextView
        android:gravity="center"
        android:textSize="30dp"
        android:textColor="#000"
        android:text="新品"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

item1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/item_img"
        android:layout_width="150dp"
        android:layout_height="150dp" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="150dp"
        android:layout_height="50dp">
        <LinearLayout
            android:layout_weight="3"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <TextView
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:text="asos"
                android:id="@+id/asos"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="0dp" />
            <TextView
                android:layout_marginLeft="5dp"
                android:layout_marginBottom="5dp"
                android:text="中文商品名"
                android:id="@+id/zwspm"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="0dp" />
        </LinearLayout>
        <TextView
            android:gravity="center"
            android:text="价格"
            android:id="@+id/jg"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

MainActivity

package com.example.myapplication;


import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TabLayout mTabLayout;
    ViewPager mViewPager;
    private List<String> titles=new ArrayList<>();
    private List<Fragment> data=new ArrayList<>();
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabLayout=findViewById(R.id.tab_layout_id);
        mViewPager=findViewById(R.id.view_pager_id);
        titles.add("新品");
        titles.add("精选");
        titles.add("分类");
        titles.add("我的衣橱");
        titles.add("个人中心");
        data.add(new xinpin());
        data.add(new jingxuan());
        data.add(new fenei());
        data.add(new wodeyichu());
        data.add(new gerenzhongxin());
        adapter=new MyAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);

    }



    class MyAdapter extends FragmentPagerAdapter{


        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

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

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

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


}


xinpin

package com.example.myapplication;

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

import com.google.gson.Gson;


import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 猴嫂 on 2018/4/2.
 */

public class xinpin extends Fragment {

    RecyclerView mRecyclerView;
    private List<bean> beanList=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.xinpin1, container, false);
        mRecyclerView=inflate.findViewById(R.id.recyclerView);



        RequestParams requestParams=new RequestParams("http://atp.fulishe.com/ClientApi/category.php?api_version=1.0&act=search_category_goods_list&c_id=35&order_price=0&page_num=20&page=1&debug=true&client_id=null");
        x.http().get(requestParams, new org.xutils.common.Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                        Log.e("www",""+result);
                Gson gson=new Gson();
                bean b=gson.fromJson(result,bean.class);
                beanList.add(b);
                ReAdapter reAdapter = new ReAdapter(getContext(), beanList);
                mRecyclerView.setAdapter(reAdapter);
                mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("www","onError");
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("www","onCancelled");
            }

            @Override
            public void onFinished() {

            }
        });

        return inflate;
    }
}

jingxuan

public class jingxuan extends android.support.v4.app.Fragment {
}


MyApp

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(true);
    }
}

ReAdapter--------------------------------------------------------------------
package com.example.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 猴嫂 on 2018/4/2.
 */

public class ReAdapter extends RecyclerView.Adapter<ReAdapter.Holder> {
    Context context;
    private List<bean> spbean;

    public ReAdapter(Context context, List<bean> spbean) {
        this.context = context;
        this.spbean = spbean;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item1,parent,false);
        Holder holder=new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        bean b=spbean.get(0);
        bean.InfoBean.GoodsBean goodsBean = b.getInfo().getGoods().get(position);
        Picasso.with(context).load(goodsBean.getThumb()).into(holder.item_img);
        holder.zwspm.setText(goodsBean.getGoods_name());
        holder.jg.setText(goodsBean.getShop_price());
    }

    @Override
    public int getItemCount() {
        //spbean.size();
        return spbean.get(0).getInfo().getGoods().size();
    }

    class Holder extends RecyclerView.ViewHolder{
        private ImageView item_img;
        private TextView asos;
        private TextView zwspm;
        private TextView jg;

        public Holder(View itemView) {
            super(itemView);
            item_img=itemView.findViewById(R.id.item_img);
            asos=itemView.findViewById(R.id.asos);
            zwspm=itemView.findViewById(R.id.zwspm);
            jg=itemView.findViewById(R.id.jg);
        }
    }
}

bean类自己生成 alt+s



implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.android.support:support-v4:26.1.0'
implementation 'com.squareup.picasso:picasso:2.3.2'

implementation 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.okhttp3:okhttp:3.8.0'

compile 'org.xutils:xutils:3.3.36'


<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:name=".MyApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>



猜你喜欢

转载自blog.csdn.net/qq_41326326/article/details/79837231