Android项目实战 | 从零开始写app实现app首页智慧服务&热门推荐&热门主题新闻

Posted 李猫er

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android项目实战 | 从零开始写app实现app首页智慧服务&热门推荐&热门主题新闻相关的知识,希望对你有一定的参考价值。

说在前面,由于各种adapter,xml布局,bean实体类,Activity,也为了让看懂,代码基本都是“简单粗暴直接不好看”,没啥okhttp和util工具类之类的封装,本篇幅可能有点长吧,大佬请放过,哈~

菜鸡一枚,写得不好,有问题的请指教~~

本篇实现效果:

RecyclerView+json+okhttp+glide实现服务分类和热门主题推荐,显示,自定义接口实现RecyclerView 子item的点击事件,点击对应的item 进入对应的服务,热门详情页面。直接用简单的Intent 数据传递新闻详情信息。也可用WebView 加载新闻详情。
在这里插入图片描述

文章导航

一、【Android项目实战 | 从零开始写app(一)】 创建项目

二、【Android项目实战 | 从零开始写app(二)】实现闪屏页,启动app

三、【Android项目实战 | 从零开始写app(三)】实现引导页,进入登录or主页面

四、【Android项目实战 | 从零开始写app(四)】Okhttp+Gson实现服务端登录验证功能

五、【Android项目实战 | 从零开始写app(五)】okhttp+gson实现服务端注册功能

六、【Android项目实战 | 从零开始写app(六)】用TabLayout+ViewPager搭建App 框架主页面底部导航栏

七、【Android项目实战 | 从零开始写app(七)】优化主页导航栏,禁用主页页面滑动切换效果

八、【Android项目实战 | 从零开始写app(八)】实现app首页广告轮播图切换和搜索跳转

九、【Android项目实战 | 从零开始写app(九)】Tablayout+ViewPager实现页面分类顶部标题页面联动切换

十、【Android项目实战 | 从零开始写app(十)】Okhttp+glide+json+ListView实现新闻模块数据的填充显示

十一、【Android项目实战 | 从零开始写app(十一)】实现app首页智慧服务页面服务分类数据的解析及点击跳转

十二、【Android项目实战 | 从零开始写app(十二)】实现app首页智慧服务&热门推荐&热门主题、新闻

十三、【Android项目实战 | 从零开始写app(十三)】实现用户中心模块清除token退出登录&信息修改等功能

十四、【Android项目实战 | 从零开始写app(十四)】实现图片发布模块 | 必知必会之调用系统相机拍照、相册

十五、【Android项目实战 | 从零开始写app(教程汇总)】Android 项目实战系列汇总、源代码


功能逻辑实现

首页

在HomeFragment 加入如下代码:

package com.example.myapp.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapp.R;
import com.example.myapp.activity.ApponitmentActivity;
import com.example.myapp.activity.BannerWebView;
import com.example.myapp.activity.BusActivity;
import com.example.myapp.activity.CityStateActivity;
import com.example.myapp.activity.LivingPayActivity;
import com.example.myapp.activity.NewSearchActivity;
import com.example.myapp.activity.NewsWebViewActivity;
import com.example.myapp.activity.ParkActivity;
import com.example.myapp.activity.RecommendWebView;
import com.example.myapp.activity.WeiZhangActivity;
import com.example.myapp.adapter.NewTabAdapter;
import com.example.myapp.adapter.RecommendAdapter;
import com.example.myapp.adapter.RecycleServiceAdapter;
import com.example.myapp.bean.BannerBean;
import com.example.myapp.bean.NewsBean;
import com.example.myapp.bean.RecommendBean;
import com.example.myapp.bean.ServiceBean;
import com.example.myapp.utils.APIConfig;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @ProjectName: MyApp
 * @Package: com.example.myapp.fragment
 * @ClassName: HomeFragment
 * @Description:
 * @Author: liyingxia
 * @CreateDate: 2021/4/13 21:34
 */
