Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android L新控件RecyclerView詳解與DeMo

android L新控件RecyclerView詳解與DeMo

編輯:關於Android編程

介紹

在谷歌的官網我們可以看到它是這樣介紹的:RecyclerView is a more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically.

RecyclerView比listview更先進更靈活,對於很多的視圖它就是一個容器,可以有效的重用和滾動。當數據動態變化的時候請使用它。

RecyclerView is easy to use, because it provides:

  • A layout manager for positioning items
  • Default animations for common item operations
  • You also have the flexibility to define custom layout managers and animations for this widget. RecyclerView使用起來很方便因為它提供:

    它為item的定位提供一個layoutmanager

    為item的操作提供一個缺省的animations

    您還可以靈活地定義這個小部件的自定義布局管理器和動畫

    To use the RecyclerView widget, you have to specify an adapter and a layout manager. To create an adapter, you extend the RecyclerView.Adapter class. The details of the implementation depend on the specifics of your dataset and the type of views. For more information, see the examples below.

    為了使用RecyclerVIew,你必須指定一個adapter和一個layoutmanager,為了創建一個adapter,你必須得繼承RecyclerView.Adapter,詳細的實現方法取決與你的數據集和你視圖的類型。

    Demo介紹不同於官網

    這裡就介紹完了下面我們就要做自己的Demo了。如果需要看官網的Demo那麼請打開這裡:官方Demo

    這裡既然是詳解那麼就要與官方的Demo有不同,好了看看我們要做的效果吧。\

    實現圖片文字按鈕的混排。

    首先還是看我的工程結構吧。\

    首先還是貼出我的main_acitivy.xml<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;"> 其他幾個xml就不用貼了,很簡單的放了寫TextVIew,ImgeView之類。

    然後我們就來看看代碼,首先是Bean裡面的代碼。

    package com.androidl.bob;
    
    /**
     * 實體包
     * 
     * @author edsheng
     * 
     */
    public class Bean {
    	public static final int Y_TYPE = 0; //view類型0
    	public static final int X_TYPE = 1; //view類型2
    	public static final int Z_TYPE = 2;//view 類型3
    	private int type;
    	private String text;
    
    	public Bean(int type, String text) {
    		super();
    		this.type = type;
    		this.text = text;
    	}
    
    	public int getType() {
    		return type;
    	}
    
    	public void setType(int type) {
    		this.type = type;
    	}
    
    	public String getText() {
    		return text;
    	}
    
    	public void setText(String text) {
    		this.text = text;
    	}
    
    }
    

    然後是Adapter裡面的代碼:

    package com.androidl.bob;
    
    import java.util.List;
    
    import com.example.androidl.R;
    
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.RecyclerView.ViewHolder;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    /**
     * Date : 2014/7/15
     * 
     * @author edsheng
     * 
     */
    public class RecycleAdapter extends RecyclerView.Adapter {
    
    	private List beans;
    
    	public RecycleAdapter(List beans) {
    		super();
    		this.beans = beans;
    	}
    
    	/**
    	 * 內部TextHoler
    	 * 
    	 * @author edsheng
    	 * 
    	 */
    	public class TextHoler extends RecyclerView.ViewHolder {
    		public TextView textView;
    
    		public TextHoler(View textview) {
    			super(textview);
    			this.textView = (TextView) textview.findViewById(R.id.mytext);
    		}
    	}
    
    	/**
    	 * iamgeHolder
    	 * 
    	 * @author edsheng
    	 * 
    	 */
    	public class ImageHoler extends RecyclerView.ViewHolder {
    		public ImageView Imageview;
    
    		public ImageHoler(View textview) {
    			super(textview);
    			this.Imageview = (ImageView) textview.findViewById(R.id.myiamge);
    		}
    	}
    
    	/**
    	 * 按鈕的holder
    	 * 
    	 * @author edsheng
    	 * 
    	 */
    	public class ButtonHolder extends RecyclerView.ViewHolder {
    		public Button button;
    
    		public ButtonHolder(View textview) {
    			super(textview);
    			this.button = (Button) textview.findViewById(R.id.mybutton);
    		}
    	}
    
    	@Override
    	public int getItemCount() {
    		// TODO Auto-generated method stub
    		return beans.size();
    	}
    
    	/**
    	 * 獲取消息的類型
    	 */
    	@Override
    	public int getItemViewType(int position) {
    		// TODO Auto-generated method stub
    		return beans.get(position).getType();
    	}
    
    	/**
    	 * 創建VIewHolder
    	 */
    	@Override
    	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewtype) {
    		// TODO Auto-generated method stub
    		View v = null;
    		ViewHolder holer = null;
    		switch (viewtype) {
    		case Bean.X_TYPE:
    			v = LayoutInflater.from(parent.getContext()).inflate(
    					R.layout.recylce_item_x, null);
    			holer = new TextHoler(v);
    			break;
    		case Bean.Y_TYPE:
    			v = LayoutInflater.from(parent.getContext()).inflate(
    					R.layout.recylce_item_y, null);
    			holer = new ButtonHolder(v);
    			break;
    		case Bean.Z_TYPE:
    			v = LayoutInflater.from(parent.getContext()).inflate(
    					R.layout.recylce_item_z, null);
    			holer = new ImageHoler(v);
    			break;
    		}
    
    		return holer;
    	}
    
    	/**
    	 * 綁定viewholder
    	 */
    	@Override
    	public void onBindViewHolder(ViewHolder holder, int position) {
    		// TODO Auto-generated method stub
    		switch (getItemViewType(position)) {
    		case Bean.X_TYPE:
    			TextHoler textholer = (TextHoler) holder;
    			textholer.textView.setText(beans.get(position).getText());
    			break;
    		case Bean.Y_TYPE:
    			ButtonHolder buttonHolder = (ButtonHolder) holder;
    			buttonHolder.button.setText(beans.get(position).getText());
    			break;
    		case Bean.Z_TYPE:
    			ImageHoler imageHoler = (ImageHoler) holder;
    			// imageHoler.Imageview.setImageResource(android.R.drawable.checkbox_on_background);
    			break;
    		}
    	}
    }
    

    最後是activity的代碼。

    package com.androidl.bob;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    
    import com.example.androidl.R;
    
    public class Mainactivity extends Activity {
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main_activity);
    		 RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    
    //		// improve performance if you know that changes in content
    //		// do not change the size of the RecyclerView
    //		 mRecyclerView.setHasFixedSize(true);
    
    		 //創建布局管理器
    		LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
    		mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    		mRecyclerView.setLayoutManager(mLayoutManager);
    
    		//初始化數據
    		List myDataset = new ArrayList();
    
    		myDataset.add(new Bean(Bean.Z_TYPE, "圖片"));
    		myDataset.add(new Bean(Bean.X_TYPE, "文字"));
    		myDataset.add(new Bean(Bean.Y_TYPE, "按鈕"));
    		myDataset.add(new Bean(Bean.Z_TYPE, "圖片"));
    		myDataset.add(new Bean(Bean.X_TYPE, "shit"));
    		myDataset.add(new Bean(Bean.X_TYPE, "我擦"));
    		myDataset.add(new Bean(Bean.Z_TYPE, "圖片"));
    		myDataset.add(new Bean(Bean.Y_TYPE, "按鈕"));
    		myDataset.add(new Bean(Bean.Y_TYPE, "按鈕"));
    		myDataset.add(new Bean(Bean.X_TYPE, "文字"));
    		//創建Adapter
    		RecycleAdapter mAdapter = new RecycleAdapter(myDataset);
    		mRecyclerView.setAdapter(mAdapter);
    
    	}
    
    }
    

    Demo傳送門:開始傳送

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved