一、说在前面
1、今天任务:整改新闻详情页(3h)
2、明天任务:整改新闻首页,用viewpage来区别不同类别的新闻(预计4h)
3、遇到问题:整改评论区是不会实现将用户头像统一设置成圆形!!
二、效果
三、代码
1、newActivity.java
public class NewsActivity extends AppCompatActivity implements View.OnClickListener{ private MyImageView imageView; private MyImageView imgCome; private TextView tvCome; private TextView tvDate; private TextView tvTitle; private WebView zw; private ImageView comment; private ImageView chat; private TextView hide_down; private EditText comment_content; private Button comment_send; private SentUrlTask sentUrlTask; private SentUrlTask_ sentUrlTask_; private SentUrlTask_getComment sentUrlTask_getComment; private LinearLayout rl_enroll; private RelativeLayout rl_comment; private ListView comment_list; private AdapterComment adapterComment; private List<Comment> data; private New_ new_2 = new New_(); private String html; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news); initView(); action(); } private class SentUrlTask extends AsyncTask<Void,Void,String> { private String url; public SentUrlTask(String url) { this.url = url; } @Override protected String doInBackground(Void... voids) { return HttpUtil.setUrlT(url); } @Override protected void onPostExecute(@NonNull String s) { super.onPostExecute(s); html = JsoupNewsUtil.zwHtml(s); zw.loadData(html,"text/html;charset=utf-8",null); } } public void initView(){ imgCome = findViewById(R.id.iv_new_come); imageView = findViewById(R.id.iv_new_dg); tvCome = findViewById(R.id.tv_come); tvDate = findViewById(R.id.tv_date); tvTitle = findViewById(R.id.tv_title); zw = findViewById(R.id.wv_zw); // 初始化评论列表 comment_list = findViewById(R.id.comment_list); // 初始化数据 if (data==null){ data = new ArrayList<>(); } // 初始化适配器 adapterComment = new AdapterComment(getApplicationContext(), data); // 为评论列表设置适配器 comment_list.setAdapter(adapterComment); comment_list.setDividerHeight(0); comment = findViewById(R.id.comment); hide_down = findViewById(R.id.hide_down); comment_content = findViewById(R.id.comment_content); comment_send = findViewById(R.id.comment_send); rl_enroll = findViewById(R.id.rl_enroll); rl_comment = findViewById(R.id.rl_comment); chat = findViewById(R.id.chat); setListener(); } /** * 设置监听 */ public void setListener(){ comment.setOnClickListener(this); hide_down.setOnClickListener(this); comment_send.setOnClickListener(this); chat.setOnClickListener(this); } public void action(){ Intent intent = getIntent(); String news = intent.getStringExtra("news"); Gson gson = new Gson(); New_ news1 = gson.fromJson(news, New_.class); if(news1!=null){ new_2 = news1; getComment(new_2.getId()); tvTitle.setText(new_2.getTitle()); tvDate.setText(new_2.getPtime()); tvCome.setText(new_2.getSource()); sentUrlTask = new SentUrlTask(news1.getUrl()); sentUrlTask.execute(); imageView.setImageURL(news1.getImgsrc()); // imgCome.setImageURL(""); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.comment: // 弹出输入法 InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); // 显示评论框 rl_enroll.setVisibility(View.GONE); rl_comment.setVisibility(View.VISIBLE); break; case R.id.hide_down: // 隐藏评论框 rl_enroll.setVisibility(View.VISIBLE); rl_comment.setVisibility(View.GONE); // 隐藏输入法,然后暂存当前输入框的内容,方便下次使用 InputMethodManager im = (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(comment_content.getWindowToken(), 0); break; case R.id.comment_send: sendComment(); break; case R.id.chat: if(View.GONE==comment_list.getVisibility()){ comment_list.setVisibility(View.VISIBLE); // linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,200)); }else{ comment_list.setVisibility(View.GONE); // linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,60)); } default: break; } } /** * 发送评论 */ public void sendComment() { if (comment_content.getText().toString().equals("")) { Toast.makeText(getApplicationContext(), "评论不能为空!", Toast.LENGTH_SHORT).show(); } else { Comment comment1 = new Comment(); comment1.setContent(comment_content.getText().toString()); comment1.setName("17734103685"); comment1.setNid(new_2.getId()); // sentUrlTask_ = new SentUrlTask_("http://192.168.1.17:8080/NewsApi/news?method=addComment&name="+name+"&content="+content+"&nid="+nid); sentUrlTask_ = new SentUrlTask_(""); sentUrlTask_.setComment(comment1); sentUrlTask_.execute(); // 生成评论数据 Comment comment = new Comment(); comment.setName("17734103685" + ": "); comment.setContent(comment_content.getText().toString()); adapterComment.addComment(comment); // data.add(comment); // 发送完,清空输入框 comment_content.setText(""); Toast.makeText(getApplicationContext(), "评论成功!", Toast.LENGTH_SHORT).show(); // sent("17734103685: ",comment_content.getText().toString(),new_2.getId()); } } public void getComment(int nid){ sentUrlTask_getComment = new SentUrlTask_getComment("http://192.168.1.17:8080/NewsApi/news?method=commentList&nid="+nid); // sentUrlTask = new ListFragment.SentUrlTask("http://192.168.43.243:8080/NewsApi/news?method=allnews"); sentUrlTask_getComment.execute(); } private class SentUrlTask_ extends AsyncTask<Void,Void,String> { private String url; private Comment comment; public void setComment(Comment comment) { this.comment = comment; } public SentUrlTask_ (String url) { this.url = url; } @Override protected String doInBackground(Void... voids) { String con = ""; try { con = URLEncoder.encode(comment.getContent(), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return HttpUtil.setUrl("http://192.168.1.17:8080/NewsApi/news?method=addComment&name="+ comment.getName()+"&content="+con+"&nid="+comment.getNid()); } @Override protected void onPostExecute(@NonNull String s) { super.onPostExecute(s); setListViewHeightBasedOnChildren(comment_list); } } private class SentUrlTask_getComment extends AsyncTask<Void,Void,String> { private String url; public SentUrlTask_getComment(String url) { this.url = url; } @Override protected String doInBackground(Void... voids) { return HttpUtil.setUrl(url); } @Override protected void onPostExecute(@NonNull String s) { super.onPostExecute(s); // zw.loadData(html,"text/html;charset=utf-8",null); Gson gson = new Gson(); // Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); if(!s.equals("")){ data = gson.fromJson(s,new TypeToken<List<Comment>>(){}.getType()); setListViewHeightBasedOnChildren(comment_list); adapterComment.setData(data); } } } public void setListViewHeightBasedOnChildren(ListView listView) { int totalHeight = data.size()*280; ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight; listView.setLayoutParams(params); } }
2、new_activity.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff" tools:context=".NewsActivity"> <ScrollView android:id="@+id/scrollView2" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/relativeLayout" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#00000000" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="鱼越此时海,花开彼岸天!" android:textSize="30dp" android:textColor="#000000"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="horizontal"> <com.me.view.MyImageView android:id="@+id/iv_new_come" android:layout_width="50dp" android:layout_height="50dp" android:scaleType="fitXY" android:src="@drawable/xhs"/> <TextView android:id="@+id/tv_come" android:layout_width="260dp" android:layout_height="wrap_content" android:text="新华社" android:textSize="20dp" android:textColor="#000000"/> <Button android:id="@+id/new_guanzhu" android:layout_width="70dp" android:layout_height="35dp" android:layout_margin="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="10dp" android:background="#F30808" android:text="关注" android:textColor="#ffffff" android:textSize="13sp" /> </LinearLayout> <TextView android:id="@+id/tv_date" android:layout_marginTop="-30dp" android:layout_marginStart="62dp" android:layout_width="200dp" android:layout_height="wrap_content" android:text="2020-04-29 " /> <com.me.view.MyImageView android:id="@+id/iv_new_dg" android:layout_width="match_parent" android:layout_height="150dp" android:layout_marginTop="10dp" android:scaleType="fitXY" /> <WebView android:id="@+id/wv_zw" android:layout_width="match_parent" android:layout_height="wrap_content" tools:ignore="WebViewLayout" /> <ListView android:id="@+id/comment_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" /> </LinearLayout> </ScrollView> <RelativeLayout android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="60dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <LinearLayout android:id="@+id/rl_enroll" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:background="#ffffff" android:orientation="horizontal"> <ImageView android:id="@+id/comment" android:layout_width="32dp" android:layout_height="32dp" android:layout_gravity="center" android:layout_weight="1" android:src="@drawable/comment" /> <ImageView android:id="@+id/iv_sc" android:layout_width="32dp" android:layout_height="32dp" android:layout_gravity="center" android:layout_weight="1" android:src="@drawable/ic_favorite_border_black_24dp" /> <ImageView android:id="@+id/iv_fx" android:layout_width="32dp" android:layout_height="32dp" android:layout_gravity="center" android:layout_weight="1" android:src="@drawable/ic_share_black_24dp" /> <ImageView android:id="@+id/chat" android:layout_width="23dp" android:layout_height="23dp" android:layout_gravity="center" android:layout_weight="1" android:src="@drawable/chat" /> </LinearLayout> <RelativeLayout android:id="@+id/rl_comment" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:background="#ffffff" android:visibility="gone"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#969696" /> <TextView android:id="@+id/hide_down" android:layout_width="wrap_content" android:layout_height="35dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:drawableBottom="@drawable/hide_dowm" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_toRightOf="@id/hide_down" android:background="#969696" /> <EditText android:id="@+id/comment_content" android:layout_width="240dp" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/hide_down" android:background="@null" android:hint="评论" android:singleLine="true" android:textSize="15sp" /> <Button android:id="@+id/comment_send" android:layout_width="50dp" android:layout_height="35dp" android:layout_alignParentRight="true" android:layout_margin="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="10dp" android:background="#E91E63" android:text="发送" android:textColor="#ffffff" android:textSize="13sp" /> </RelativeLayout> </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>