欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android RecyclerView网格布局示例解析

时间:2022-08-23 10:23:47|栏目:Android代码|点击:

一个简单的网格布局

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
  tools:context=".MainActivity">

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

message_main_notice_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="150dp"
  android:gravity="center"
  android:background="@color/colorAccent"
  android:orientation="vertical"
  android:layout_marginTop="5dp"
  >

  <ImageView
    android:id="@+id/iv_image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@mipmap/logo"
    android:gravity="center"></ImageView>

  <LinearLayout
    android:layout_width="60dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dp">

    <TextView
      android:id="@+id/tv_title"
      android:layout_width="match_parent"
      android:layout_height="30dp"
      android:gravity="center"
      android:text="BIBIA"></TextView>
  </LinearLayout>
</LinearLayout>

适配器MyRecyclerViewAdapter.java:

package com.example.administrator.recyclerviewtest;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{
  private List<ItemBean> mList;

  static class ViewHolder extends RecyclerView.ViewHolder{
    View myView;
    ImageView imageView;
    TextView title;
    public ViewHolder(View itemView) {
      super(itemView);
      myView = itemView;
      imageView = (ImageView) itemView.findViewById(R.id.iv_image);
      title = (TextView) itemView.findViewById(R.id.tv_title);
    }
  }

  public MyRecyclerViewAdapter(List<ItemBean> list){
    this.mList = list;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_main_notice_list,null);
    final ViewHolder holder = new ViewHolder(view);
    return holder;
  }

  //将数据绑定到控件上
  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {
    ItemBean bean = mList.get(position);
    holder.imageView.setBackgroundResource(bean.itemImage);
    holder.title.setText(bean.itemTitle);
  }

  @Override
  public int getItemCount() {
    return mList.size();
  }


  //下面两个方法提供给页面刷新和加载时调用
  public void add(List<ItemBean> addMessageList) {
    //增加数据
    int position = mList.size();
    mList.addAll(position, addMessageList);
    notifyItemInserted(position);
  }

  public void refresh(List<ItemBean> newList) {
    //刷新数据
    mList.removeAll(mList);
    mList.addAll(newList);
    notifyDataSetChanged();
  }
}

主方法:

package com.example.administrator.recyclerviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

  private RecyclerView recyclerView;

  private List<ItemBean> list;

  private MyRecyclerViewAdapter myAdapte1r;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = new ArrayList<ItemBean>();
    for ( int i=0; i< 19;i++){
      list.add(new ItemBean(
          R.mipmap.logo,
          "Hello",
          new Date().toString()+""
      ));
    }
    myAdapte1r = new MyRecyclerViewAdapter(list);

    recyclerView = (RecyclerView) findViewById(R.id.message_notice_list_item);

    //纵向线性布局
    //LinearLayoutManager layoutManager = new LinearLayoutManager(this);

    //纵向线性布局
    GridLayoutManager layoutManager = new GridLayoutManager(this,2);

    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(myAdapte1r);
  }
}

效果:

上一篇:Android打包篇:Android Studio将代码打包成jar包教程

栏    目:Android代码

下一篇:没有了

本文标题:Android RecyclerView网格布局示例解析

本文地址:http://www.codeinn.net/misctech/211665.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有