欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

RecyclerView的使用之多種Item加載布局

 更新時(shí)間:2016年03月09日 10:08:06   作者:李濟(jì)洲  
本文給大家介石介紹下如何利用RecyclerView實(shí)現(xiàn)多Item布局的加載,多Item布局的加載的意思就是在開(kāi)發(fā)過(guò)程中List的每一項(xiàng)可能根據(jù)需求的不同會(huì)加載不同的Layout

本文給大家介石介紹下如何利用RecyclerView實(shí)現(xiàn)多Item布局的加載,多Item布局的加載的意思就是在開(kāi)發(fā)過(guò)程中List的每一項(xiàng)可能根據(jù)需求的不同會(huì)加載不同的Layout。

下面給大家展示下演示效果圖:

 這里寫(xiě)圖片描述

* 圖片資源版權(quán)歸屬于Facebook dribbble

RecyclerView實(shí)現(xiàn)加載不同的Layout的核心就是在A(yíng)dapter的onCreateViewHolder里面去根據(jù)需求而加載不同的布局。

具體的實(shí)現(xiàn)步驟:(以Android Studio作為開(kāi)發(fā)工具)

1:Gradle配置 build.gradle

這里cardview也是一種新的布局容器,上一篇有介紹。

compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.1.1'

2:建立列表的布局 activity_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_list"
/>
</LinearLayout>

由于需要多種item Layout的加載,我們需要建立2個(gè)item布局

3:建立列表Item項(xiàng)的布局(1) item1.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="@+id/cv_item"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#ffffff"
card_view:cardElevation="4dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_item1_pic"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_weight="1"
android:background="@mipmap/lighthouse"
/>
<TextView
android:id="@+id/tv_item1_text"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>

4:建立列表Item項(xiàng)的布局(2) item2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#E040FB"
card_view:cardElevation="4dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_item2_text"
android:padding="20dp"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>

*最重要的部分 Adapter

5:建立RecyclerView的Adapter,RecyclerViewAdapter.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by Lijizhou on 2016/2/21.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private LayoutInflater mLayoutInflater;
private Context context;
private String [] titles;
//建立枚舉 2個(gè)item 類(lèi)型
public enum ITEM_TYPE {
ITEM1,
ITEM2
}
public RecyclerViewAdapter(Context context,String[] titles){
this.titles = titles;
this.context = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//加載Item View的時(shí)候根據(jù)不同TYPE加載不同的布局
if (viewType == ITEM_TYPE.ITEM1.ordinal()) {
return new Item1ViewHolder(mLayoutInflater.inflate(R.layout.item1, parent, false));
} else {
return new Item2ViewHolder(mLayoutInflater.inflate(R.layout.item2, parent, false));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof Item1ViewHolder) {
((Item1ViewHolder) holder).mTextView.setText(titles[position]);
} else if (holder instanceof Item2ViewHolder) {
((Item2ViewHolder) holder).mTextView.setText(titles[position]);
}
}
//設(shè)置ITEM類(lèi)型,可以自由發(fā)揮,這里設(shè)置item position單數(shù)顯示item1 偶數(shù)顯示item2
@Override
public int getItemViewType(int position) {
//Enum類(lèi)提供了一個(gè)ordinal()方法,返回枚舉類(lèi)型的序數(shù),這里ITEM_TYPE.ITEM1.ordinal()代表0, ITEM_TYPE.ITEM2.ordinal()代表1
return position % 2 == 0 ? ITEM_TYPE.ITEM1.ordinal() : ITEM_TYPE.ITEM2.ordinal();
}
@Override
public int getItemCount() {
return titles == null ? 0 : titles.length;
}
//item1 的ViewHolder
public static class Item1ViewHolder extends RecyclerView.ViewHolder{
TextView mTextView;
public Item1ViewHolder(View itemView) {
super(itemView);
mTextView=(TextView)itemView.findViewById(R.id.tv_item1_text);
}
}
//item2 的ViewHolder
public static class Item2ViewHolder extends RecyclerView.ViewHolder{
TextView mTextView;
public Item2ViewHolder(View itemView) {
super(itemView);
mTextView=(TextView)itemView.findViewById(R.id.tv_item2_text);
}
}
}

OK,Adapter建立好了,那么最后一步就是在A(yíng)ctivity里面進(jìn)行相關(guān)操作