public class HomeFragment extends BaseFragment {
    private static final String TAG = HomeFragment.class.getSimpleName();
    private TextView tv_more,tv_theme,tv_theme_title;
    private EditText ed_search;
    private Banner banner;
    private RecyclerView home_recyclerview,home_recyclerview1;
    private RecycleServiceAdapter recycleServiceAdapter;
    private List<ServiceBean.RowsBean> rowsBeanList;
    private List<RecommendBean.RowsDTO> recommendList;
    private RecommendAdapter recommendAdapter;
    private NewsBean newsBean;
    private NewsAdapter newsAdapter;
    private NewTabAdapter newTabAdapter;
    private TabLayout tab_layout;
    private ListView home_listview;
    private ViewPager view_pager;
    private OkHttpClient client = new OkHttpClient();

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==0) {
                ServiceBean serviceBean = (ServiceBean) msg.obj;
                rowsBeanList = serviceBean.getRows();
                recycleServiceAdapter= new RecycleServiceAdapter(getActivity(),rowsBeanList);
                home_recyclerview.setLayoutManager(new GridLayoutManager(getActivity(),5));
                home_recyclerview.setAdapter(recycleServiceAdapter);
                recycleServiceAdapter.setItemClickListener(new RecycleServiceAdapter.MyItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        String url = APIConfig.BASE_URL+"/"+rowsBeanList.get(position).getLink();
                        Intent intent = null;
                        if (position==0){
                            intent = new Intent(getActivity(), CityStateActivity.class);
                        } else if (position==1) {
                            intent = new Intent(getActivity(), BusActivity.class);
                        } else if (position==2) {
                            intent = new Intent(getActivity(), ApponitmentActivity.class);
                        } else if (position==3) {
                            intent = new Intent(getActivity(), LivingPayActivity.class);
                        } else if (position==4) {
                            intent = new Intent(getActivity(), WeiZhangActivity.class);
                        } else if (position==5) {
                            intent = new Intent(getActivity(), ParkActivity.class);
                        }
                        Bundle bundle = new Bundle();
                        bundle.putString("title",rowsBeanList.get(position).getServiceName());
                        bundle.putString("url",url);
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);
                    }
                });
            }
            if (msg.what==1) {
                RecommendBean recommendBean = (RecommendBean) msg.obj;
                recommendList = recommendBean.getRows();
                recommendAdapter = new RecommendAdapter(getActivity(),recommendList);
                home_recyclerview1.setLayoutManager(new GridLayoutManager(getActivity(),2));
                home_recyclerview1.setAdapter(recommendAdapter);
                recommendAdapter.setOnItemClickListener(new RecommendAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position, List<RecommendBean.RowsDTO> list) {
                        String url = APIConfig.BASE_URL+"/"+list.get(position).getLink();
                        Toast.makeText(getContext(),"url详情:"+url,Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(getActivity(), RecommendWebView.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("title",list.get(position).getServiceName());
                        bundle.putString("url",url);
                        intent.putExtras(bundle);
                        getActivity().startActivity(intent);
                    }
                });
            }
        }
    };

    @Override
    public View initView() {
        Log.i(TAG, "首页的视图被初始化了");
        View view = View.inflate(getContext(), R.layout.fragment_home, null);
        ed_search = view.findViewById(R.id.ed_search);
        banner = view.findViewById(R.id.banner);
        home_recyclerview = view.findViewById(R.id.home_recyclerview);
        home_recyclerview1 = view.findViewById(R.id.home_recyclerview1);
        home_listview = view.findViewById(R.id.home_listview);
        tab_layout = view.findViewById(R.id.tab_layout);
        view_pager = view.findViewById(R.id.view_pager);
        tv_more = view.findViewById(R.id.tv_more);
        tv_theme = view.findViewById(R.id.tv_theme);
        tv_theme_title = view.findViewById(R.id.tv_theme_title);

        return view;
    }

    /**
     * 解决ScrollView 导致ListView 只显示一项问题
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView){
        NewsAdapter newsAdapter = (NewsAdapter) listView.getAdapter();
        if (newsAdapter==null){
            return;
        }
        int totalHeight=0;
        // newsAdapter.getCount() 获取返回数据项的数目
        for (int i=0;newsAdapter.getCount()>i;i++){
            View listItem = newsAdapter.getView(i,null,listView);
            // 计算子项View的宽高
            listItem.measure(0,0);
            // 统计所有子项的总高度
            totalHeight+=listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight+(listView.getDividerHeight()*(newsAdapter.getCount()-1));
        //getDividerHeight():获取子项间分隔符占用的高度, params.height 得到整个ListView 完整显示需要的高度
        listView.setLayoutParams(params);
    }

    @Override
    public void initData() {
        super.initData();
        initSearch();
        initBanner();
        getServiceData();
        getRecommendData();
        initNews();
    }

    // 顶部搜索框
    public void initSearch(){
        ed_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i== EditorInfo.IME_ACTION_SEARCH){
                    String search = ed_search.getText().toString();
                    Intent intent = new Intent(getActivity(), NewSearchActivity.class);
                    intent.putExtra("search",search);
                    startActivity(intent);
                    /*HomeFragment fragment = new HomeFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString("search",search);
                    fragment.setArguments(bundle);
                    return fragment;*/
                }
                return false;
            }
        });
        tv_more.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivit

以上是关于Android项目实战 | 从零开始写app实现app首页智慧服务&热门推荐&热门主题新闻的主要内容,如果未能解决你的问题,请参考以下文章

Android项目实战 | 从零开始写app(十三)实现用户中心模块清除token退出登录&信息修改等功能

Android项目实战 | 从零开始写app(14)实现图片发布模块 | 必知必会之调用系统相机拍照相册一一解决android7 打开相机闪退奔溃问题

从零开始搭建架构实施Android项目

《Android Studio开发实战 从零基础到App上线(第3版)》出版后记

《Android Studio开发实战 从零基础到App上线(第3版)》出版后记

《Android Studio开发实战 从零基础到App上线(第3版)》出版后记