RecyclerView詳細了解

關于RecyclerView大家都不陌生了,它的使用也越來越受歡迎,現在總體了解一下RecyclerView的作用,為什么會有RecyclerView呢,我用ListView也能干所有的事情啊,尺有所短,寸有所長,先來看看RecyclerView的優點吧
  1. 可以快速實現gallery 效果。
  2. 可以快速實現瀑布流效果。
  3. 可以方便地為Item添加動畫效果。

好吧,看到這些對RecyclerView的強大應該有一些認識了吧,再看看使用RecyclerView會遇到哪些麻煩

1.  沒有為Item提供點擊事件。
2.  沒有為Item提供分割線。

先來實現一個簡單的RecyclerView–

在grade里引入support包

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:+'

MainActivity

public class MainActivity extends AppCompatActivity {private RecyclerView mRecyclerView;private List<String> mListData;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initData();mRecyclerView = (RecyclerView) findViewById(R.id.rl_list);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));mRecyclerView.setAdapter(new RecyclerAdapter(this,mListData));}private void initData() {mListData = new ArrayList<String>();for (int i = 0; i < 100; i++) {mListData.add("我是第" + i + "行");}}}

RecyclerAdapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.TestViewHolder> {private Context mContext;private List<String> mList;public RecyclerAdapter(Context context, List<String> list) {mContext = context;mList = list;}@Overridepublic TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {TestViewHolder holder = new TestViewHolder(LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item_layout, parent, false));return holder;}@Overridepublic void onBindViewHolder(TestViewHolder holder, int position) {holder.tv.setText(mList.get(position));}@Overridepublic int getItemCount() {return mList.size();}class TestViewHolder extends RecyclerView.ViewHolder {
;        TextView tv;public TestViewHolder(View view) {super(view);tv = (TextView) view.findViewById(R.id.tv_number);}}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.farmlink.myrecyclerview.MainActivity"><android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/rl_list" />
</RelativeLayout>

看一下效果圖
這里寫圖片描述

沒有分割線很糟糕,不過RecycleView 給你提供了繪制分割線的方法

public class DividerItemDecoration extends RecyclerView.ItemDecoration {private static final int[] ATTRS = new int[]{android.R.attr.listDivider};public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;public static  final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;private Drawable mDivider;private int mOrientation;public DividerItemDecoration(Context context, int orientation) {final TypedArray a = context.obtainStyledAttributes(ATTRS);mDivider = a.getDrawable(0);a.recycle();setOrientation(orientation);}public void setOrientation(int orientation) {if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {throw new IllegalArgumentException("invalid orientation");}mOrientation = orientation;}@Overridepublic void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {super.onDrawOver(c, parent, state);if (mOrientation == VERTICAL_LIST) {drawVertical(c, parent);} else {drawHorizontal(c, parent);}}public void drawVertical(Canvas c, RecyclerView parent) {final int left = parent.getPaddingLeft();final int right = parent.getWidth() - parent.getPaddingRight();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i ++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)child.getLayoutParams();final int top = child.getBottom() + layoutParams.bottomMargin;final int bottom = top + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}public void drawHorizontal(Canvas c, RecyclerView parent) {final int top = parent.getPaddingTop();final int bottom = parent.getHeight() - parent.getPaddingBottom();final int childCount = parent.getChildCount();for (int i = 0; i < childCount; i++) {final View child = parent.getChildAt(i);final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();final int left = child.getRight() + params.rightMargin;final int right = left + mDivider.getIntrinsicHeight();mDivider.setBounds(left, top, right, bottom);mDivider.draw(c);}}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {super.getItemOffsets(outRect, view, parent, state);if (mOrientation == VERTICAL_LIST) {outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());} else {outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);}}
}

加上這句就ok了

mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

這里寫圖片描述
關于RecyclerView 先了解到這里。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389157.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389157.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389157.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

案例與案例之間的非常規排版

In 1929 the Cond Nast publishing group brought Russian-born Mehemed Fehmy Agha—who had been working for the German edition of Vogue magazine—to America as art director for House & Garden, Vanity Fair, and the senior edition of Vogue.1929年&#xff0c…

熊貓分發_熊貓新手:第二部分

熊貓分發This article is a continuation of a previous article which kick-started the journey to learning Python for data analysis. You can check out the previous article here: Pandas for Newbies: An Introduction Part I.本文是上一篇文章的延續&#xff0c;該文…

淺析微信支付:申請退款、退款回調接口、查詢退款

本文是【淺析微信支付】系列文章的第八篇&#xff0c;主要講解商戶如何處理微信申請退款、退款回調、查詢退款接口&#xff0c;其中有一些坑的地方&#xff0c;會著重強調。 淺析微信支付系列已經更新七篇了喲&#xff5e;&#xff0c;沒有看過的朋友們可以看一下哦。 淺析微信…

view工作原理-計算視圖大小的過程(onMeasure)

view的視圖有兩種情況&#xff1a; 內容型視圖&#xff1a;由視圖的內容決定其大小。圖形型視圖&#xff1a;父視圖為view動態調整大小。 ### measure的本質 把視圖布局使用的“相對值”轉化成具體值的過程&#xff0c;即把WRAP_CONTENT,MATCH_PARENT轉化為具體的值。 measur…

[轉載]使用.net 2003中的ngen.exe編譯.net程序

ngen.exe程序為.net 2003自帶&#xff0c; 在&#xff1a;/windows/microsoft.net/framework/v1.1.4322目錄下ngen.exe ngen能把.net框架的東西編譯成機器碼.... 網友&#xff1a;Visual Studio .NET 2003程序的運行速度怎么樣&#xff0c;我有一個感覺&#xff0c;V…

基于Redis實現分布式鎖實戰

背景在很多互聯網產品應用中&#xff0c;有些場景需要加鎖處理&#xff0c;比如&#xff1a;秒殺&#xff0c;全局遞增ID&#xff0c;樓層生成等等。大部分的解決方案是基于DB實現的&#xff0c;Redis為單進程單線程模式&#xff0c;采用隊列模式將并發訪問變成串行訪問&#x…

數據分析 績效_如何在績效改善中使用數據分析

數據分析 績效Imagine you need to do a bank transaction, but the website is so slow. The page takes so much time to load, all you can see is a blue circle.想象您需要進行銀行交易&#xff0c;但是網站是如此緩慢。 該頁面需要花費很多時間來加載&#xff0c;您只能看…

隱私策略_隱私圖標

隱私策略During its 2020 Worldwide Developers Conference, Apple spent time on one of today’s hottest topics — privacy. During the past couple of years, Apple has been rolling out various public campaigns aiming to position itself as a company that respect…

Java中的數組

一、數組的定義type[] arrayName;type arrayName[]; 推薦第一種 二、數組的初始化 含義&#xff1a;所謂的初始化&#xff0c;就是為數組的數組元素分配內存空間&#xff0c;并為每個數組元素賦初始值 &#xff08;1&#xff09;靜態初始化&#xff1a;arrayName new type[…

您一直在尋找5+個簡單的一線工具來提升Python可視化效果

Insightful and aesthetic visualizations don’t have to be a pain to create. This article will prevent 5 simple one-liners you can add to your code to increase its style and informational value.富有洞察力和美學的可視化不必費心創建。 本文將防止您添加到代碼中…

用C#編寫的代碼經C#編譯器后,并非生成本地代碼而是生成托管代碼

用C#編寫的代碼經C#編譯器后&#xff0c;并非生成本地代碼而是生成托管代碼。也就是說&#xff0c;程序集在打包時是連同CLR一起打包的。在客戶端的機器上&#xff0c;CLR一行行的讀取IL&#xff0c;在讀取每行IL時&#xff0c;CLR利用JIT編譯器將IL編譯成本地的CPU指令。若要節…

figma 安裝插件_彩色濾光片Figma插件,用于色盲

figma 安裝插件So as a UX Designer, it is important to design with disabilities in mind. One of these is color blindness. It is important to make sure important information on your product is legible to everyone. This is why I like using this tool:因此&…

服務器運維

1.服務器和網站漏洞檢測&#xff0c;對Web漏洞、弱口令、潛在的惡意行為、違法信息等進行定期掃描&#xff1b;代碼的定期檢查&#xff0c;漏洞檢查及服務器安全加固 2.服務器數據備份&#xff0c;包括網站程序文件備份&#xff0c;數據庫文件備份、配置文件備份&#xff0c;如…

產品觀念:更好的捕鼠器_故事很重要:為什么您需要成為更好的講故事的人

產品觀念&#xff1a;更好的捕鼠器重點 (Top highlight)Telling a compelling story helps you get your point across effectively else you get lost in translation.講一個引人入勝的故事可以幫助您有效地傳達觀點&#xff0c;否則您會迷失在翻譯中。 Great stories happen…

7月15號day7總結

今天復習了springMVC的框架搭建。 思維導圖&#xff1a; 轉載于:https://www.cnblogs.com/kangy123/p/9315919.html

關于注意力的問題

問題&#xff1a;一旦持續的注意力分散和精力無法集中成為習慣性動作&#xff0c;這將成為一個嚴重的問題。 實質&#xff1a;加強有意識的集中程度和持續時間&#xff0c;盡量避免無意識注意對大腦的干擾。 不要浪費注意力。大腦以天為周期&#xff0c;每天注意力是有限的。T…

設計師的10種范式轉變

For $250, a business can pay a graphic designer to create a logo for their business. Or, for $10,000 a business can hire a graphic designer to form a design strategy that contextually places the business’s branding in a stronghold against the market it’s…

面向Tableau開發人員的Python簡要介紹(第2部分)

用PYTHON探索數據 (EXPLORING DATA WITH PYTHON) And we’re back! Let’s pick up where we left off in the first article of this series and use the visual we built there as a starting point.我們回來了&#xff01; 讓我們從在本系列的第一篇文章中停下來的地方開始&…

GAC中的所有的Assembly都會存放在系統目錄%winroot%/assembly下面

是的&#xff0c;GAC中的所有的Assembly都會存放在系統目錄"%winroot%/assembly下面。放在系統目錄下的好處之一是可以讓系統管理員通過用戶權限來控制Assembly的訪問。 關于GAC本身&#xff0c;上面redcaff_l所引述的一段話正是MSDN中對GAC的定義。GAC全稱是Global A…

Mysql(三) Mysq慢查詢日志

Mysql Slow Query Log MYSQL慢查詢日志是用來記錄執行時間超過指定時間的查詢語句。通過慢查詢日志&#xff0c;可以查找出哪些查詢語句的執行效率很低&#xff0c;以便進行優化。一般建議開啟&#xff0c;它對服務器性能的影響微乎其微&#xff0c;但是可以記錄mysql服務器上執…