安卓开发-用Adapter绑定数据到RecyclerView进行显示

内容纲要

用Adapter绑定数据到RecyclerView进行显示

导入依赖库

//RecyclerView,注意与项目的appcompat版本一致
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha01'

添加RecyclerView到xml布局

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

添加自定义ViewHolder的xml文件

这里采用卡片式视图,并调整magin值美化显示效果

post_item结构

  • post_cdv CardView
  • post_title TextView
  • post_hint TextView
  • post_image SmartImageView

post_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/post_cdv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="7dp"
    android:layout_marginBottom="8dp"
    app:cardCornerRadius="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="13dp">
        <com.loopj.android.image.SmartImageView
            android:id="@+id/post_image"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="5dp" />

        <TextView
            android:id="@+id/post_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/post_image"
            android:textColor="#000"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/post_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:textSize="20dp"
            android:visibility="gone" />
    </RelativeLayout>
</androidx.cardview.widget.CardView>

添加Adapter绑定数据和视图

/**
 * RecyclerView 的适配器
 * 用于显示最新的专栏文章
 */
public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.PostViewHolder> {
    private List<PostsInfo.StoriesBean> mPostList;

    static class PostViewHolder extends RecyclerView.ViewHolder{
        CardView post_cdv;
        TextView post_title;
        SmartImageView post_image;
        TextView post_hint;

        public PostViewHolder(View view){
            super(view);
            post_cdv = (CardView)view.findViewById(R.id.post_cdv);  //View的主体即为CardView
            post_title = (TextView)view.findViewById(R.id.post_title);
            post_image = (SmartImageView) view.findViewById(R.id.post_image);
            post_hint = (TextView)view.findViewById(R.id.post_hint);
        }
    }
    public PostsAdapter(List<PostsInfo.StoriesBean> _postList){
        mPostList = _postList;
    }
    public void setAdapter(List<PostsInfo.StoriesBean> _postList){
        mPostList = _postList;
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(mContext == null){
            mContext = parent.getContext();
        }
        //将xml转换成view
        View view = LayoutInflater.from(mContext).inflate(R.layout.post_item,parent,false);
        //将view构建为PostViewHolder
        final PostViewHolder vh = new PostViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        PostsInfo.StoriesBean post = mPostList.get(position);
        if(post.getImage_hue() != null){    //当图片不为空时
            //设置控件的可见属性
            holder.post_hint.setVisibility(View.GONE);  //出于美观原因,设置为隐藏
            holder.post_title.setVisibility(View.VISIBLE);
            holder.post_image.setVisibility(View.VISIBLE);
            //设置标题和图片
            holder.post_title.setText(post.getTitle());
            holder.post_image.setImageUrl(post.getImages().get(0)); //显示图片
            Log.i("myInfo","RecyClerView Receive image"+post.getImage_hue());
            //出于美观原因,不显示hint
            //holder.post_hint.setText(post.getHint());
        }else{                              //当图片为空时
            //设置控件的可见属性
            holder.post_title.setVisibility(View.GONE);
            holder.post_image.setVisibility(View.GONE);
            holder.post_hint.setVisibility(View.VISIBLE);
            //设置标题和图片
            holder.post_hint.setText(post.getTitle());
        }
    }

    @Override
    public int getItemCount() {
        return mPostList == null ? 0 : mPostList.size();
    }
}

在RecyclerView上显示列表

创建对象

//RecyclerView显示列表
RecyclerView recyclerView;
PostsAdapter postsAdapter;
RecyclerView.LayoutManager layoutManager;

更新视图

private void initView() {
    //将接口数据转换为RecyclerView的列表
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    //设置布局管理器
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    //设置Adapter的传入数据
    postsAdapter = new PostsAdapter(postsData);
    recyclerView.setAdapter(postsAdapter);
}

发表评论