Android RecyclerView網(wǎng)格布局示例解析
一個(gè)簡(jiǎn)單的網(wǎng)格布局
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; } //將數(shù)據(jù)綁定到控件上 @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(); } //下面兩個(gè)方法提供給頁(yè)面刷新和加載時(shí)調(diào)用 public void add(List<ItemBean> addMessageList) { //增加數(shù)據(jù) int position = mList.size(); mList.addAll(position, addMessageList); notifyItemInserted(position); } public void refresh(List<ItemBean> newList) { //刷新數(shù)據(jù) 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); } }
效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android RecyclerView實(shí)現(xiàn)多種item布局的方法
- Android RecyclerView顯示Item布局不一致解決辦法
- Android RecyclerView加載不同布局簡(jiǎn)單實(shí)現(xiàn)
- Android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能
- Android RecyclerView布局就這么簡(jiǎn)單
- Android RecyclerView加載兩種布局的方法
- Android列表RecyclerView排列布局
- Android RecyclerView多類型布局卡片解決方案
- Android實(shí)現(xiàn)RecyclerView嵌套流式布局的詳細(xì)過程
相關(guān)文章
Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能
這篇文章主要介紹了Android實(shí)現(xiàn)系統(tǒng)狀態(tài)欄的隱藏和顯示功能,文中還給大家?guī)硭姆N方法,大家可以根據(jù)自己需要參考下2018-07-07Android編程中selector背景選擇器用法實(shí)例分析
這篇文章主要介紹了Android編程中selector背景選擇器用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Selector的結(jié)構(gòu)描述與使用技巧,需要的朋友可以參考下2016-01-01Android中使用七牛云存儲(chǔ)進(jìn)行圖片上傳下載的實(shí)例代碼
這篇文章主要介紹了Android中使用七牛云存儲(chǔ)進(jìn)行圖片上傳下載的實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-08-08Android頂部工具欄和底部工具欄的簡(jiǎn)單實(shí)現(xiàn)代碼
Android頂部工具欄和底部工具欄的簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-05-05Android中截取當(dāng)前屏幕圖片的實(shí)例代碼
該篇文章是說明在Android手機(jī)或平板電腦中如何實(shí)現(xiàn)截取當(dāng)前屏幕的功能,并把截取的屏幕保存到SDCard中的某個(gè)目錄文件夾下面。實(shí)現(xiàn)的代碼如下:2013-08-08Android實(shí)現(xiàn)音量調(diào)節(jié)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)音量調(diào)節(jié)的方法,涉及Android頁(yè)面布局及多媒體播放的設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09ImageView簡(jiǎn)單加載網(wǎng)絡(luò)圖片實(shí)例代碼
使用ImageView實(shí)現(xiàn)簡(jiǎn)單加載網(wǎng)絡(luò)圖片的功能,示例代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-06-06超好看的下拉刷新動(dòng)畫Android代碼實(shí)現(xiàn)
超好看的下拉刷新動(dòng)畫Android代碼實(shí)現(xiàn),效果簡(jiǎn)單大方,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01Android身份證號(hào)有效性校驗(yàn)工具類案例
這篇文章主要介紹了Android身份證號(hào)有效性校驗(yàn)工具類案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09