代码已经整理好,效果如下图:
首先:RecyclerViewGridViewHorizontalFragment.java
import android.content.Context;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.GridView;import android.widget.RelativeLayout;import android.widget.TextView;import com.haiersmart.smartfridgefirst.R;import com.haiersmart.smartfridgefirst.constant.ConstantUtil;import com.haiersmart.smartfridgefirst.domain.FragmentResult;import com.haiersmart.smartfridgefirst.domain.ListViewLabOne;import com.haiersmart.smartfridgefirst.domain.ListViewLabOne2;import com.haiersmart.smartfridgefirst.domain.NetMessage;import com.haiersmart.smartfridgefirst.domain.ShouyeLeft1Model;import com.haiersmart.smartfridgefirst.domain.ShouyeLeft1ModelNew;import com.haiersmart.smartfridgefirst.ui.adapter.MyViewPagerAdapterDemo;import com.haiersmart.smartfridgefirst.ui.adapter.ShouyeLeft1AdapterDemo;import com.haiersmart.smartfridgefirst.ui.base.BaseNetWorkFragment;import com.haiersmart.smartfridgefirst.util.MyLogUtil;import com.haiersmart.smartfridgefirst.util.ToastUtil;import java.util.ArrayList;import java.util.List;/** * @function: * @description: * @history: 1. Date:2015/12/26 20:27 * Author:jack_D * modification: */public class RecyclerViewGridViewHorizontalFragment extends BaseNetWorkFragment { private TextView tv, tvback; private Button test_net_button; //MainPager View One 下一页标识符 private int pagerFlag; //TODO geek private TextView tv_Hello;//视图一title private RelativeLayout rl_Viewpager_LeftSlip;//左滑 private RelativeLayout rl_add_button;//加号 private RelativeLayout rl_del_button;//减号 private RelativeLayout rl_Viewpager_RightSlip;//右滑 private static final float APP_PAGE_SIZE = 8.0f; private MyViewPagerAdapterDemo adapter; private ShouyeLeft1AdapterDemo adapter2; private ArrayListarray; private ViewPager viewPager; private int exPos = 0; //假数据 // get all apps private int pageCount2 = 5;//服务器总页数 private List apps12 = ListViewLabOne.getmInstance().getmParent_model();//16 private List apps3 = ListViewLabOne2.getmInstance().getmParent_model2();//8 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyLogUtil.i(TAG, "-----onCreate()"); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { MyLogUtil.i(TAG, "-----onCreateView()"); this.inflater = inflater; setContentView(R.layout.activity_recyclerview, container);// baseView = inflater.inflate(R.layout.activity_test_net, container, false); findviews(); addlisteners(); requestViewOne(); if (getArguments() != null) { tv.setText(getArguments().getString(ConstantUtil.Bundle_INFO1)); } else { tv.setText("null"); } return baseView; } private void findviews() { test_net_button = (Button) findViewById(R.id.test_net_button); tv = (TextView) findViewById(R.id.test_net_tv); tvback = (TextView) findViewById(R.id.test_back_button); viewPager = (ViewPager) baseView.findViewById(R.id.viewpager_view_111); rl_Viewpager_LeftSlip = (RelativeLayout) baseView.findViewById(R.id.rl_Viewpager_LeftSlip); rl_Viewpager_RightSlip = (RelativeLayout) baseView.findViewById(R.id.rl_Viewpager_RightSlip); adapter2 = new ShouyeLeft1AdapterDemo(mContext); } private void addlisteners() { tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putString(ConstantUtil.Bundle_INFO1, "back Str"); setResult(ConstantUtil.ResultCodeO1, bundle); backTo(OneFragment.class); } }); test_net_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putString(ConstantUtil.Bundle_INFO1, "str 2 to 3"); startFragmentForResult(RecyclerViewGridViewHorizontalFragment.this, TestThreeFragment.class, bundle, ConstantUtil.RequestCodeO1, ConstantUtil.TAB_01); } }); tvback.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { back(); } }); setViewPagerOneListener(); } @Override public void onFragmentResult(FragmentResult result) { super.onFragmentResult(result); tv.setText(result.toString()); } @Override protected void networkCallBack(NetMessage message) { } @Override public void onStart() { MyLogUtil.i(TAG, "-----onStart()"); super.onStart(); } @Override public void onDetach() { MyLogUtil.i(TAG, "-----onDetach()"); super.onDetach(); } @Override public void onHiddenChanged(boolean hidden) { MyLogUtil.i(TAG, "-----onHiddenChanged()"); super.onHiddenChanged(hidden); } @Override public void onResume() { MyLogUtil.i(TAG, "-----onResume()"); super.onResume(); } @Override public void onPause() { MyLogUtil.i(TAG, "-----onPause()"); super.onPause(); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { MyLogUtil.i(TAG, "-----onActivityCreated()"); super.onActivityCreated(savedInstanceState); } @Override public void onStop() { MyLogUtil.i(TAG, "-----onStop()"); super.onStop(); } @Override public void onDestroy() { MyLogUtil.i(TAG, "-----onDestroy()"); super.onDestroy(); } @Override public void onDestroyView() { MyLogUtil.i(TAG, "-----onDestroyView()"); super.onDestroyView(); } @Override public void onAttach(Context context) { MyLogUtil.i(TAG, "-----onAttach()"); super.onAttach(context); } /** * ShouYeLeft1数据展示 */ private void requestViewOne() { adapter = new MyViewPagerAdapterDemo(mContext); viewPager.setAdapter(adapter); // the total pages final int PageCount = (int) Math.ceil(apps12.size() / APP_PAGE_SIZE); array = new ArrayList (); for (int i = 0; i < PageCount; i++) { GridView appPage = new GridView(mContext); ShouyeLeft1AdapterDemo adapter2 = new ShouyeLeft1AdapterDemo(mContext); if (i == 0) { List apps1 = apps12.subList(0, 8); adapter2.setContacts(apps1, i); appPage.setAdapter(adapter2); appPage.setNumColumns(4); array.add(appPage); } if (i == 1) { List apps2 = apps12.subList(8, apps12.size()); adapter2.setContacts(apps2, i); appPage.setAdapter(adapter2); appPage.setNumColumns(4); array.add(appPage); } } MyLogUtil.e("----array-------", array.size() + ""); adapter.setContacts(array); adapter.notifyDataSetChanged(); } private void updateViewpager(int position, List apps3) { int PageCount = (int) Math.ceil(apps3.size() / APP_PAGE_SIZE); if (position >= 1 && position < pageCount2 - 1) { GridView appPage = new GridView(mContext); adapter2.setContacts2(apps3, position + 1); adapter2.notifyDataSetChanged(); appPage.setAdapter(adapter2); appPage.setNumColumns(4); adapter.addConstacts(appPage); adapter.notifyDataSetChanged(); ToastUtil.showToastLong(position + ""); } else {// ToastUtil.showToastLong(position + "," + " 暂无数据"); } } /** * 当viewPager滑动时,将popwindow dismiss */ private void setViewPagerOneListener() { if (null != viewPager) { viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {// ToastUtil.showToastLong(position+""); } @Override public void onPageSelected(int position) { if (position > 0) { rl_Viewpager_LeftSlip.setVisibility(View.VISIBLE); } else { rl_Viewpager_LeftSlip.setVisibility(View.INVISIBLE); } if (position < (pagerFlag - 1)) { rl_Viewpager_RightSlip.setVisibility(View.VISIBLE); } else { rl_Viewpager_RightSlip.setVisibility(View.INVISIBLE); } if (position > exPos) { exPos = position; updateViewpager(position,apps3); } else {// ToastUtil.showToastLong(" 暂无数据"); } } @Override public void onPageScrollStateChanged(int state) { } }); } rl_Viewpager_LeftSlip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewPager.setCurrentItem(viewPager.getCurrentItem() - 1); } }); rl_Viewpager_RightSlip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); } }); }}
然后MyViewPagerAdapterDemo:
package com.haiersmart.smartfridgefirst.ui.adapter;import android.content.Context;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.view.View;import android.widget.GridView;import com.haiersmart.smartfridgefirst.util.MyLogUtil;import java.util.List;/** * 实现ViewPager页卡切换的适配器 * * @author Administrator */public class MyViewPagerAdapterDemo extends PagerAdapter { private Listarray; private Context context; /** * 供外部调用(new)的方法 * * @param context 上下文 * @param array 添加的序列对象 */ public MyViewPagerAdapterDemo(Context context ) { this.context =context; } public void setContacts(List array) { this.array = array; } public void addConstacts(List array) { this.array.addAll(array); } public void addConstacts(GridView gridView) { this.array.add(gridView); MyLogUtil.e("-----arraysssssss------",array.size()+""); } @Override public int getCount() { // TODO Auto-generated method stub if (array == null) return 0; return array.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public Object instantiateItem(View arg0, int arg1) { ((ViewPager) arg0).addView(array.get(arg1)); return array.get(arg1); } @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView((View) arg2); }}
还有一个ShouyeLeft1AdapterDemo:
package com.haiersmart.smartfridgefirst.ui.adapter;import android.annotation.SuppressLint;import android.content.Context;import android.os.Handler;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.PopupWindow;import android.widget.TextView;import com.haiersmart.smartfridgefirst.R;import com.haiersmart.smartfridgefirst.constant.FridgeApplication;import com.haiersmart.smartfridgefirst.domain.ShouyeLeft1Model;import com.haiersmart.smartfridgefirst.util.MyLogUtil;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class ShouyeLeft1AdapterDemo extends BaseAdapter { protected LayoutInflater inflater; protected Context context; private Listmratings; public static final int APP_PAGE_SIZE = 8;//每一页装载数据的大小 public PopupWindow pop; private View view; TextView fresh; TextView expirationDate; public Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if (null != pop && pop.isShowing()) { pop.dismiss(); } } ; }; public PopupWindow getPopupWindow() { return pop; } public Handler getHandler() { return handler; } public ShouyeLeft1AdapterDemo(Context context) { this.context = context; inflater = LayoutInflater.from(context); mratings = new ArrayList (); initPopupWindow(); } private void initPopupWindow() { view = inflater.inflate(R.layout.mainpager_tip, null); pop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); fresh = (TextView) view.findViewById(R.id.tv_Fresh); expirationDate = (TextView) view.findViewById(R.id.tv_ExpirationDate); pop.setOutsideTouchable(true); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pop.dismiss(); } }); } public void setContacts(List ratings, int page) { this.mratings = ratings; //根据当前页计算装载的应用,每页只装载16个// mratings.addAll(ratings);// int i = page * APP_PAGE_SIZE;//当前页的其实位置// int iEnd = i + APP_PAGE_SIZE;//所有数据的结束位置// while ((i < ratings.size()) && (i < iEnd)) {// mratings.add(ratings.get(i));// i++;// } MyLogUtil.e("------mratings-------", mratings.size() + ""); } public void setContacts2(List ratings, int page) { mratings = new ArrayList (); this.mratings = ratings; //根据当前页计算装载的应用,每页只装载16个// int i = page * APP_PAGE_SIZE;//当前页的其实位置// int iEnd = i + APP_PAGE_SIZE;//所有数据的结束位置 while ((i < ratings.size()) && (i < iEnd)) {// MyLogUtil.e("------mratings22222-------", mratings.size() + "");// mratings.addAll(i, ratings);// mratings.add(ratings.get(i));// i++;// }// for (int i = 0; i < APP_PAGE_SIZE; i++) {// mratings.add(ratings.get(i));// } } public void addConstacts(List ratings, int page) { this.mratings.addAll(ratings); } public List getMratings() { return mratings; } @Override public int getCount() { return mratings.size(); } @Override public Object getItem(int position) { return mratings.get(position); } @Override public long getItemId(int position) { return position; } private class ViewHolder { private ImageView ivAppIcon; private TextView tvAppName;// } @SuppressLint("InflateParams") @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; final ShouyeLeft1Model ratings = mratings.get(position); if (convertView == null) { convertView = inflater.inflate( R.layout.app_item, null); viewHolder = new ViewHolder(); viewHolder.ivAppIcon = (ImageView) convertView .findViewById(R.id.ivAppIcon); viewHolder.tvAppName = (TextView) convertView.findViewById(R.id.tvAppName); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } try { FridgeApplication.getImageLoader().displayImage(ratings.getImgUrl(), viewHolder.ivAppIcon, FridgeApplication.getLoaderOptionsFace22()); } catch (IOException e) { e.printStackTrace(); } viewHolder.tvAppName.setText(ratings.getTitle()); viewHolder.ivAppIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// //编辑部分// Intent intent = new Intent(context, AddressEditActivityBase.class);// intent.putExtra(ConstantUtil.INTENT_FROM,// AddressActivityBase.TAG);// intent.putExtra(ConstantUtil.INTENT_INFO1, ratings);// context.startActivity(intent);// if (null != ratings && position < mratings.size()) {// showpop(v, ratings.getId(), ratings.getId()); mFragment.requestCookBookListFromFoodID(list.get(position).getFood_definition_id());// } else {// showpop(v);// } } }); return convertView; } private void showpop(View v, int arg1, int arg2) { fresh.setText((context.getString(R.string.fresh) + arg1) + "%"); expirationDate.setText((context.getString(R.string.expiration_date)) + arg2 + "天"); if (pop.isShowing()) { handler.removeCallbacksAndMessages(null); pop.dismiss(); showpop(v, arg1, arg2); } else { int offx = ((int) context.getResources().getDimension(R.dimen.x150) - 110) / 2; int offy = -(int) (70 + context.getResources().getDimension(R.dimen.y150)); pop.showAsDropDown(v, offx, offy); handler.sendEmptyMessageDelayed(0, 2000); } } private void showpop(View v) { fresh.setText("大熊:"); expirationDate.setText("没有更多食材"); if (pop.isShowing()) { handler.removeCallbacksAndMessages(null); pop.dismiss(); showpop(v); } else { int offx = ((int) context.getResources().getDimension(R.dimen.x150) - 110) / 2; int offy = -(int) (70 + context.getResources().getDimension(R.dimen.y150)); pop.showAsDropDown(v, offx, offy); handler.sendEmptyMessageDelayed(0, 2000); } }}
假数据的Model:
package com.haiersmart.smartfridgefirst.domain;import java.util.ArrayList;import java.util.List;/** * Created by geek on 2016/2/26. */public class ListViewLabOne { // /**// *// */// private static final long serialVersionUID = 1L; public static ListViewLabOne mInstance; private ListmParent_model = new ArrayList (); private ListViewLabOne() { mParent_model.add(new ShouyeLeft1Model(1, "智能推荐1", "智能推荐1",1,false)); mParent_model.add(new ShouyeLeft1Model(2, "生鲜水果2", "生鲜水果2",1,false)); mParent_model.add(new ShouyeLeft1Model(3, "鸡鱼肉蛋3", "鸡鱼肉蛋3",1,false)); mParent_model.add(new ShouyeLeft1Model(4, "休闲零食4", "休闲零食4",1,false)); mParent_model.add(new ShouyeLeft1Model(5, "蛋糕甜点5", "蛋糕甜点5",1,false)); mParent_model.add(new ShouyeLeft1Model(6, "乳制品6", "乳制品6",1,false)); mParent_model.add(new ShouyeLeft1Model(7, "农场直达7", "农场直达7",1,false)); mParent_model.add(new ShouyeLeft1Model(8, "有机8", "有机8",1,false)); mParent_model.add(new ShouyeLeft1Model(1, "智能推荐9", "智能推荐9",1,false)); mParent_model.add(new ShouyeLeft1Model(2, "生鲜水果10", "生鲜水果10",1,false)); mParent_model.add(new ShouyeLeft1Model(3, "鸡鱼肉蛋11", "鸡鱼肉蛋11",1,false)); mParent_model.add(new ShouyeLeft1Model(4, "休闲零食12", "休闲零食12",1,false)); mParent_model.add(new ShouyeLeft1Model(5, "蛋糕甜点13", "蛋糕甜点13",1,false)); mParent_model.add(new ShouyeLeft1Model(6, "乳制品14", "乳制品14",1,false)); mParent_model.add(new ShouyeLeft1Model(7, "农场直达15", "农场直达15",1,false)); mParent_model.add(new ShouyeLeft1Model(8, "有机16", "有机16",1,false)); } public List getmParent_model() { return mParent_model; } public static ListViewLabOne getmInstance() { if (mInstance == null) { synchronized (ListViewLabOne.class) { if (mInstance == null) { mInstance = new ListViewLabOne(); } } } return mInstance; }}
还有一个Model:
package com.haiersmart.smartfridgefirst.domain;import java.util.ArrayList;import java.util.List;/** * Created by geek on 2016/2/26. */public class ListViewLabOne2 { // /**// *// */// private static final long serialVersionUID = 1L; public static ListViewLabOne2 mInstance; private ListmParent_model = new ArrayList (); private ListViewLabOne2() { mParent_model.add(new ShouyeLeft1Model(1, "智能推荐1", "智能推荐1", 1, false)); mParent_model.add(new ShouyeLeft1Model(2, "生鲜水果2", "生鲜水果2", 1, false)); mParent_model.add(new ShouyeLeft1Model(3, "鸡鱼肉蛋3", "鸡鱼肉蛋3", 1, false)); mParent_model.add(new ShouyeLeft1Model(4, "休闲零食4", "休闲零食4", 1, false)); mParent_model.add(new ShouyeLeft1Model(5, "蛋糕甜点5", "蛋糕甜点5", 1, false)); mParent_model.add(new ShouyeLeft1Model(6, "乳制品6", "乳制品6", 1, false)); mParent_model.add(new ShouyeLeft1Model(7, "农场直达7", "农场直达7", 1, false)); mParent_model.add(new ShouyeLeft1Model(8, "有机8", "有机8", 1, false)); } public List getmParent_model2() { return mParent_model; } public static ListViewLabOne2 getmInstance() { if (mInstance == null) { synchronized (ListViewLabOne2.class) { if (mInstance == null) { mInstance = new ListViewLabOne2(); } } } return mInstance; }}
减肥~健身~旅行~