一,MainActivity
package com.bwei.month_lianxi1.activityis; import android.content.Intent; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.LinearLayout; import android.widget.RadioGroup; import android.widget.Toast; import com.bwei.month_lianxi1.Fragment.FenLei_Fragment; import com.bwei.month_lianxi1.Fragment.ShoYe_Fragment; import com.bwei.month_lianxi1.Fragment.WoDe_Fragment; import com.bwei.month_lianxi1.R; import com.bwei.month_lianxi1.adapter.MyFragmentAdapter; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private DrawerLayout drawerLayout; private RadioGroup radioGroup; private ViewPager viewPager; private LinearLayout shangpin; private LinearLayout xiatuijian; private List<Fragment> fragment=new ArrayList<>();//放Fragment的集合 private MyFragmentAdapter myFragmentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化界面 initview(); //初始化数据 //1.将Fragment 添加到主页面 侧滑的处理 initFragment(); } private void initFragment() { Fragment sy=new ShoYe_Fragment(); Fragment fl=new FenLei_Fragment(); Fragment wd=new WoDe_Fragment(); //添加到集合 fragment.add(sy); fragment.add(fl); fragment.add(wd); //创建适配器 myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(),fragment); viewPager.setAdapter(myFragmentAdapter); //给viewpager 添加监听,和Fragment绑定 viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position){ case 0: radioGroup.check(R.id.button01); break; case 1: radioGroup.check(R.id.button02); break; case 2: radioGroup.check(R.id.button03); break; } } @Override public void onPageScrollStateChanged(int state) { } }); } private void initview() { drawerLayout=findViewById(R.id.drawerlayout); radioGroup=findViewById(R.id.main_radiogroup); viewPager=findViewById(R.id.main_viewpager); shangpin = findViewById(R.id.shangpin); xiatuijian = findViewById(R.id.xiatuijian); //商品和瞎推荐的点击 shangpin.setOnClickListener(this); xiatuijian.setOnClickListener(this); //默认选中第一个 radioGroup.check(R.id.button01); //给radiogroup添加点击事件 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.button01: viewPager.setCurrentItem(0); break; case R.id.button02: viewPager.setCurrentItem(1); break; case R.id.button03: viewPager.setCurrentItem(2); break; } } }); } //侧滑 商品和瞎推荐的点击事件 @Override public void onClick(View v) { switch (v.getId()){ case R.id.shangpin: Intent it=new Intent(MainActivity.this,GoodsActivity.class); startActivity(it); drawerLayout.closeDrawers(); break; case R.id.xiatuijian: Toast.makeText(MainActivity.this,"瞎JB推荐",Toast.LENGTH_SHORT).show(); drawerLayout.closeDrawers(); break; } } }
二,ShouYe_Fragment
package com.bwei.month_lianxi1.Fragment; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.bwei.month_lianxi1.R; import com.bwei.month_lianxi1.adapter.MyFragmentAdapter2; import java.util.ArrayList; import java.util.List; public class ShoYe_Fragment extends Fragment { private View view; private ViewPager viewPager; private TabLayout tabLayout; private List<Fragment> tab=new ArrayList<>();//栏目的集合 @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.shouye_fragment, container, false); //初始化页面 initview(); return view; } private void initview() { viewPager = view.findViewById(R.id.shouye_viewpager); tabLayout = view.findViewById(R.id.shouye_tablayout); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); tab.clear(); //ViewPager里添加Fragment Fragment f1=new Android_Fragment().create("Android"); Fragment f2=new Ios_Fragment().create("IOS"); Fragment f3=new Bfm_Fragment().create("白富美"); //添加到集合里 tab.add(f1); tab.add(f2); tab.add(f3); //创建栏目适配器 MyFragmentAdapter2 adapter = new MyFragmentAdapter2(getChildFragmentManager(),tab); viewPager.setAdapter(adapter); //添加标签 设置下划线的指针颜色 tabLayout.setSelectedTabIndicatorColor(Color.RED); //添加选项卡 TabLayout.Tab tab1 = tabLayout.newTab(); tabLayout.addTab(tab1); TabLayout.Tab tab2 = tabLayout.newTab(); tabLayout.addTab(tab2); TabLayout.Tab tab3 = tabLayout.newTab(); tabLayout.addTab(tab3); //将ViewPager 关联到tablayout上 tabLayout.setupWithViewPager(viewPager); } }
三,Android_Fragment
package com.bwei.month_lianxi1.Fragment; import android.os.Bundle; import android.support.annotation.NonNull; 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.bwei.month_lianxi1.R; import com.bwei.month_lianxi1.adapter.MyAndroidBase; import com.bwei.month_lianxi1.bean.Android; import com.bwei.month_lianxi1.httpurl.HttpUrl; import com.bwei.month_lianxi1.httputil.utils.HttpUtils; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.ArrayList; import java.util.List; public class Android_Fragment extends Fragment { private static final String TAG = "Android_Fragment----"; private View view; private PullToRefreshListView pullToRefreshListView; private int page=1; private List<Android.ResultsBean> list=new ArrayList<>(); private MyAndroidBase base; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.android_layout, container, false); //初始化视图 initview(); return view; } private void initview() { pullToRefreshListView = view.findViewById(R.id.pull_to_refresh_listview); //开启下拉和上拉刷新 pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH); //设置监听 pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { //下拉刷新 page=1; initgetData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { //上拉加载 page++; initgetData(); } }); } //加载页面 @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //创建适配器 base = new MyAndroidBase(getContext(),list); pullToRefreshListView.setAdapter(base); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if(isVisibleToUser){ //请求网络数据 initgetData(); } } private void initgetData() { HttpUtils instance = HttpUtils.getInstance(); instance.get(HttpUrl.ANDROID_URL+page); instance.setHttpUtilsListener(new HttpUtils.HttpUtilListener() { @Override public void getSuccess(String json) { Log.d(TAG, "getSuccess: "+json); //成功 Gson gson=new Gson(); Log.d(TAG, "成功: "+json); Android androidbean = gson.fromJson(json, Android.class); List<Android.ResultsBean> results = androidbean.getResults(); if(page==1){ list.clear(); } //当有数据之后,list加上去y list.addAll(results); //刷新适配器 base.notifyDataSetChanged(); pullToRefreshListView.onRefreshComplete(); } @Override public void getError(String error) { //失败 Gson gson=new Gson(); Log.d(TAG, "失败: "+error); } }); } public static Android_Fragment create(String title){ Bundle bundle = new Bundle(); bundle.putString("title",title); Android_Fragment fragment = new Android_Fragment(); fragment.setArguments(bundle); return fragment; } }
四.IOS_Fragment
package com.bwei.month_lianxi1.Fragment; import android.os.Bundle; import android.support.annotation.NonNull; 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.bwei.month_lianxi1.R; import com.bwei.month_lianxi1.adapter.MyIosBase; import com.bwei.month_lianxi1.bean.Ios; import com.bwei.month_lianxi1.httpurl.HttpUrl; import com.bwei.month_lianxi1.httputil.utils.HttpUtils; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.ArrayList; import java.util.List; public class Ios_Fragment extends Fragment { private static final String TAG = "Ios_Fragment----"; private View view; private PullToRefreshListView pullToRefreshListView; private int page=1; private List<Ios.ResultsBean> list=new ArrayList<>();//ios集合 private MyIosBase base; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.ios_item, container, false); //初始化界面 initview(); return view; } private void initview() { pullToRefreshListView = view.findViewById(R.id.ios_pull_to_refresh_listview); //开启下拉和下拉刷新 pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH); //设置监听 pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { //下拉刷新 page=1; getData(); } //上拉加载 @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { page++; getData(); } }); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //创建适配器 base = new MyIosBase(getContext(),list); pullToRefreshListView.setAdapter(base); } public static Ios_Fragment create(String title){ Bundle bundle = new Bundle(); bundle.putString("title",title); Ios_Fragment fragment = new Ios_Fragment(); fragment.setArguments(bundle); return fragment; } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); // 请求网络数据 getData(); } private void getData() { HttpUtils instance = HttpUtils.getInstance(); instance.get(HttpUrl.IOS_URL+page); instance.setHttpUtilsListener(new HttpUtils.HttpUtilListener() { @Override public void getSuccess(String json) { //成功 Gson gson=new Gson(); Ios ios = gson.fromJson(json, Ios.class); List<Ios.ResultsBean> results = ios.getResults(); if(page==1){ list.clear(); } //添加到集合里 list.addAll(results); //刷新适配器 base.notifyDataSetChanged(); pullToRefreshListView.onRefreshComplete(); } @Override public void getError(String error) { Log.d(TAG, "失败: "+error); //失败 } }); } }
五,FuLi_Fragment
package com.bwei.month_lianxi1.Fragment; import android.os.Bundle; import android.support.annotation.NonNull; 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.GridView; import com.bwei.month_lianxi1.R; import com.bwei.month_lianxi1.adapter.MyGridBase; import com.bwei.month_lianxi1.bean.Fl; import com.bwei.month_lianxi1.httpurl.HttpUrl; import com.bwei.month_lianxi1.httputil.utils.HttpUtils; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshGridView; import java.util.ArrayList; import java.util.List; public class Bfm_Fragment extends Fragment { private static final String TAG = "Bfm_Fragment----"; private View view; private PullToRefreshGridView pullToRefreshGridView; private int page=1; private List<Fl.ResultsBean> bfm=new ArrayList<>(); private MyGridBase base; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fl_item, container, false); //初始化视图 initview(); return view; } private void initview() { pullToRefreshGridView = view.findViewById(R.id.bfm_pull_to_refresh_gridview); //设置上拉和加载 pullToRefreshGridView.setMode(PullToRefreshBase.Mode.BOTH); //设置监听 pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) { //下拉刷新 page=1; getData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) { //上拉加载 page++; getData(); } }); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //获取数据 getData(); //创建适配器 base = new MyGridBase(getContext(),bfm); pullToRefreshGridView.setAdapter(base); } private void getData() { HttpUtils instance = HttpUtils.getInstance(); instance.get(HttpUrl.FULI_URL+page); instance.setHttpUtilsListener(new HttpUtils.HttpUtilListener() { @Override public void getSuccess(String json) { Log.d(TAG, "getSuccess: "+json); //成功 Gson gson=new Gson(); Fl fl = gson.fromJson(json, Fl.class); List<Fl.ResultsBean> results = fl.getResults(); //添加到集合 if(page==1){ bfm.clear(); } bfm.addAll(results); //刷新适配器 base.notifyDataSetChanged(); //清除数据 pullToRefreshGridView.onRefreshComplete(); } @Override public void getError(String error) { //失败 } }); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if(isVisibleToUser){ getData(); } } public static Bfm_Fragment create(String title){ Bundle bundle = new Bundle(); bundle.putString("title",title); Bfm_Fragment fragment = new Bfm_Fragment(); fragment.setArguments(bundle); return fragment; } }
六,ShangPin_Fragment
package com.bwei.month_lianxi1.activityis; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ScrollView; import android.widget.Toast; import com.bwei.month_lianxi1.MyListView; import com.bwei.month_lianxi1.R; import com.bwei.month_lianxi1.adapter.MyShangPin; import com.bwei.month_lianxi1.adapter.MyViewPager; import com.bwei.month_lianxi1.app.MyApp; import com.bwei.month_lianxi1.bean.Goods; import com.bwei.month_lianxi1.bean.ShangPin; import com.bwei.month_lianxi1.httpurl.HttpUrl; import com.bwei.month_lianxi1.httputil.utils.HttpUtils; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshScrollView; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; import java.util.List; public class GoodsActivity extends AppCompatActivity { private static final String TAG = "GoodsActivity-----"; private PullToRefreshScrollView pullToRefreshScrollView; private int page=1; private ViewPager viewPager; private MyListView listView; private List<ImageView> goods=new ArrayList<>();//放图片的集合 轮播图 private MyViewPager myViewPager; private List<ShangPin.DataBean> shangpin_list=new ArrayList<>();//商品集合 private MyShangPin myShangPin; private List<ImageView> shape=new ArrayList<>();//小圆点集合 private LinearLayout linearLayout; private MyHandler myHandler=new MyHandler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_goods); //初始化界面 initview(); //Viewpager适配器 //商品适配器 myShangPin = new MyShangPin(GoodsActivity.this,shangpin_list); listView.setAdapter(myShangPin); //请求ViewPgaer数据 getData(); //自动轮播 } class MyHandler extends Handler { @Override public void handleMessage(Message msg) { viewPager.setCurrentItem(viewPager.getCurrentItem()+1); //设置小圆点 for (int i = 0; i <shape.size() ; i++) { if(i==viewPager.getCurrentItem()%shape.size()){ shape.get(i).setSelected(true); }else{ shape.get(i).setSelected(false); } } myHandler.sendEmptyMessageDelayed(0,3000); } } private void getListViewData() { HttpUtils instance = HttpUtils.getInstance(); instance.get(HttpUrl.GOODS_URL+page); instance.setHttpUtilsListener(new HttpUtils.HttpUtilListener() { @Override public void getSuccess(String json) { Log.d(TAG, "商品: "+json); //成功 Gson gson=new Gson(); ShangPin shangPin = gson.fromJson(json, ShangPin.class); List<ShangPin.DataBean> data = shangPin.getData();//商品集合 //判断如果page=1的时候,清空集合 if(page==1){ shangpin_list.clear(); } //判断没有数据的时候 吐司 if(shangpin_list.size()==0){ Toast.makeText(GoodsActivity.this,"没有数据",Toast.LENGTH_SHORT).show(); } //将数据添加到集合里 shangpin_list.addAll(data); //刷新 myShangPin.notifyDataSetChanged(); //让ScrollView 缩回去 pullToRefreshScrollView.onRefreshComplete(); } @Override public void getError(String error) { } }); } private void getData() { HttpUtils instance = HttpUtils.getInstance(); instance.get(HttpUrl.AD_URL); instance.setHttpUtilsListener(new HttpUtils.HttpUtilListener() { @Override public void getSuccess(String json) { //成功 Gson gson=new Gson(); Goods goodsbean = gson.fromJson(json, Goods.class); //将数据添加到集合 List<Goods.DataBean> data = goodsbean.getData(); //循环 图片的url地址 for (int i = 0; i <data.size() ; i++) { String icon = data.get(i).getIcon();//取出图片的地址 ImageView imageView=new ImageView(GoodsActivity.this); ImageLoader.getInstance().displayImage(icon,imageView, MyApp.getOptions());//加载图片 //添加到集合里 goods.add(imageView); //添加小圆点 ImageView p=new ImageView(GoodsActivity.this); p.setImageResource(R.drawable.selector_shape); //添加到集合 shape.add(p); //设置小圆点间距 LinearLayout.LayoutParams jj=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); jj.setMargins(15,0,0,0); //添加到布局 linearLayout.addView(p,i); } //默认第一个选中 shape.get(0).setSelected(true); //创建适配器 myViewPager = new MyViewPager(goods); viewPager.setAdapter(myViewPager); //开启自动轮播 myHandler.sendEmptyMessageDelayed(0,3000); //请求listView 数据 getListViewData(); } @Override public void getError(String error) { //失败 } }); } private void initview() { pullToRefreshScrollView = findViewById(R.id.Scroll_to_refresh_Scrolltview); viewPager = findViewById(R.id.goods_viewpager); listView = findViewById(R.id.goods_listview); linearLayout = findViewById(R.id.goods_shapa); //给scroll一个监听 下拉刷新 pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH); pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) { //下拉刷新 page=1; getListViewData(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) { //上拉加载 page++; getListViewData(); } }); } }
七,自定义View
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int height=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, height); }