今天进行新闻的列表设计。
在进行新闻列表设计时,首先使用了listview,但考虑到可能设计多个样式的item,因此决定采用RecyclerView。
对于每个item的设计:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cardview="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="120dp" android:orientation="horizontal" android:id="@+id/click" android:padding="10dp"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="100dp" android:layout_gravity="center" cardview:cardCornerRadius="4dp" cardview:cardElevation="4dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="80dp" android:orientation="horizontal"> <ImageView android:id="@+id/iv_portrait" android:layout_width="70dp" android:layout_height="70dp" android:layout_margin="5dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textSize="15sp" android:textStyle="bold" android:text="新闻标题" android:textColor="@color/black"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="时间" android:textSize="10dp" android:textColor="@color/grey2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_author" android:layout_marginTop="5dp" android:layout_marginLeft="10dp" android:text="作者" android:textSize="10dp" android:textColor="@color/grey2"/> <TextView android:id="@+id/tv_place" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginLeft="10dp" android:text="出处" android:textSize="10dp" android:textColor="@color/grey2" /> </LinearLayout> </LinearLayout> </LinearLayout> </androidx.cardview.widget.CardView> </LinearLayout>
RecyclerView使用在Fragment中
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:ignore="MissingClass"> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" /> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </LinearLayout>
RecyclerView的适配器:
package com.example.bowenwang; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import java.util.List; public class RecyclerAdapter2 extends RecyclerView.Adapter<RecyclerAdapter2.Hoder> { List<Bean> mlist; private Context mContext; public RecyclerAdapter2(List<Bean> mlist) { this.mlist = mlist; } @NonNull @Override public Hoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { mContext=parent.getContext(); LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View view=inflater.inflate(R.layout.list_item,parent,false); Hoder hoder=new Hoder(view); return hoder; } @Override public void onBindViewHolder(@NonNull Hoder holder, int position) { holder.title.setText(mlist.get(position).getTitle()); holder.time.setText(mlist.get(position).getTime()); holder.place.setText(mlist.get(position).getPlace()); holder.author.setText(mlist.get(position).getAuthor()); String img=mlist.get(position).getImg(); if(img.equals("无")){ Glide.with(mContext).load("http://n.sinaimg.cn/default/feedbackpics/transform/116/w550h366/20180326/gx3A-fysqfnf9556450.png").into(holder.img); } else { Glide.with(mContext).load(img.trim()).into(holder.img); } int p=position; holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bean bean=new Bean(); bean.setTitle(mlist.get(p).getTitle()); bean.setTime(mlist.get(p).getTime()); bean.setPlace(mlist.get(p).getPlace()); bean.setAuthor(mlist.get(p).getAuthor()); bean.setContent(mlist.get(p).getContent()); Toast.makeText(mContext,bean.getTitle(),Toast.LENGTH_SHORT).show(); Intent iti=new Intent(mContext,DetailActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("bean",bean); iti.putExtras(bundle); mContext.startActivity(iti); } }); } @Override public int getItemCount() { return mlist.size(); } class Hoder extends RecyclerView.ViewHolder{ TextView title; TextView time; TextView author; TextView place; ImageView img; public Hoder(@NonNull View itemView) { super(itemView); title=itemView.findViewById(R.id.tv_title); time=itemView.findViewById(R.id.tv_time); author=itemView.findViewById(R.id.tv_author); place=itemView.findViewById(R.id.tv_place); img=itemView.findViewById(R.id.iv_portrait); } } }
明天进行后端数据的获取