6:列表頁(yè)面的類(lèi)文件 RecyclerViewActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
/**
* Created by Lijizhou on 2016/2/21.
*/
public class RecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
//item 顯示所需(僅供DEMO)
private String[] title = {"Blog : http://blog.csdn.net/Leejizhou.",
"A good laugh and a long sleep are the best cures in the doctor's book.",
"all or nothing, now or never ",
"Be nice to people on the way up, because you'll need them on your way down.",
"Be confident with yourself and stop worrying what other people think. Do what's best for your future happiness!",
"Blessed is he whose fame does not outshine his truth.",
"Create good memories today, so that you can have a good past"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);
mRecyclerView=(RecyclerView)findViewById(R.id.rv_list);
//這里根據(jù)上一個(gè)頁(yè)面的傳入值來(lái)加載LIST或GRID,上一個(gè)頁(yè)面僅僅2個(gè)按鈕,參考演示DEMO
if (getIntent().getIntExtra("type", 0) == 1){
//List
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
}else if(getIntent().getIntExtra("type", 0) == 2){
//grid
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
}
//RecyclerView設(shè)置Adapter
mRecyclerView.setAdapter(new RecyclerViewAdapter(this, title));
}
}

Ok,這樣RecyclerView的多Item布局的加載就實(shí)現(xiàn),關(guān)于RecyclerView的使用之多種Item加載布局就給大家介紹這么多,希望對(duì)大家有所幫助!

相關(guān)文章

  • Android通過(guò)BLE傳輸文件遇到問(wèn)題解決

    Android通過(guò)BLE傳輸文件遇到問(wèn)題解決

    這篇文章主要為大家介紹了Android通過(guò)BLE傳輸文件遇到問(wèn)題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Android編程實(shí)現(xiàn)通知欄進(jìn)度條效果的方法示例

    Android編程實(shí)現(xiàn)通知欄進(jìn)度條效果的方法示例

    這篇文章主要介紹了Android編程實(shí)現(xiàn)通知欄進(jìn)度條效果的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android通知欄進(jìn)度條效果的功能、布局相關(guān)實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2018-02-02
  • Android實(shí)現(xiàn)自定義華麗的水波紋效果

    Android實(shí)現(xiàn)自定義華麗的水波紋效果

    關(guān)于A(yíng)ndroid的水波紋效果小編之前給大家也分享幾篇類(lèi)似的,有興趣可通過(guò)下面的相關(guān)文章進(jìn)行查看,今天給大家再分享一個(gè)華麗的水波紋效果,這個(gè)效果很不錯(cuò),感興趣的可以參考借鑒。
    2016-08-08
  • Android檢測(cè)url地址是否可達(dá)的兩種方法

    Android檢測(cè)url地址是否可達(dá)的兩種方法

    今天小編就為大家分享一篇Android檢測(cè)url地址是否可達(dá)的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Android編程中selector背景選擇器用法實(shí)例分析

    Android編程中selector背景選擇器用法實(shí)例分析

    這篇文章主要介紹了Android編程中selector背景選擇器用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Selector的結(jié)構(gòu)描述與使用技巧,需要的朋友可以參考下
    2016-01-01
  • Android封裝實(shí)現(xiàn)短信驗(yàn)證碼的獲取倒計(jì)時(shí)

    Android封裝實(shí)現(xiàn)短信驗(yàn)證碼的獲取倒計(jì)時(shí)

    這篇文章主要介紹了Android封裝實(shí)現(xiàn)短信驗(yàn)證碼的獲取倒計(jì)時(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-03-03
  • Android ListView的OnItemClickListener詳解

    Android ListView的OnItemClickListener詳解

    這篇文章主要介紹了Android ListView的OnItemClickListener詳解的相關(guān)資料,涉及到OnItemClickListener的position和id參數(shù)做詳細(xì)的解釋的知識(shí)點(diǎn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2016-07-07
  • android通過(guò)Location API顯示地址信息的實(shí)現(xiàn)方法

    android通過(guò)Location API顯示地址信息的實(shí)現(xiàn)方法

    這篇文章主要介紹了android通過(guò)Location API顯示地址信息的方法,涉及Android操作Geocoder類(lèi)獲取地址信息的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • Android自定義控件實(shí)現(xiàn)不規(guī)則區(qū)域點(diǎn)擊事件

    Android自定義控件實(shí)現(xiàn)不規(guī)則區(qū)域點(diǎn)擊事件

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)不規(guī)則區(qū)域點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android開(kāi)發(fā)之TableLayout表格布局

    Android開(kāi)發(fā)之TableLayout表格布局

    這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)之TableLayout表格布局,表格布局模型是以行列的形式管理子控件,對(duì)TableLayout表格布局感興趣的小伙伴們可以參考一下
    2016-03-03

最新評(píng)論