一、概述
作为一名三年Android开发经验的程序员,今天和大家一起实战一款APP的首页功能,这个首页在我们平时接触中还是很常见的,虽然页面简单,但是里面涉及的功能点还是挺多的。代码如有不足的还望各路同仁指点一二。
页面中使用的开发库:
整个首页架构使用的是LRecyclerView,包含下拉刷新和自动加载功能
compile 'com.github.jdsjlzx:LRecyclerView:1.3.3'
无限循环轮播图使用的是convenientbanner,效果还是很顺畅的,还可以根据自己的需要修改过渡动画
compile 'com.bigkoo:convenientbanner:2.0.5'
图片加载使用的是glide图片库,里面的方法是自己封装的
网络请求依赖是okhttp,使用的开源库okgo
compile 'com.lzy.net:okgo:2.1.4'
其他的还是九宫格图
compile 'com.lzy.widget:ninegridview:0.2.0'
自动注解butterknife库等等
二、实现效果图
1.首页展示轮播图
2.下拉刷新
3.自动加载
4.cardview水波纹动画
三、核心代码
LRecyclerView作为主框架,轮播图以及分类网格列表作为header放在index_header.xml布局文件下。
IndexFragment.java
package com.czhappy.commonindexdemo.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.czhappy.commonindexdemo.KuaiZhiApplication;
import com.czhappy.commonindexdemo.R;
import com.czhappy.commonindexdemo.adapter.CampaignListAdapter;
import com.czhappy.commonindexdemo.adapter.ClassflyListAdapter;
import com.czhappy.commonindexdemo.jsonparse.JSONObject;
import com.czhappy.commonindexdemo.jsonparse.ReflectUtil;
import com.czhappy.commonindexdemo.model.CampaignList;
import com.czhappy.commonindexdemo.model.Classfly;
import com.czhappy.commonindexdemo.model.IndexBanner;
import com.czhappy.commonindexdemo.model.IndexBannerList;
import com.czhappy.commonindexdemo.okhttp.LoadingDialogCallback;
import com.czhappy.commonindexdemo.utils.AccordionTransformer;
import com.czhappy.commonindexdemo.utils.Api;
import com.czhappy.commonindexdemo.utils.NetworkImageHolderView;
import com.czhappy.commonindexdemo.utils.ToastUtil;
import com.czhappy.commonindexdemo.view.GridViewForScrollView;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.lzy.okgo.OkGo;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;
/**
* Description:
* User: chenzheng
* Date: 2016/9/9 0009
* Time: 17:18
*/
public class IndexFragment extends Fragment implements ViewPager.OnPageChangeListener{
@BindView(R.id.back_iv)
ImageView backIv;
@BindView(R.id.layout_back)
LinearLayout layoutBack;
@BindView(R.id.title_tv)
TextView titleTv;
@BindView(R.id.right_tv)
TextView rightTv;
@BindView(R.id.layout_right)
LinearLayout layoutRight;
@BindView(R.id.campaign_recyclerview)
LRecyclerView mRecyclerView;
private View mView;
private GridViewForScrollView classflyGridview;
private LinearLayout pointGroup;;
private ConvenientBanner convenientBanner;
private TextView bannerTitleTv;;
private List networkImages = new ArrayList();
private int lastPosition = 0;
private CampaignListAdapter campaignListAdapter;
private ClassflyListAdapter classflyListAdapter;
private IndexBannerList indexBannerList;
private CampaignList campaignList;
private LRecyclerViewAdapter mLRecyclerViewAdapter;
public int pageNum = 1;
public int pageSize = 4;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_index, container,
false);
ButterKnife.bind(this, mView);
initView();
initClassfly();
return mView;
}
private void initView() {
backIv.setVisibility(View.GONE);
titleTv.setText("快知网");
layoutRight.setVisibility(View.INVISIBLE);
campaignListAdapter = new CampaignListAdapter(getActivity());
mLRecyclerViewAdapter = new LRecyclerViewAdapter(campaignListAdapter);
mRecyclerView.setAdapter(mLRecyclerViewAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
mRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
//add a HeaderView
final View header = LayoutInflater.from(getActivity()).inflate(R.layout.index_header,(ViewGroup)mView.findViewById(android.R.i