編輯:關於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:
它為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有不同,好了看看我們要做的效果吧。
實現圖片文字按鈕的混排。
首先還是看我的工程結構吧。
首先還是貼出我的main_acitivy.xml<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">
然後我們就來看看代碼,首先是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; } }
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; } } }
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); //初始化數據 ListmyDataset = 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); } }
AndroidProgressLayout實現為界面添加圓形進度條。調用setprogress()方法顯示和隱藏進度條在Android的開發中,往往有這種需求,比如一個耗
一、Socket通信簡介Android與服務器的通信方式主要有兩種,一是Http通信,一是Socket通信。兩者的最大差異在於,http連接使用的是“請求&m
Android筆記——自動顯示、隱藏布局的ListView。學習了Android有一段時間了,學習的東西老是遺忘,於是決定寫博客來鞏固知識點,激勵
1WiFiDisplay簡介1.1WiFiDisplay概述WiFiDisplay(WFD)是WiFi聯盟在已有技術的基礎上,為了加速視/音頻的傳輸分享而提出來的一個新概