本篇文章给大家分享的是有关利用Android制作一个APP首页,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
首页展示
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;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<String> networkImages = new ArrayList<String>(); 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.id.content), false); classflyGridview = (GridViewForScrollView) header.findViewById(R.id.classfly_gridview); classflyGridview.setFocusable(false); pointGroup = (LinearLayout) header.findViewById(R.id.point_group); convenientBanner = (ConvenientBanner) header.findViewById(R.id.convenientBanner); bannerTitleTv = (TextView) header.findViewById(R.id.banner_title_tv); mLRecyclerViewAdapter.addHeaderView(header); mRecyclerView.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { pageNum = 1; doRequest(pageNum, true); requestBannerList(); } }); mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore() { pageNum++; doRequest(pageNum, false); } }); //设置头部加载颜色 mRecyclerView.setHeaderViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white); //设置底部加载颜色 mRecyclerView.setFooterViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white); //设置底部加载文字提示 mRecyclerView.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊,点击再试一次吧"); mRecyclerView.setRefreshing(true);// mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {// @Override// public void onItemClick(View view, int position) {//// }//// });//// mLRecyclerViewAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {// @Override// public void onItemLongClick(View view, int position) {//// }// }); } private void initClassfly() { List<Classfly> list = new ArrayList<Classfly>(); Classfly c1 = new Classfly("3", "通知公告", R.mipmap.classfly_bg1); Classfly c2 = new Classfly("4", "教育学习", R.mipmap.classfly_bg2); Classfly c3 = new Classfly("5", "偶像明星", R.mipmap.classfly_bg3); Classfly c4 = new Classfly("6", "特价优惠", R.mipmap.classfly_bg4); Classfly c5 = new Classfly("7", "线下活动", R.mipmap.classfly_bg5); Classfly c6 = new Classfly("8", "其它", R.mipmap.classfly_bg6); list.add(c1); list.add(c2); list.add(c3); list.add(c4); list.add(c5); list.add(c6); classflyListAdapter = new ClassflyListAdapter(getActivity()); classflyGridview.setAdapter(classflyListAdapter); classflyListAdapter.setItems(list); classflyGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); } private void doRequest(int page, final boolean isRefresh) { OkGo.post(Api.GET_CAMPAIGN_LIST) .tag(this) .params("currentPage", String.valueOf(page)) .params("pageSize", String.valueOf(pageSize)) .execute(new LoadingDialogCallback(getActivity()) { @Override public void onSuccess(String s, Call call, Response response) { if(isRefresh){ mRecyclerView.refreshComplete(); }else { mRecyclerView.loadMoreComplete(); } try { campaignList = (CampaignList) ReflectUtil .copy(CampaignList.class, new JSONObject(s)); } catch (Exception e) { e.printStackTrace(); } if (campaignList != null) { String result = campaignList.getResult(); if (Api.SUCCESS.equals(result)) { int j = campaignList.getData().size(); if (isRefresh) { campaignListAdapter.clear(); } if (j != pageSize) { mRecyclerView.setNoMore(true); } campaignListAdapter.addAll(campaignList.getData()); }else{ ToastUtil.show(getActivity(), campaignList.getMsg()); } } } @Override public void onError(Call call, Response response, Exception e) { super.onError(call, response, e); if(isRefresh){ mRecyclerView.refreshComplete(); }else { mRecyclerView.loadMoreComplete(); } KuaiZhiApplication.showResultToast(getActivity(), call, e); } }); } private void requestBannerList() { OkGo.post(Api.GET_INDEX_BANNER_LIST) .tag(this) .cacheKey("cache_index_banner_list") .execute(new LoadingDialogCallback(getActivity()) { @Override public void onCacheSuccess(String s, Call call) { super.onCacheSuccess(s, call); analysisIndexBannerRequest(s); } @Override public void onSuccess(String s, Call call, Response response) { analysisIndexBannerRequest(s); } @Override public void onError(Call call, Response response, Exception e) { super.onError(call, response, e); KuaiZhiApplication.showResultToast(getActivity(), call, e); } }); } private void analysisIndexBannerRequest(String s){ try { indexBannerList = (IndexBannerList) ReflectUtil .copy(IndexBannerList.class, new JSONObject(s)); } catch (Exception e) { e.printStackTrace(); } if (indexBannerList != null) { String result = indexBannerList.getResult(); if (Api.SUCCESS.equals(result)) { networkImages.clear(); for (IndexBanner ib : indexBannerList.getData()) { networkImages.add(ib.getBanner_url()); } setRecommendInfo(); initBanner(); }else{ ToastUtil.show(getActivity(), indexBannerList.getMsg()); } } } private void initBanner() { convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() { @Override public NetworkImageHolderView createHolder() { return new NetworkImageHolderView(); } }, networkImages) .setOnPageChangeListener(this);//监听翻页事件 convenientBanner.setPageTransformer(new AccordionTransformer()); convenientBanner.startTurning(3000); convenientBanner.setOnItemClickListener(new com.bigkoo.convenientbanner.listener.OnItemClickListener() { @Override public void onItemClick(int position) { } }); } private void setRecommendInfo() { pointGroup.removeAllViews(); for (int i = 0; i < indexBannerList.getData().size(); i++) { // 添加指示点 ImageView point = new ImageView(getActivity()); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); if (i != indexBannerList.getData().size() - 1) { params.rightMargin = 10; } point.setLayoutParams(params); point.setBackgroundResource(R.drawable.point_bg); if (i == 0) { point.setEnabled(true); } else { point.setEnabled(false); } pointGroup.addView(point); } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { // 改变指示点的状态 // 把当前点enbale 为true if (indexBannerList != null && indexBannerList.getData() != null && indexBannerList.getData().size() > 0) { bannerTitleTv.setText(indexBannerList.getData().get(position).getBanner_title()); } pointGroup.getChildAt(position).setEnabled(true); // 把上一个点设为false pointGroup.getChildAt(lastPosition).setEnabled(false); lastPosition = position; } @Override public void onPageScrollStateChanged(int state) { }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1142
183.71 KB下载数642
644.84 KB下载数2755
相关文章
发现更多好内容猜你喜欢
AI推送时光机利用Android制作一个APP首页
后端开发2023-05-31
利用Android怎么制作一个APP登录界面
后端开发2023-05-31
利用Android制作一个商品详情页
后端开发2023-05-31
利用Flutter制作一个摸鱼桌面版App
后端开发2024-04-02
Android 利用OpenCV制作人脸检测APP
后端开发2024-04-02
如何利用Flutter制作一个摸鱼桌面版App
后端开发2023-06-22
Android Studio制作一个简单的计算器APP
后端开发2023-10-03
Android手把手教大家制作APP首页(下拉刷新、自动加载)
后端开发2022-06-06
Android如何利用OpenCV制作人脸检测APP
后端开发2023-06-21
Android怎么制作一个app顶部menu菜单栏
后端开发2023-05-31
如何利用Github免费制作app介绍主页
后端开发2023-06-19
在android应用中利用WebView制作一个进度条
后端开发2023-05-31
怎么在android中利用recyclerview制作一个聊天界面
后端开发2023-05-31
怎么在Android中利用extView制作一个跑马灯效果
后端开发2023-05-31
利用Android怎么绘制一个太极图
后端开发2023-05-31
如何使用FrontPage制作一个网页
后端开发2023-06-08
使用Android制作一个欢迎界面
后端开发2023-05-31
如何用html制作一个动画网页
后端开发2024-04-02
使用Three.js制作一个3D奖牌页面
后端开发2024-04-02
CentOS中怎么利用FPM制作一个RPM包
后端开发2023-06-10
咦!没有更多了?去看看其它编程学习网 内容吧