L SDK发布的 , 新API中最有意思的就是 (后面为RV) 和 了,按照官方的说法,RV 是一个 的一个更高级更灵活的一个版本,可以自定义的东西太多了 。以前会不会觉得写一个都觉得挺吃力的 , 但是如果你看过RV的话 , 你就会觉得这也太简单了吧 。废话不多,下面转入正题 。
今天这里主要讲述的是 RV 简单用法:
1.跟 一样 需要一个
2.跟 一样 需要一个
3.有点不同了, 需要一个
详细过程看代码吧:
【Android RecyclerView 的简单使用】package com.jarrah.demo;import java.util.ArrayList;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;public abstract class RecyclerViewActivity extends ActionBarActivity{protected RecyclerView mRecyclerView;protected Adapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mRecyclerView = new RecyclerView(this);setContentView(mRecyclerView);//配置RecyclerView 可以提高执行效率, 前提你要知道有多少不变的item// use this setting to improve performance if you know that changes// in content do not change the layout size of the RecyclerViewmRecyclerView.setHasFixedSize(true);//初始化适配器mAdapter = new Adapter() {@Overridepublic ViewHolder onCreateViewHolder(ViewGroup group,int position) {return newViewHolder(group, position);}};mRecyclerView.setAdapter(mAdapter);//设置一个垂直方向的layout managerint orientation = LinearLayoutManager.VERTICAL;mRecyclerView.setLayoutManager(getLayoutManager(orientation));}protected LinearLayoutManager getLayoutManager(int orientation) {return new LinearLayoutManager(this, orientation, false);}/*** 实例化一个ViewHolder* @param group* @param position* @return*/public abstract ViewHolder newViewHolder(ViewGroup group, int position);public static abstract class Adapter extends RecyclerView.Adapter
下面是demo activity
package com.jarrah.demo;import java.util.ArrayList;import java.util.Arrays;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast;public class DemoActivity extends RecyclerViewActivity{@Overridepublic ViewHolder newViewHolder(ViewGroup group, int position) {final TextView tv = new TextView(this);tv.setPadding(20, 20, 20, 20);tv.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String text = tv.getText().toString();Toast.makeText(DemoActivity.this, text, Toast.LENGTH_SHORT).show();}});ViewHolder viewHolder = new ViewHolder(tv) {@Overridepublic void onBind(String data, View v) {TextView item = (TextView) v;item.setText(data);}};return viewHolder;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//设置数据ArrayList data = http://www.kingceram.com/post/new ArrayList();data.addAll(Arrays.asList(sCheeses));mAdapter.setData(data);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.demo, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {//切换方向if (item.getItemId() == R.id.action_orientation) {LinearLayoutManager lm = (LinearLayoutManager) mRecyclerView.getLayoutManager();int orientation = lm.getOrientation() == 0 ? 1 : 0;mRecyclerView.setLayoutManager(getLayoutManager(orientation));}return super.onOptionsItemSelected(item);}public static final String[] sCheeses = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale","Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese","Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell","Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc","Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss","Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon","Barry's Bay Cheddar", "Basing", "Basket Cheese", "Bath Cheese", "Bavarian Bergkase","Baylough", "Beaufort", "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese","Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir", "Bierkase", "Bishop Kennedy","Blarney", "Bleu d'Auvergne", "Bleu de Gex", "Bleu de Laqueuille","Bleu de Septmoncel", "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore","Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini", "Bocconcini (Australian)","Boeren Leidenkaas", "Bonchester", "Bosworth", "Bougon", "Boule Du Roves","Boulette d'Avesnes", "Boursault", "Boursin", "Bouyssou", "Bra", "Braudostur","Breakfast Cheese", "Brebis du Lavort", "Brebis du Lochois", "Brebis du Puyfaucon","Bresse Bleu", "Brick", "Brie", "Brie de Meaux", "Brie de Melun", "Brillat-Savarin","Brin", "Brin d' Amour", "Brin d'Amour", "Brinza (Burduf Brinza)","Briquette de Brebis", "Briquette du Forez", "Broccio", "Broccio Demi-Affine","Brousse du Rove", "Bruder Basil", "Brusselae Kaas (Fromage de Bruxelles)", "Bryndza","Buchette d'Anjou", "Buffalo", "Burgos", "Butte", "Butterkase", "Button (Innes)","Buxton Blue", "Cabecou", "Caboc", "Cabrales", "Cachaille", "Caciocavallo", "Caciotta","Caerphilly", "Cairnsmore", "Calenzana", "Cambazola", "Camembert de Normandie","Canadian Cheddar", "Canestrato", "Cantal", "Caprice des Dieux", "Capricorn Goat","Capriole Banon", "Carre de l'Est", "Casciotta di Urbino", "Cashel Blue", "Castellano","Castelleno", "Castelmagno", "Castelo Branco", "Castigliano", "Cathelain","Celtic Promise", "Cendre d'Olivet", "Cerney", "Chabichou", "Chabichou du Poitou","Chabis de Gatine", "Chaource", "Charolais", "Chaumes", "Cheddar","Cheddar Clothbound", "Cheshire", "Chevres", "Chevrotin des Aravis", "Chontaleno","Civray", "Coeur de Camembert au Calvados", "Coeur de Chevre", "Colby", "Cold Pack","Comte", "Coolea", "Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper","Cotherstone", "Cotija", "Cottage Cheese", "Cottage Cheese (Australian)","Cougar Gold", "Coulommiers", "Coverdale", "Crayeux de Roncq", "Cream Cheese","Cream Havarti", "Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza","Croghan", "Crottin de Chavignol", "Crottin du Chavignol", "Crowdie", "Crowley","Cuajada", "Curd", "Cure Nantais", "Curworthy", "Cwmtawe Pecorino","Cypress Grove Chevre", "Danablu (Danish Blue)", "Danbo", "Danish Fontina","Daralagjazsky", "Dauphin", "Delice des Fiouves", "Denhany Dorset Drum", "Derby","Dessertnyj Belyj", "Devon Blue", "Devon Garland", "Dolcelatte", "Doolin","Doppelrhamstufel", "Dorset Blue Vinney", "Double Gloucester", "Double Worcester","Dreux a la Feuille", "Dry Jack", "Duddleswell", "Dunbarra", "Dunlop", "Dunsyre Blue","Duroblando", "Durrus", "Dutch Mimolette (Commissiekaas)", "Edam", "Edelpilz","Emental Grand Cru", "Emlett", "Emmental", "Epoisses de Bourgogne", "Esbareich","Esrom", "Etorki", "Evansdale Farmhouse Brie", "Evora De L'Alentejo", "Exmoor Blue","Explorateur", "Feta", "Feta (Australian)", "Figue", "Filetta", "Fin-de-Siecle","Finlandia Swiss", "Finn", "Fiore Sardo", "Fleur du Maquis", "Flor de Guia","Flower Marie", "Folded", "Folded cheese with mint", "Fondant de Brebis","Fontainebleau", "Fontal", "Fontina Val d'Aosta", "Formaggio di capra", "Fougerus","Four Herb Gouda", "Fourme d' Ambert", "Fourme de Haute Loire", "Fourme de Montbrison","Fresh Jack", "Fresh Mozzarella", "Fresh Ricotta", "Fresh Truffles", "Fribourgeois","Friesekaas", "Friesian", "Friesla", "Frinault", "Fromage a Raclette", "Fromage Corse","Fromage de Montagne de Savoie", "Fromage Frais", "Fruit Cream Cheese","Frying Cheese", "Fynbo", "Gabriel", "Galette du Paludier", "Galette Lyonnaise","Galloway Goat's Milk Gems", "Gammelost", "Gaperon a l'Ail", "Garrotxa", "Gastanberra","Geitost", "Gippsland Blue", "Gjetost", "Gloucester", "Golden Cross", "Gorgonzola","Gornyaltajski", "Gospel Green", "Gouda", "Goutu", "Gowrie", "Grabetto", "Graddost","Grafton Village Cheddar", "Grana", "Grana Padano", "Grand Vatel","Grataron d' Areches", "Gratte-Paille", "Graviera", "Greuilh", "Greve","Gris de Lille", "Gruyere", "Gubbeen", "Guerbigny", "Halloumi","Halloumy (Australian)", "Haloumi-Style Cheese", "Harbourne Blue", "Havarti","Heidi Gruyere", "Hereford Hop", "Herrgardsost", "Herriot Farmhouse", "Herve","Hipi Iti", "Hubbardston Blue Cow", "Hushallsost", "Iberico", "Idaho Goatster","Idiazabal", "Il Boschetto al Tartufo", "Ile d'Yeu", "Isle of Mull", "Jarlsberg","Jermi Tortes", "Jibneh Arabieh", "Jindi Brie", "Jubilee Blue", "Juustoleipa","Kadchgall", "Kaseri", "Kashta", "Kefalotyri", "Kenafa", "Kernhem", "Kervella Affine","Kikorangi", "King Island Cape Wickham Brie", "King River Gold", "Klosterkaese","Knockalara", "Kugelkase", "L'Aveyronnais", "L'Ecir de l'Aubrac", "La Taupiniere","La Vache Qui Rit", "Laguiole", "Lairobell", "Lajta", "Lanark Blue", "Lancashire","Langres", "Lappi", "Laruns", "Lavistown", "Le Brin", "Le Fium Orbo", "Le Lacandou","Le Roule", "Leafield", "Lebbene", "Leerdammer", "Leicester", "Leyden", "Limburger","Lincolnshire Poacher", "Lingot Saint Bousquet d'Orb", "Liptauer", "Little Rydings","Livarot", "Llanboidy", "Llanglofan Farmhouse", "Loch Arthur Farmhouse","Loddiswell Avondale", "Longhorn", "Lou Palou", "Lou Pevre", "Lyonnais", "Maasdam","Macconais", "Mahoe Aged Gouda", "Mahon", "Malvern", "Mamirolle", "Manchego","Manouri", "Manur", "Marble Cheddar", "Marbled Cheeses", "Maredsous", "Margotin","Maribo", "Maroilles", "Mascares", "Mascarpone", "Mascarpone (Australian)",};}
运行效果:
文章插图
1)=
2)orientation = horizontal
还有很多高级自定义的东西, 例如, , 里面的重写, 后续陆续给大家更新这方面的资料.
需要代码Demo的朋友可以留下邮箱
文章插图
- Tangram 2.0——猫客页面内组件的动态化方案
- 尿素哪种肥料容易使钙离子流失,尿素的主要成分是什么
- 原神寻找奇怪的圆盘任务怎么做 原神寻找奇怪的圆盘怎么触发开关
- 11月26日丰都县解除2个高风险区的通知 11月26日丰都县解除2个高风险区
- 色味俱全的意思是啥 色味俱全是什么意思
- 琵琶本名枇杷枇杷一词的来源是什么 琵琶本名枇杷一词来源于什么
- 太阳耀斑发生在太阳的哪一个层 太阳耀斑是发生在太阳色球层
- 什么是月球的同步自转
- 我国最早的编年体史书是 我国最早的编年体史书是哪部著作
- 中药材岗梅根的作用与用量 岗梅根的功效与作用及药用价值