viewpager+fragment通过HttpUrlconnection请求json数据并展示+侧滑抽屉

implementation 'com.google.code.gson:gson:2.2.4'
compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'


MainActivity
public class MainActivity extends AppCompatActivity {
    TabLayout mTabLayout;
    ViewPager mViewPager;
    protected ImageLoader imageLoader;
    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);

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));

        titles.add("新品");
        titles.add("分类");
        titles.add("我的");

        data.add(new xinpin());
        data.add(new fenlei());
        data.add(new wode());

        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

public class xinpin extends Fragment {
    ListView mListView;
    List<Bean.DataBean> data;
    private String ur="http://47.94.234.136/api/homeitemo.php";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.xinpin1, null, false);
        getData();
        mListView=inflate.findViewById(R.id.lv);
        return inflate;
    }

    private void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url=new URL(ur);
                    HttpURLConnection huc = (HttpURLConnection) url.openConnection();
                    huc.setRequestMethod("GET");
                    huc.setReadTimeout(2000);
                    huc.connect();
                    if (huc.getResponseCode()==200){
                        Gson gson=new Gson();
                        Bean bean = gson.fromJson(new InputStreamReader(huc.getInputStream(), "UTF-8"), Bean.class);
                        data = bean.getData();
                        mListView.setAdapter(new BeanAdapter(data,getActivity()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }


}
Bean
BeanAdapter
public class BeanAdapter extends BaseAdapter {
    private List<Bean.DataBean> dataBeans;
    Context context;

    public BeanAdapter(List<Bean.DataBean> dataBeans, Context context) {
        this.dataBeans = dataBeans;
        this.context = context;
    }

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

    @Override
    public Object getItem(int i) {
        return dataBeans.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item1, null, false);
        ImageView img = inflate.findViewById(R.id.img);

        ImageLoader.getInstance().displayImage(dataBeans.get(i).getPicture(),img);
        //Glide.with(context).load(dataBeans.get(i).getPicture()).into(img);
        return inflate;
    }
}

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.muonizhou1.MainActivity">

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

    </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>




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

</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:layout_height="wrap_content">
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>


xinpin1
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>


    <android.support.design.widget.NavigationView
        app:menu="@menu/menu_1"
        app:headerLayout="@layout/header"
        android:layout_gravity="left"
        android:layout_width="300dp"
        android:layout_height="match_parent">

    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>







阅读更多
文章标签: it
个人分类: 代码
上一篇xutils网络获取数据展示RecyclerView
下一篇viewpager+fragment联动加载网络数据展示XRecycview
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