上回说到,Android实战-智慧城市(2)
软件设计
主页面
主页面是个大工程,首先用ViewPager2+Fragment的方式实现了4页切换,与引导页不同的是每页有一个单独的Fragment页面,底部的四个按钮与四个页面相呼应

新闻内容页
新闻内容页展示新闻内容,当点击了智慧城市页和新闻页的新闻后,即跳转到新闻内容页,展示对应的新闻和评论
修改HomeFragment0.java
修改HomeFragment0.java中的startNewsActivity方法,使点击专题和新闻列表时发声页面跳转
1 2 3 4 5 6 7
| private void startNewsActivity(int id) {
Intent intent = new Intent(getContext(), NewsHomeActivity.class); intent.putExtra("newsId",id); startActivity(intent); }
|
新闻详细内容的反序列化对象
用于将请求到的新闻的详细内容的json反序列化
NewsHomeBean.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
| package com.SmartCity.NewsHome;
public class NewsHomeBean {
private String msg; private int code; private DataBean data;
public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; }
public int getCode() { return code; }
public void setCode(int code) { this.code = code; }
public DataBean getData() { return data; }
public void setData(DataBean data) { this.data = data; }
public static class DataBean { private Object searchValue; private String createBy; private String createTime; private String updateBy; private String updateTime; private Object remark; private ParamsBean params; private int id; private String appType; private String cover; private String title; private Object subTitle; private String content; private String status; private String publishDate; private Object tags; private int commentNum; private int likeNum; private int readNum; private String type; private String top; private String hot;
public Object getSearchValue() { return searchValue; }
public void setSearchValue(Object searchValue) { this.searchValue = searchValue; }
public String getCreateBy() { return createBy; }
public void setCreateBy(String createBy) { this.createBy = createBy; }
public String getCreateTime() { return createTime; }
public void setCreateTime(String createTime) { this.createTime = createTime; }
public String getUpdateBy() { return updateBy; }
public void setUpdateBy(String 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 getAppType() { return appType; }
public void setAppType(String appType) { this.appType = appType; }
public String getCover() { return cover; }
public void setCover(String cover) { this.cover = cover; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public Object getSubTitle() { return subTitle; }
public void setSubTitle(Object subTitle) { this.subTitle = subTitle; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getPublishDate() { return publishDate; }
public void setPublishDate(String publishDate) { this.publishDate = publishDate; }
public Object getTags() { return tags; }
public void setTags(Object tags) { this.tags = tags; }
public int getCommentNum() { return commentNum; }
public void setCommentNum(int commentNum) { this.commentNum = commentNum; }
public int getLikeNum() { return likeNum; }
public void setLikeNum(int likeNum) { this.likeNum = likeNum; }
public int getReadNum() { return readNum; }
public void setReadNum(int readNum) { this.readNum = readNum; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public String getTop() { return top; }
public void setTop(String top) { this.top = top; }
public String getHot() { return hot; }
public void setHot(String hot) { this.hot = hot; }
public static class ParamsBean { } } }
|
新闻评论内容的反序列化对象
用于将请求到的新闻的评论内容的json反序列化
NewCommentBean.java(这里打错了一个字母News打成了New)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| package com.SmartCity.NewsHome;
import java.io.Serializable; import java.util.List;
public class NewCommentBean implements Serializable {
private int total; private List<RowsBean> rows; private int code; private String msg;
public int getTotal() { return total; }
public void setTotal(int total) { this.total = total; }
public List<RowsBean> getRows() { return rows; }
public void setRows(List<RowsBean> rows) { this.rows = rows; }
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 static class RowsBean implements Serializable { private Object searchValue; private Object createBy; private Object createTime; private Object updateBy; private Object updateTime; private Object remark; private ParamsBean params; private int id; private String appType; private int newsId; private String content; private String commentDate; private int userId; private int likeNum; private String userName; private String newsTitle;
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 Object getCreateTime() { return createTime; }
public void setCreateTime(Object createTime) { this.createTime = createTime; }
public Object getUpdateBy() { return updateBy; }
public void setUpdateBy(Object updateBy) { this.updateBy = updateBy; }
public Object getUpdateTime() { return updateTime; }
public void setUpdateTime(Object 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 getAppType() { return appType; }
public void setAppType(String appType) { this.appType = appType; }
public int getNewsId() { return newsId; }
public void setNewsId(int newsId) { this.newsId = newsId; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public String getCommentDate() { return commentDate; }
public void setCommentDate(String commentDate) { this.commentDate = commentDate; }
public int getUserId() { return userId; }
public void setUserId(int userId) { this.userId = userId; }
public int getLikeNum() { return likeNum; }
public void setLikeNum(int likeNum) { this.likeNum = likeNum; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getNewsTitle() { return newsTitle; }
public void setNewsTitle(String newsTitle) { this.newsTitle = newsTitle; }
public static class ParamsBean implements Serializable { } } }
|
显示html内容到Android页面
通过截图中可以看出,请求到的新闻的内容是html格式,通过Html.fromHtml可以将html格式页面显示在TextView中,但是并不能显示图片,如果想使其动态显示图片,就需要重写ImageGetter类,具体实现可以看TextView实现图文混排

创建MyImageGetter类继承ImageGetter类,在该类中重写了getDrawable(String source)方法,当Html.fromHtml解析<img>标签时会回调该方法,方法中的source就是img标签src属性的属性值,当回调该方法时,将source中的图片加载出来并以Drawable格式返回给Html.fromHtml,即可做到图片加载
MyImageGetter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.SmartCity.NewsHome;
import android.content.Context; import android.graphics.drawable.Drawable; import android.graphics.drawable.LevelListDrawable; import android.text.Html; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.Nullable;
import com.SmartCity.SoftData; import com.bumptech.glide.Glide; import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.transition.Transition;
public class MyImageGetter implements Html.ImageGetter { private Context context; private TextView textView;
public MyImageGetter(Context context,TextView textView){ this.context = context; this.textView = textView; }
@Override
public Drawable getDrawable(String source) {
LevelListDrawable levelListDrawable = new LevelListDrawable();
Glide.with(context).asDrawable().load("http://" + SoftData.ipPort + source).into(new CustomTarget<Drawable>() { @Override public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) { levelListDrawable.addLevel(1,1,resource);
levelListDrawable.setBounds(0,0,resource.getIntrinsicWidth(),resource.getMinimumHeight()); levelListDrawable.setLevel(1); textView.invalidate(); textView.setText(textView.getText()); }
@Override public void onLoadCleared(@Nullable Drawable placeholder) {
} });
return levelListDrawable; } }
|
评论列表的item布局
activity_news_home_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingTop="5dp" android:paddingBottom="5dp" xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:src="@mipmap/avatar" android:layout_width="48dp" android:layout_height="48dp"/> <LinearLayout android:orientation="vertical" android:layout_marginRight="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="nickName" android:id="@+id/nickName" android:layout_width="0dp" android:textColor="@color/black" android:textSize="14sp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:text="times" android:id="@+id/times" android:textColor="@color/black" android:textSize="14sp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> </LinearLayout> <TextView android:id="@+id/comments" android:text="comments" android:gravity="left" android:textColor="@color/black" android:layout_marginRight="10dp" android:textStyle="bold" android:textSize="15sp" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> </LinearLayout>
|
评论列表的适配器
一个普通适配器而已
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.SmartCity.NewsHome;
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;
import com.SmartCity.R;
import java.util.List;
public class NewsCommentAdapter extends RecyclerView.Adapter<NewsCommentAdapter.MyViewHolder> { private List<String> commentContents; private List<String> commentUserNames; private List<String> commentDates; private Context context;
public NewsCommentAdapter(List<String> commentContents, List<String> commentUserNames, List<String> commentDates, Context context) { this.commentContents = commentContents; this.commentUserNames = commentUserNames; this.commentDates = commentDates; this.context = context; }
@NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.activity_news_home_item,parent,false); return new MyViewHolder(view); }
@Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.comments.setText(commentContents.get(position)); holder.times.setText(commentDates.get(position)); holder.nickName.setText(commentUserNames.get(position)); }
@Override public int getItemCount() { return commentContents.size(); }
public class MyViewHolder extends RecyclerView.ViewHolder { private TextView nickName,times,comments; public MyViewHolder(@NonNull View itemView) { super(itemView); nickName = itemView.findViewById(R.id.nickName); times = itemView.findViewById(R.id.times); comments = itemView.findViewById(R.id.comments); } } }
|
使最新评论列表绝对禁止滑动
创建MyLinearLayoutManager类继承自LinearLayoutManager,重写它的canScrollVertically()方法,使其永远返回false,调用该类创建的LayoutManager设置为列表的LayoutManager时,该列表将无法被滑动,不会像recyclerView.setNestedScrollingEnabled(false);一样,禁止滑动再滑动时会显示蓝色底边
MyLinearLayoutManager.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.SmartCity.NewsHome;
import android.content.Context; import android.util.AttributeSet;
import androidx.recyclerview.widget.LinearLayoutManager;
public class MyLinearLayoutManager extends LinearLayoutManager { public MyLinearLayoutManager(Context context) { super(context); }
public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); }
public MyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }
@Override public boolean canScrollVertically() {
return false; } }
|
新闻详情页面布局
显示新闻详情
activity_news_home.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:gravity="center_horizontal" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:background="@color/blue" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="58dp"> <LinearLayout android:onClick="finishActivity" android:gravity="center" android:layout_width="58dp" android:layout_height="58dp"> <ImageView android:src="@mipmap/back" android:layout_width="20dp" android:layout_height="20dp"/> </LinearLayout> <LinearLayout android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"> <TextView android:text="新闻详情" android:textStyle="bold" android:textSize="18sp" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="58dp" android:layout_height="58dp">
</LinearLayout> </LinearLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:gravity="center" android:text="title" android:id="@+id/title" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_marginRight="20dp" android:layout_marginLeft="20dp" android:textSize="20sp" android:textColor="@color/black" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <View android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:background="@color/darker_gray" android:layout_width="match_parent" android:layout_height="1dp"/> <TextView android:id="@+id/content" android:textSize="18sp" android:textColor="@color/black" android:textStyle="bold" android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:textColor="@color/black" android:textSize="16sp" android:textStyle="bold" android:text="最新评论" android:gravity="left" android:layout_width="match_parent" android:layout_height="wrap_content"/> <androidx.recyclerview.widget.RecyclerView android:layout_margin="10dp" android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="没有评论\n快来成为第一个吧OvO" android:gravity="center" android:textColor="@color/black" android:textSize="25sp" android:textStyle="bold" android:id="@+id/noComment" android:visibility="gone" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <LinearLayout android:orientation="horizontal" android:layout_marginBottom="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="写评论" android:layout_marginRight="100dp" android:layout_marginLeft="10dp" android:onClick="onClick" android:textColor="@color/blue" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:gravity="right" android:text="全部评论" android:id="@+id/allCommentSum" android:onClick="onClick" android:layout_marginLeft="100dp" android:layout_marginRight="10dp" android:textColor="@color/blue" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>
|
新闻详情程序设计
两个难点:1.Html.fromHtml()加载图片;2.最新评论显示5条
还有一个容易出错的地方:bundle传递对象
NewsHomeActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| package com.SmartCity.NewsHome;
import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.text.Html; import android.view.View; import android.widget.TextView; import android.widget.Toast;
import com.SmartCity.MyHttpRequest; import com.SmartCity.R;
import java.util.ArrayList; import java.util.List;
public class NewsHomeActivity extends AppCompatActivity { private int newsId; private TextView title,content; private MyHttpRequest myHttpRequest = new MyHttpRequest(); private NewsHomeBean newsHomeBean; private Handler handler; private NewCommentBean newCommentBean; private List<String> commentContents; private List<String> commentUserNames; private List<String> commentDates; private RecyclerView recyclerView; private TextView allCommentSum; private TextView noComment; private NewsCommentAdapter newsCommentAdapter;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news_home); myHandler(); init(); getNewsData(); getCommentData(); }
private void myHandler() { handler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if (msg.what == 1){ setNewsData(); } else if (msg.what == 2){ setComment(); } } }; }
private void setNewsData() { if (newsHomeBean == null){ MyToast("新闻内容获取失败: 未知原因"); } else if (newsHomeBean.getCode() != 200){ MyToast("新闻内容获取失败: " + newsHomeBean.getMsg()); } else { String newsTitle = newsHomeBean.getData().getTitle(); String newsContent = newsHomeBean.getData().getContent(); title.setText(newsTitle);
MyImageGetter myImageGetter = new MyImageGetter(NewsHomeActivity.this,content); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { content.setText(Html.fromHtml(newsContent,Html.FROM_HTML_MODE_COMPACT,myImageGetter,null)); } else { content.setText(Html.fromHtml(newsContent,myImageGetter,null)); } } }
@SuppressLint("SetTextI18n") private void setComment() { if (newCommentBean == null){ MyToast("评论内容加载失败: 未知原因"); } else if (newCommentBean.getCode() != 200){ MyToast("评论内容加载失败: " + newCommentBean.getMsg()); } else { allCommentSum.setText("全部评论(" + Math.min(newCommentBean.getTotal(),99) + ")"); if (newCommentBean.getTotal() != 0){
if (newsCommentAdapter == null){ noComment.setVisibility(View.GONE);
MyLinearLayoutManager layoutManager = new MyLinearLayoutManager(this,RecyclerView.VERTICAL,false);
newsCommentAdapter = new NewsCommentAdapter(commentContents,commentUserNames,commentDates,this);
recyclerView.addItemDecoration(new DividerItemDecoration(NewsHomeActivity.this,DividerItemDecoration.VERTICAL)); recyclerView.setAdapter(newsCommentAdapter); recyclerView.setLayoutManager(layoutManager); } else {
newsCommentAdapter.notifyDataSetChanged(); } } else {
noComment.setVisibility(View.VISIBLE); } } }
private void init() { newsId = getIntent().getExtras().getInt("newsId"); title = findViewById(R.id.title); content = findViewById(R.id.content); recyclerView = findViewById(R.id.recyclerView); allCommentSum = findViewById(R.id.allCommentSum); noComment = findViewById(R.id.noComment); }
private void getNewsData() { new Thread(){ @Override public void run() { super.run(); newsHomeBean = (NewsHomeBean) myHttpRequest.myHttp("/prod-api/press/press/" + newsId,null,NewsHomeBean.class,null,"get"); Message message = Message.obtain(); message.what = 1; handler.sendMessage(message); } }.start(); }
private void getCommentData() { new Thread(){ @Override public void run() { super.run(); newCommentBean = (NewCommentBean) myHttpRequest.myHttp("/prod-api/press/comments/list?newsId=" + newsId,null,NewCommentBean.class,null,"get"); if (newCommentBean != null){ int commentSum = Math.min(newCommentBean.getTotal(), 5); setList();
for (int i = 0,j = newCommentBean.getTotal()-1; i < commentSum; i++,j--) { commentContents.add(newCommentBean.getRows().get(j).getContent()); commentUserNames.add(newCommentBean.getRows().get(j).getUserName()); commentDates.add(newCommentBean.getRows().get(j).getCommentDate()); } } Message message = Message.obtain(); message.what = 2; handler.sendMessage(message); } }.start(); }
private void setList() { if (commentContents != null && commentUserNames != null && commentDates != null){ commentContents.clear(); commentUserNames.clear(); commentDates.clear(); } else { commentContents = new ArrayList<>(); commentUserNames = new ArrayList<>(); commentDates = new ArrayList<>(); } }
public void onClick(View view) { Intent intent = new Intent(NewsHomeActivity.this,CommentActivity.class); Bundle bundle = new Bundle(); bundle.putInt("newsId",newsId); bundle.putSerializable("newCommentBean", newCommentBean); intent.putExtras(bundle); startActivityForResult(intent,1); }
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1){ getCommentData(); } }
private void MyToast(String message){ Toast.makeText(this,message,Toast.LENGTH_SHORT).show(); }
public void finishActivity(View view) { finish(); } }
|
全部评论页面布局
当点击写评论或全部评论时就会跳转到这一页
activity_comment.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:gravity="center_horizontal" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:background="@color/blue" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="58dp"> <LinearLayout android:onClick="finishActivity" android:gravity="center" android:layout_width="58dp" android:layout_height="58dp"> <ImageView android:src="@mipmap/back" android:layout_width="20dp" android:layout_height="20dp"/> </LinearLayout> <LinearLayout android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"> <TextView android:text="全部评论" android:textStyle="bold" android:textSize="18sp" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="58dp" android:layout_height="58dp">
</LinearLayout> </LinearLayout> <LinearLayout android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_weight="1" />
<TextView android:text="没有评论\n快来成为第一个吧OvO" android:gravity="center" android:textColor="@color/black" android:textSize="25sp" android:textStyle="bold" android:id="@+id/noComment" android:visibility="gone" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="match_parent"/>
<LinearLayout android:gravity="center_vertical" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="48dp"> <EditText android:hint="请输入评论内容" android:paddingLeft="10dp" android:paddingRight="10dp" android:textSize="16sp" android:id="@+id/edit_comment" android:background="@drawable/edit_text_style" android:layout_weight="1" android:layout_width="0dp" android:layout_marginLeft="10dp" android:layout_height="38dp"/> <Button android:text="发送" android:textStyle="bold" android:textColor="@color/white" android:textSize="15sp" android:id="@+id/sendComment" android:background="@drawable/button_style2" android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:layout_width="58dp" android:layout_height="38dp"/> </LinearLayout> </LinearLayout> </LinearLayout>
|
全部评论程序设计
全部评论页面和新闻详情页面中的评论只有两个不同的地方:1.一个加载全部评论,一个加载部分评论;2.一个可以写评论,一个不可以写评论
CommentActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| package com.SmartCity.NewsHome;
import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;
import com.SmartCity.Bean; import com.SmartCity.MyHttpRequest; import com.SmartCity.R; import com.SmartCity.SoftData;
import java.util.ArrayList; import java.util.List;
public class CommentActivity extends AppCompatActivity { private int newsId; private NewCommentBean newCommentBean; private Button sendComment; private EditText edit_comment; private RecyclerView recyclerView; private MyHttpRequest myHttpRequest = new MyHttpRequest(); private Handler handler; private List<String> commentContents; private List<String> commentUserNames; private List<String> commentDates; private NewsCommentAdapter newsCommentAdapter; private TextView noComment; private Bean bean;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_comment); myHandler(); init(); submit(); getData(); setComment(); }
private void myHandler() { handler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if (msg.what == 1){ setComment(); } else if (msg.what == 2){ commentResult(); } } }; }
private void setComment() { if (newCommentBean == null){ MyToast("评论内容加载失败: 未知原因"); } else if (newCommentBean.getCode() != 200){ MyToast("评论内容加载失败: " + newCommentBean.getMsg()); } else {
if (newCommentBean.getTotal() != 0){
if (newsCommentAdapter == null){ noComment.setVisibility(View.GONE); recyclerView.setVisibility(View.VISIBLE); LinearLayoutManager layoutManager = new LinearLayoutManager(this,RecyclerView.VERTICAL,false); newsCommentAdapter = new NewsCommentAdapter(commentContents,commentUserNames,commentDates,this);
recyclerView.addItemDecoration(new DividerItemDecoration(CommentActivity.this,DividerItemDecoration.VERTICAL)); recyclerView.setAdapter(newsCommentAdapter); recyclerView.setLayoutManager(layoutManager); } else { newsCommentAdapter.notifyDataSetChanged(); } } else {
noComment.setVisibility(View.VISIBLE); recyclerView.setVisibility(View.GONE); } } }
private void commentResult() { if (bean == null){ MyToast("评论失败: 未知原因"); } else if (bean.getCode() != 200){ MyToast("评论失败: " + bean.getMsg()); } else { MyToast("评论发表成功"); getCommentData(); edit_comment.setText(""); } }
private void init() { sendComment = findViewById(R.id.sendComment); edit_comment = findViewById(R.id.edit_comment); recyclerView = findViewById(R.id.recyclerView); newsId = getIntent().getExtras().getInt("newsId"); noComment = findViewById(R.id.noComment); newCommentBean = (NewCommentBean) getIntent().getExtras().getSerializable("newCommentBean"); }
private void submit() {
sendComment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String string = edit_comment.getText().toString(); if (TextUtils.isEmpty(string)){ MyToast("评论内容不可为空"); } else {
sendCommentContent(string); } } }); }
private void sendCommentContent(String string) { new Thread(){ @Override public void run() { super.run(); String json = "{\n" + "\"newsId\": " + newsId + ",\n" + "\"content\": \"" + string + "\"\n" + "}"; bean = (Bean) myHttpRequest.myHttp("/prod-api/press/pressComment",json,Bean.class, SoftData.token,"post"); Message message = Message.obtain(); message.what = 2; handler.sendMessage(message); } }.start(); }
private void getData() { setList(); for (int i = 0,j = newCommentBean.getTotal()-1; i < newCommentBean.getTotal(); i++,j--) {
commentContents.add(newCommentBean.getRows().get(j).getContent()); commentUserNames.add(newCommentBean.getRows().get(j).getUserName()); commentDates.add(newCommentBean.getRows().get(j).getCommentDate()); } }
private void getCommentData() { new Thread(){ @Override public void run() { super.run(); newCommentBean = (NewCommentBean) myHttpRequest.myHttp("/prod-api/press/comments/list?newsId=" + newsId,null,NewCommentBean.class,null,"get"); if (newCommentBean != null){ setList(); for (int i = 0,j = newCommentBean.getTotal()-1; i < newCommentBean.getTotal(); i++,j--) {
commentContents.add(newCommentBean.getRows().get(j).getContent()); commentUserNames.add(newCommentBean.getRows().get(j).getUserName()); commentDates.add(newCommentBean.getRows().get(j).getCommentDate()); } } Message message = Message.obtain(); message.what = 1; handler.sendMessage(message); } }.start(); }
private void setList() { if (commentContents != null && commentUserNames != null && commentDates != null){ commentContents.clear(); commentUserNames.clear(); commentDates.clear(); } else { commentContents = new ArrayList<>(); commentUserNames = new ArrayList<>(); commentDates = new ArrayList<>(); } }
private void MyToast(String message){ Toast.makeText(this,message,Toast.LENGTH_SHORT).show(); }
public void finishActivity(View view) { finish(); } }
|
现在主页面已经全部搞定了,我这里“主页面”的设计范围是根据题目要求定的,下面这些功能都实现了,所以我说主页面全部搞定了

从下一篇开始就是“个人中心页”的开发。欲知后事如何,且听下回分解