先看代码:
1. public class Fragment_Home extends Fragment implements HomeInterFace
{
private ViewPager vp_home;
private HomeViewpageAdapter homeViewpageAdapter1;
private HomePersenter homePersenter;
private List<DataBean.ResultBean.BrandsBean> brands;
private LinearLayout linearLayout;
private ArrayList<View> views;
private int upIndex = 0;
private int currentitem = 0;
private Timer timer;
private GridView grid_view;
private GridAdapter gridAdapter;
private List<DataBean.ResultBean.IndexProductsBean> indexProducts;
private TextView more_;
private List<DataBean.ResultBean.CategoryBean> category;
private List<DataBean.ResultBean.NationsBean> nations;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragmenthome, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView();
initData();
}
private void initData() {
//接口回掉
homePersenter = new HomePersenter();
homePersenter.setImvpview(this);
homePersenter.getData();
}
private void initView() {
vp_home = (ViewPager) getView().findViewById(R.id.vp_home);
linearLayout = (LinearLayout) getView().findViewById(R.id.line);
grid_view = (GridView) getView().findViewById(R.id.grid_f1);
more_ = (TextView) getView().findViewById(R.id.more_text);
//viewpage的页面切换监听
vp_home.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//无限轮播所以需%
views.get((upIndex % views.size())).setBackgroundResource(R.drawable.dot_false);
views.get((position % views.size())).setBackgroundResource(R.drawable.dot_true);
upIndex = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//点击更多的跳转
more_.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (category.size()>0 && nations.size()>0) {
//意图跳转传值,注意bean类需要实现序列化Serializable,bean类直接实现此接口即可
Intent intent = new Intent(getActivity(),SecendActivity.class);
intent.putExtra("up", (Serializable) category);
intent.putExtra("down", (Serializable) nations);
startActivity(intent);
}
}
});
}
//数据回掉的方法
@Override
public void CallBack(DataBean dataBean) {
//两个频道的数据
category = dataBean.getResult().getCategory();
nations = dataBean.getResult().getNations();
//轮播图数据
brands = dataBean.getResult().getBrands();
//gridview数据
indexProducts = dataBean.getResult().getIndexProducts();
//设置gridview
gridAdapter = new GridAdapter(getActivity());
gridAdapter.setData(indexProducts);
grid_view.setAdapter(gridAdapter);
//设置轮播图
homeViewpageAdapter1 = new HomeViewpageAdapter(getActivity());
homeViewpageAdapter1.setData(brands);
vp_home.setAdapter(homeViewpageAdapter1);
if (brands != null) {
//轮播图下面小圆点的集合,智能添加删除
views = new ArrayList<>();
for (int i = 0; i < brands.size(); i++) {
View view = new View(getActivity());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
view.setLayoutParams(layoutParams);
views.add(view);
if (i == 0) {
view.setBackgroundResource(R.drawable.dot_true);
} else {
view.setBackgroundResource(R.drawable.dot_false);
}
linearLayout.addView(views.get(i));
}
}
//计时器每两秒执行一次
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
vp_home.setCurrentItem(currentitem, false);
currentitem++;
}
});
}
}, 0, 2000);
}
//失去焦点时让计时器失效
@Override
public void onPause() {
super.onPause();
if (timer != null) {
timer.cancel();
timer = null;
}
}
重新获取焦点是运行
@Override
public void onResume() {
super.onResume();
if(vp_home!=null){
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
vp_home.setCurrentItem(currentitem, false);
currentitem++;
}
});
}
}, 0, 2000);
}
} }
//secendactivity界面 为接值的类
public class SecendActivity extends AppCompatActivity implements MyOnclick {
private TextView menu_text;
private RecyclerView recyclerview1;
private RecyclerView recyclerview2;
private UpRecycleViewAdapter upRecycleViewAdapter;
private BottomRecycleViewAdapter bottomRecycleViewAdapter;
private ArrayList<DataBean.ResultBean.CategoryBean> up;
private ArrayList<DataBean.ResultBean.NationsBean> down;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secend);
initView();
initData();
}
private void initData() {
//获取从上个界面传过来的值
Intent intent = getIntent();
up = (ArrayList<DataBean.ResultBean.CategoryBean>) intent.getSerializableExtra("up");
down = (ArrayList<DataBean.ResultBean.NationsBean>) intent.getSerializableExtra("down");
upRecycleViewAdapter.setData(up);
upRecycleViewAdapter.notifyDataSetChanged();
bottomRecycleViewAdapter.setData(down);
bottomRecycleViewAdapter.notifyDataSetChanged();
Log.e("initData: ", up.size() + "==" + down.size());
}
private void initView() {
menu_text = (TextView) findViewById(R.id.menu_text);
GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this, 4);
//recyclerview1 初始化动作
recyclerview1 = (RecyclerView) findViewById(R.id.recyclerview1);
recyclerview1.setLayoutManager(gridLayoutManager1);
upRecycleViewAdapter = new UpRecycleViewAdapter(this);
recyclerview1.setAdapter(upRecycleViewAdapter);
upRecycleViewAdapter.setMyOnclick(this);
recyclerview1.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
//recyclerview2 初始化动作
GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this, 4);
recyclerview2 = (RecyclerView) findViewById(R.id.recyclerview2);
recyclerview2.setLayoutManager(gridLayoutManager2);
bottomRecycleViewAdapter = new BottomRecycleViewAdapter(this);
recyclerview2.setAdapter(bottomRecycleViewAdapter);
bottomRecycleViewAdapter.setMyOnclick(this);
}
@Override
public void OnClickBack(View v, int position, int type) {
switch (type) {
case 1: {
//先添加,后移除
DataBean.ResultBean.NationsBean nationsBean = new DataBean.ResultBean.NationsBean();
nationsBean.setName(up.get(position).getName());
down.add(nationsBean);
bottomRecycleViewAdapter.setData(down);
bottomRecycleViewAdapter.notifyDataSetChanged();
up.remove(position);
upRecycleViewAdapter.setData(up);
upRecycleViewAdapter.notifyDataSetChanged();
}
break;
case 2: {
DataBean.ResultBean.CategoryBean categoryBean = new DataBean.ResultBean.CategoryBean();
categoryBean.setName(down.get(position).getName());
up.add(categoryBean);
upRecycleViewAdapter.setData(up);
upRecycleViewAdapter.notifyDataSetChanged();
down.remove(position);
bottomRecycleViewAdapter.setData(down);
bottomRecycleViewAdapter.notifyDataSetChanged();
}
break;
}
}
}
接下来是适配器类
**//轮播图的适配器**
public class HomeViewpageAdapter extends PagerAdapter {
private ArrayList<DataBean.ResultBean.BrandsBean> list = new ArrayList<>();
private Context mcontext;
public HomeViewpageAdapter(Context mcontext) {
this.mcontext = mcontext;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(mcontext);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
Utils.setImage(list.get((position % list.size())).getPic(), imageView);
container.addView(imageView);
return imageView;
}
public void setData(List<DataBean.ResultBean.BrandsBean> brands) {
if (brands != null) {
this.list.clear();
this.list.addAll(brands);
notifyDataSetChanged();
}
}
}
**//上面RecycleView的适配器**
public class UpRecycleViewAdapter extends RecyclerView.Adapter<UpRecycleViewAdapter.UpViewHolder> {
private ArrayList<DataBean.ResultBean.CategoryBean> list = new ArrayList<>();
private Context mcontext;
private SecendActivity secendActivity;
public UpRecycleViewAdapter(Context mcontext) {
this.mcontext = mcontext;
}
@Override
public UpViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = View.inflate(mcontext, R.layout.recycleview_item, null);
UpViewHolder upViewHolder = new UpViewHolder(inflate);
return upViewHolder;
}
@Override
public void onBindViewHolder(final UpViewHolder holder, final int position) {
holder.textView.setText(list.get(position).getName());
//条目点击事件
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (secendActivity!=null){
secendActivity.OnClickBack(holder.textView,position,1);
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void setData(ArrayList<DataBean.ResultBean.CategoryBean> category_been) {
if (category_been != null) {
this.list.clear();
this.list.addAll(category_been);
}
}
public void setMyOnclick(SecendActivity secendActivity) {
this.secendActivity=secendActivity;
}
public class UpViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public UpViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text_rr);
}
}
}
**//下面RecycleView的适配器**
public class BottomRecycleViewAdapter extends RecyclerView.Adapter<BottomRecycleViewAdapter.BottomViewHolder> {
private ArrayList<DataBean.ResultBean.NationsBean> list=new ArrayList<>();
private Context mcontext;
private SecendActivity secendActivity;
public BottomRecycleViewAdapter(Context mcontext) {
this.mcontext = mcontext;
}
@Override
public BottomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = View.inflate(mcontext, R.layout.recycleview_item, null);
BottomViewHolder ViewHolder = new BottomViewHolder(inflate);
return ViewHolder;
}
@Override
public void onBindViewHolder(final BottomViewHolder holder, final int position) {
holder.textView.setText(list.get(position).getName()+"dd");
//条目点击事件
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (secendActivity!=null){
secendActivity.OnClickBack(holder.textView,position,2);
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void setData(ArrayList<DataBean.ResultBean.NationsBean> nationsBeens) {
if (nationsBeens != null) {
this.list.clear();
this.list.addAll(nationsBeens);
}
}
public void setMyOnclick(SecendActivity secendActivity) {
this.secendActivity=secendActivity;
}
public class BottomViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public BottomViewHolder(View itemView) {
super(itemView);
textView= (TextView) itemView.findViewById(R.id.text_rr);
}
}
}
//utils类
public class Utils {
public static void HttpGet(String url, final UtilsInterface utilsInterface){
x.http().get(new RequestParams(url), new Callback.CommonCallback<String >() {
@Override
public void onSuccess(String result) {
Gson gson = new Gson();
DataBean dataBean = gson.fromJson(result, DataBean.class);
utilsInterface.CallBack(dataBean);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.e( "dddddddddd ", ex.getMessage());
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
}
public static void setImage(String url, ImageView imageView){
DisplayImageOptions build = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.cacheOnDisk(true)
.cacheInMemory(false)
.build();
ImageLoader.getInstance().displayImage(url, imageView, build);
}
public interface UtilsInterface{
<T>void CallBack(T t);
}
}
**//在Appliction的初始化动作**
public class MyAppliction extends Application{
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);
//初始化ImageLoader
File cacheDirectory = StorageUtils.getOwnCacheDirectory(this, Environment.getExternalStorageDirectory().getPath());
ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this)
.diskCacheFileCount(500)
.diskCacheSize(100 * 1024 * 1024)
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCache(new UnlimitedDiskCache(cacheDirectory))
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.threadPoolSize(3)
.build();
ImageLoader.getInstance().init(imageLoaderConfiguration);
}
}
由于时间原因,代码写的有些匆忙,大家能看懂就多看看,谢谢!
你的支持就是我的动力!我是桃先森我为狸菇凉代言>_<