简单的RecyclerView显示,结合bmob(管理员发布通知例子)

首先在build.gradle添加RecyclerView依赖

implementation 'com.android.support:recyclerview-v7:28.0.0'

设置数据

public class Notification  extends BmobObject{ 
    private String notification;
    private String updateAt_data;
    //省略get,set
}

新建一个notifications.xml 显示通知

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">
    <TextView
        android:id="@+id/tv_notification"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="15sp"
        android:layout_marginTop="3dp"
        />
    <TextView
        android:id="@+id/news_source"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:textColor="#b72a65"
        android:text="管理员"
        />
    <TextView
        android:id="@+id/tv_notification_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text=""/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#cdcdcd" />

</RelativeLayout>

然后在activity绑定的xml,添加

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"                     //
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

在activity里

private RecyclerView mrecyclerView;
private RecyclerView.LayoutManager layoutManager;            //初始化

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Bmob.initialize(this,Info.appid);
    setContentView(R.layout.activity_system_notifications);


    mrecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);   //获取控件
    mrecyclerView.setHasFixedSize(true);         //用来使RecyclerView保持固定的大小

    layoutManager = new LinearLayoutManager(this);
    mrecyclerView.setLayoutManager(layoutManager);            //设置布局方式

    /*bmob获取,并添加到list中*/
    BmobQuery<Notification> query = new BmobQuery<Notification>();
    query.order("-createdAt");
    query.findObjects(new FindListener<Notification>() {
        @Override
        public void done(List<Notification> list, BmobException e) {
            if(e==null){
                List<Notification> notificationList = new ArrayList<>();
                notificationList.addAll(list);          //添加数据到notificationList中                     
                NotificationAdapter nofiticationAdapter = new NotificationAdapter(notificationList);
                mrecyclerView.setAdapter(nofiticationAdapter);      //设置适配器
            }
            else {
                Log.i("bmob获取通知","获取通知失败:"+e.getMessage()+","+e.getErrorCode());
            }
        }
    });
}

重点,适配器的设置,继承RecyclerView.Adapter

 public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.MyViewHolder> {
    List<Notification> notificationList=new ArrayList<>();  //数据集合
    public NotificationAdapter(List<Notification> notificationList){
        this.notificationList=notificationList;             //数据初始化
    }

     //  初始化控件
     class MyViewHolder extends RecyclerView.ViewHolder {

         public TextView tv_notification;
         public TextView tv_notification_time;
         public MyViewHolder(View itemView) {
             super(itemView);
             //获取子布局的控件实例
             tv_notification=(TextView) itemView.findViewById(R.id.tv_notification);
             tv_notification_time=(TextView) itemView.findViewById(R.id.tv_notification_time);

         }
     }
     //ViewHolder就是一个容器,它放你要展示的item里面的控件内容
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View item =LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.notifications,viewGroup,false);   //获取notifications.xml
        MyViewHolder myViewHolder = new MyViewHolder(item);
        return myViewHolder;

    }

     //操作item,为item填充数据了
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final Notification notification = getItem(position);
        holder.tv_notification.setText(notification.getNotification());
        holder.tv_notification_time.setText(notification.getUpdateAt_data());
    }
     protected Notification getItem(int position){
         //获取内容
         return notificationList.get(position);
     }
     

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return notificationList.size();
    }
}

最后显示效果
简单的RecyclerView显示,结合bmob(管理员发布通知例子)