在上一篇: Android项目实战 | 从零开始写app(九)】Tablayout+ViewPager实现页面分类顶部标题页面联动切换 的基础上实现数据的填充展示
由于首页会展示到推荐新闻列表,所以今天先把后台的新闻数据先请求下来,就跳着更吧,后面再继续完善首页~
~这篇早早就写好了,奈何发布了几次老是说审核不通过,说内容违规???? 无可奈何~
一、【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(九)】实现主页底部新闻模块数据的解析
十、【Android项目实战 | 从零开始写app(10)】Okhttp+glide+json+ListView实现新闻模块数据的填充显示
十一、【Android项目实战 | 从零开始写app(11)】实现app首页九宫格服务分类点击跳转
十二、【Android项目实战 | 从零开始写app(12)】实现app首页热门推荐
十三、【Android项目实战 | 从零开始写app(13)】实现服务页面数据的解析
十四、【Android项目实战 | 从零开始写app(14)】实现用户中心模块清除token退出登录&信息修改等功能
十五、【Android项目实战 | 从零开始写app(15)】实现发布模块…
okhttp+gson+glide实现新闻分类模块数据的解析,并显示到ListView上,点击查看新闻详情。数据有限,只写新闻其中一个分类的新闻数据,其他的自己照着写。
修改NewsFragment中的代码如下:
package com.example.myapp.fragment;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
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.NewsWebViewActivity;
import com.example.myapp.adapter.NewTabAdapter;
import com.example.myapp.bean.BannerBean;
import com.example.myapp.bean.NewsBean;
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/18 14:34
*/
public class NewsFragment extends BaseFragment{
private static final String TAG = NewsFragment.class.getSimpleName();
private Banner banner;
private NewsAdapter newsAdapter;
private ViewPager new_viewpager;
private ListView listView;
private NewTabAdapter newTabAdapter;
private TabLayout tabLayout;
private List<NewsBean.RowsBean> newBean;
private List<BannerBean.RowsDTO> bannerList;
@Override
public View initView() {
Log.i(TAG, "新闻的视图被实例化了");
View view = View.inflate(getActivity(), R.layout.fragment_news, null);
findView(view);
return view;
}
private void findView(View view) {
tabLayout = view.findViewById(R.id.tab_layout);
new_viewpager = view.findViewById(R.id.view_pager);
banner = view.findViewById(R.id.banner);
listView = (ListView) view.findViewById(R.id.new_listView);
}
@Override
public void initData() {
super.initData();
Log.e(TAG, "新闻中心的Fragment的数据被初始化了");
initBanner();
initTab();
getHttpData();
}
private void initTab() {
// 新闻分类标题导航栏
String[] title = {
"时政","电视","旅游","视频","广播","基层"};
//创建装载Fragment的列表
List<Fragment> fragmentlist;
// 初始化列表,并把创建的6个Fragment页面添加到列表中
fragmentlist = new ArrayList<>();
fragmentlist.add(new NShizhengFragment());
fragmentlist.add(new NTVFragment());
fragmentlist.add(new NTravelFragment());
fragmentlist.add(new NvideoFragment());
fragmentlist.add(new NbrodcastFragment());
fragmentlist.add(new NJicengFragment());
newTabAdapter = new NewTabAdapter(getChildFragmentManager(),fragmentlist,title);
newsAdapter = new NewsAdapter();
new_viewpager.setAdapter(newTabAdapter); // ViewPager与Adapter绑定
tabLayout.setupWithViewPager(new_viewpager); // TabLayout与ViewPager绑定
}
private void initBanner() {
// Banner轮播图
bannerList = new ArrayList<BannerBean.RowsDTO>();
bannerList.add(new BannerBean.RowsDTO("http://192.93.196.45:8080/profile/home1.png", null));
bannerList.add(new BannerBean.RowsDTO("http://192.93.196.45:8080/profile/home2.png", null));
bannerList.add(new BannerBean.RowsDTO("http://192.93.196.45:8080/profile/home3.png", null));
bannerList.add(new BannerBean.RowsDTO("http://192.93.196.45:8080/profile/home4.png", null));
banner.setAdapter(new BannerImageAdapter<BannerBean.RowsDTO>(bannerList) {
@Override
public void onBindView(BannerImageHolder bannerImageHolder, BannerBean.RowsDTO rowsDTO, int i, int i1) {
//加载图片
Glide.with(getActivity())
.load(rowsDTO.getImgUrl())
.apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
.into(bannerImageHolder.imageView);
}
})
.addBannerLifecycleObserver(this)
.setIndicator(new CircleIndicator(getActivity()))
.setOnBannerListener(new OnBannerListener() {
@Override
public void OnBannerClick(Object o, int i) {
// getIntent(i);
Toast.makeText(getActivity(),"点击了"+i,Toast.LENGTH_LONG).show();
}
});
}
public void getHttpData(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(APIConfig.BASE_URL+"/news/list")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("onFailure",e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String result = response.body().string();
// Log.i("请求成功",result);
Gson gson = new Gson();
NewsBean news = gson.fromJson(result, NewsBean.class);
newBean = news.getRows();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
listView.setAdapter(newsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), NewsWebViewActivity.class);
ImageView new_img = view.findViewById(R.id.new_img); // 通过发送图片
new_img.setDrawingCacheEnabled(Boolean.TRUE);
intent.putExtra("bitmap",new_img.getDrawingCache());
intent.putExtra("title",newBean.get(i).getTitle());
intent.putExtra("time",newBean.get(i).getCreateTime());
intent.putExtra("content",newBean.get(i).getContent());
startActivity(intent);
}
});
}
});
}
}
});
}
/**
* 新闻适配器
*/
public class NewsAdapter extends BaseAdapter {
@Override
public int getCount() {
return newBean!=null ? newBean.size() : 0;
}
@Override
public Object getItem(int i) {
return newBean.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view==null) {
view = View.inflate(getContext(), R.layout.news_item,null);
holder = new ViewHolder();
holder.new_title = view.findViewById(R.id.new_title);
holder.new_context = view.findViewById(R.id.new_context);
holder.new_create = view.findViewById(R.id.new_date);
holder.likeNumber = view.findViewById(R.id.likeNumber);
holder.viewsNumber = view.findViewById(R.id.viewsNumber);
holder.new_img = view.findViewById(R.id.new_img);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.likeNumber.setText(newBean.get(i).getLikeNumber()+"");
holder.viewsNumber.setText(newBean.get(i).getViewsNumber()+"");
holder.new_create.setText(newBean.get(i).getCreateTime());
holder.new_context.setText(newBean.get(i).getContent());
holder.new_title.setText(newBean.get(i).getTitle());
String url = APIConfig.BASE_URL+newBean.get(i).getImgUrl();
Glide.with(getActivity()).load(url).into(holder.new_img);
return view;
}
class ViewHolder{
TextView new_title;
TextView new_context;
TextView new_create;
ImageView new_img;
TextView viewsNumber;
TextView likeNumber;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/news"
android:background="#ECECEC"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="#1D8EE8"
app:tabTextColor="#FFFFFF"
app:tabIndicatorColor="#FAE104"
app:tabSelectedTextColor="#FAE002">
com.google.android.material.tabs.TabLayout>
<com.youth.banner.Banner
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#8DC8F6"
android:id="@+id/banner"/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_margin="10dp"
android:background="@drawable/shape_login_form"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
androidx.viewpager.widget.ViewPager>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000000"
android:id="@+id/new_listView"/>
LinearLayout>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle"
android:useLevel="false">
<solid android:color="#ffffff" />
<corners android:radius="10dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
shape>
item>
layer-list>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@drawable/item_shape"
android:orientation="horizontal">
<ImageView
android:id="@+id/new_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="4dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="8dp"
android:layout_weight="0.6"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:id="@+id/new_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="20dp"
android:textColor="#5B4CB1"
android:text="我是titl"
android:gravity="center_vertical"
android:singleLine="true"/>
<TextView
android:id="@+id/new_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="14dp"
android:layout_gravity="right"
android:text="2020-01-01"
android:layout_marginRight="0dp"
android:gravity="center_vertical"
android:singleLine="true"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="8dp"
android:layout_weight="0.4"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:id="@+id/new_context"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="14dp"
android:text="我是新闻内容。。。。。"
android:textColor="#5B4CB1"
android:gravity="center_vertical"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="22"
android:drawableLeft="@mipmap/like"
android:id="@+id/likeNumber"
android:layout_marginRight="6dp" />
<TextView
android:text="333"
android:drawableLeft="@mipmap/browse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/viewsNumber"/>
LinearLayout>
LinearLayout>
LinearLayout>
LinearLayout>
package com.example.myapp.bean;
import java.util.List;
/**
* @ProjectName: MyApp4
* @Package: com.example.myapp4.bean
* @ClassName: NewsBean
* @Description:
* @Author: liyingxia
* @CreateDate: 2021/4/18 14:14
*/
public class NewsBean {
/**
* total : 24
* rows : [............]
* code : 200
* msg : 查询成功
*/
private int total;
private int code;
private String msg;
private List<RowsBean> rows;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<RowsBean> getRows() {
return rows;
}
public void setRows(List<RowsBean> rows) {
this.rows = rows;
}
public static class RowsBean {
/**
* searchValue : null
* createBy : null
* createTime : 2020-10-12 16:00:09
* updateBy : null
* updateTime : 2020-10-12 16:00:21
* remark : null
* params : {}
* id : 1
* title : .....
* content : ..........。
* imgUrl : /profile/cshi.jpg
* pressCategory : 视频
* isRecommend : 1
* likeNumber : 125
* viewsNumber : 200
* userId : 1
* pressStatus : 0
*/
private Object searchValue;
private Object createBy;
private String createTime;
private Object updateBy;
private String updateTime;
private Object remark;
private ParamsBean params;
private int id;
private String title;
private String content;
private String imgUrl;
private String pressCategory;
private int isRecommend;
private int likeNumber;
private int viewsNumber;
private int userId;
private int pressStatus;
public Object getSearchValue() {
return searchValue;
}
public void setSearchValue(Object searchValue) {
this.searchValue = searchValue;
}
public Object getCreateBy() {
return createBy;
}
public void setCreateBy(Object createBy) {
this.createBy = createBy;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public Object getUpdateBy() {
return updateBy;
}
public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public Object getRemark() {
return remark;
}
public void setRemark(Object remark) {
this.remark = remark;
}
public ParamsBean getParams() {
return params;
}
public void setParams(ParamsBean params) {
this.params = params;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public String getPressCategory() {
return pressCategory;
}
public void setPressCategory(String pressCategory) {
this.pressCategory = pressCategory;
}
public int getIsRecommend() {
return isRecommend;
}
public void setIsRecommend(int isRecommend) {
this.isRecommend = isRecommend;
}
public int getLikeNumber() {
return likeNumber;
}
public void setLikeNumber(int likeNumber) {
this.likeNumber = likeNumber;
}
public int getViewsNumber() {
return viewsNumber;
}
public void setViewsNumber(int viewsNumber) {
this.viewsNumber = viewsNumber;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getPressStatus() {
return pressStatus;
}
public void setPressStatus(int pressStatus) {
this.pressStatus = pressStatus;
}
public static class ParamsBean {
}
public RowsBean(String createTime, String title, String content, String imgUrl, String pressCategory, int likeNumber, int viewsNumber) {
this.createTime = createTime;
this.title = title;
this.content = content;
this.imgUrl = imgUrl;
this.pressCategory = pressCategory;
this.likeNumber = likeNumber;
this.viewsNumber = viewsNumber;
}
@Override
public String toString() {
return "RowsBean{" +
"createTime='" + createTime + '\'' +
", title='" + title + '\'' +
", content='" + content + '\'' +
", imgUrl='" + imgUrl + '\'' +
", pressCategory='" + pressCategory + '\'' +
", likeNumber=" + likeNumber +
", viewsNumber=" + viewsNumber +
'}';
}
}
}
public class ResponseBean {
/**
* msg : 操作成功
* code : 200
*/
private String msg;
private String code;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "ResponseBean{" +
"msg='" + msg + '\'' +
", code=" + code +
'}';
}
}
public class CommentBean {
/**
* total : 29730
* rows : [...........................]
* code : 200
* msg : 查询成功
*/
private int total;
private int code;
private String msg;
private List<RowsDTO> rows;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<RowsDTO> getRows() {
return rows;
}
public void setRows(List<RowsDTO> rows) {
this.rows = rows;
}
public static class RowsDTO {
/**
* searchValue : null
* createBy : null
* createTime : 2020-10-12 17:14:36
* updateBy : null
* updateTime : null
* remark : null
* params : {}
* userId : 2
* pressId : 1
* content : ..........................。
* nickName : ???
* userName : zhangsan
* avatar : /profile/2021/04/03/8fb1c481-9fc5-4a85-a257-5ba3de3cce35.jpg
*/
private String createTime;
private Object updateBy;
private ParamsDTO params;
private String content;
private String nickName;
private String userName;
private String avatar;
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public Object getUpdateBy() {
return updateBy;
}
public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}
public ParamsDTO getParams() {
return params;
}
public void setParams(ParamsDTO params) {
this.params = params;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public static class ParamsDTO {
}
@Override
public String toString() {
return "RowsDTO{" +
"createTime='" + createTime + '\'' +
", content='" + content + '\'' +
", nickName='" + nickName + '\'' +
", userName='" + userName + '\'' +
", avatar='" + avatar + '\'' +
'}';
}
public RowsDTO(String createTime, String content, String nickName, String userName, String avatar) {
this.createTime = createTime;
this.content = content;
this.nickName = nickName;
this.userName = userName;
this.avatar = avatar;
}
}
}
public class NewTabAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private String[] titles;
public NewTabAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList, String[] titles) {
super(fm);
this.fragmentList = fragmentList;
this.titles = titles;
}
/**
* 返回当前的fragment
* @param position: 当前页面的位置
* @return
*/
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
/**
* fragment中的个数
*/
@Override
public int getCount() {
return fragmentList.size();
}
/**
* 返回当前的标题
*/
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
public class CommentBean {
/**
* total : 29730
* rows : [.......]
* code : 200
* msg : 查询成功
*/
private int total;
private int code;
private String msg;
private List<RowsDTO> rows;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<RowsDTO> getRows() {
return rows;
}
public void setRows(List<RowsDTO> rows) {
this.rows = rows;
}
public static class RowsDTO {
/**
* searchValue : null
* createBy : null
* createTime : 2020-10-12 17:14:36
* updateBy : null
* updateTime : null
* remark : null
* params : {}
* userId : 2
* pressId : 1
* content :...........................。
* nickName : ???
* userName : zhangsan
* avatar : /profile/2021/04/03/8fb1c481-9fc5-4a85-a257-5ba3de3cce35.jpg
*/
private String createTime;
private Object updateBy;
private ParamsDTO params;
private String content;
private String nickName;
private String userName;
private String avatar;
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public Object getUpdateBy() {
return updateBy;
}
public void setUpdateBy(Object updateBy) {
this.updateBy = updateBy;
}
public ParamsDTO getParams() {
return params;
}
public void setParams(ParamsDTO params) {
this.params = params;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public static class ParamsDTO {
}
@Override
public String toString() {
return "RowsDTO{" +
"createTime='" + createTime + '\'' +
", content='" + content + '\'' +
", nickName='" + nickName + '\'' +
", userName='" + userName + '\'' +
", avatar='" + avatar + '\'' +
'}';
}
public RowsDTO(String createTime, String content, String nickName, String userName, String avatar) {
this.createTime = createTime;
this.content = content;
this.nickName = nickName;
this.userName = userName;
this.avatar = avatar;
}
}
}
APIConfig
public class APIConfig {
public final static String BASE_URL = "http://192.93.196.45:8080";
}
哈,突然发现写文好费时间啊~ ,我的Springboot 项目还没写完,悲伤了…