Android 5.0 RecyclerView 使用,androidrecyclerview

來源:互聯網
上載者:User

Android 5.0 RecyclerView 使用,androidrecyclerview

RecyclerView 是5.0開始出來的新的ListView,主要是提高了效能,顯示方式也多樣化。

老版本中我們最佳化view都是通過定義一個Holder來實行的,現在的RecyclerView就封裝了一個Holder

支援水平滾動和垂直滾動2種列表

還支援Grid格子布局和亂序的布局


ICON + TEXT

public class Item {    public int imgId;    public String desc;    public Item(String desc, int imgId) {        this.desc = desc;        this.imgId = imgId;    }}

簡單的Holder

public class MyViewHolder extends RecyclerView.ViewHolder {    public MyViewHolder(View itemView) {        super(itemView);    }}


public class MyRecyclerAdapter extends RecyclerView.Adapter<MyViewHolder> {    private Activity activity;    private List<Item> list;    public MyRecyclerAdapter (Activity act, List<Item> list) {        this.activity = act;        this.list = list;    }    @Override  //綁定一個UI作為Holder 提高效能    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View v = LayoutInflater.from(activity).inflate(R.layout.recy_item,null);        MyViewHolder holder = new MyViewHolder(v);        return holder;    }    //設定資料    @Override    public void onBindViewHolder(MyViewHolder holder, int position) {        Item item = list.get(position);        TextView text1 = (TextView) holder.itemView.findViewById(R.id.text) ;        text1.setText(item.desc);        ImageView img = (ImageView) holder.itemView.findViewById(R.id.img);        img.setImageResource(item.imgId);    }    @Override    public int getItemCount() {        return list.size();    }}

recy_item.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:orientation="horizontal"    >    <ImageView        android:layout_width="70dp"        android:layout_height="70dp"        android:id = "@+id/img"        android:scaleType="fitCenter"        android:layout_marginLeft="2dp"        android:layout_gravity="center_vertical"        />     <TextView         android:id="@+id/text"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginLeft="3dp"         android:layout_marginTop="5dp"         android:textColor="#336699"         android:textSize="18sp"         /></LinearLayout>



public class MainActivity extends ActionBarActivity {    private List<Item> itemList;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        String title = "To travel hopefully is a better thing than to arrive";  //    String title = "";        itemList = new ArrayList<Item>(5);        itemList.add(new Item(title,R.drawable.cat0));        itemList.add(new Item(title,R.drawable.cat0));        itemList.add(new Item(title,R.drawable.cat2));        itemList.add(new Item(title,R.drawable.cat3));        itemList.add(new Item(title,R.drawable.cat4));        itemList.add(new Item(title,R.drawable.cat4));        itemList.add(new Item(title,R.drawable.cat3));        itemList.add(new Item(title,R.drawable.cat2));        itemList.add(new Item(title,R.drawable.cat3));        itemList.add(new Item(title,R.drawable.cat2));        itemList.add(new Item(title,R.drawable.cat4));        itemList.add(new Item(title,R.drawable.cat4));        itemList.add(new Item(title,R.drawable.cat4));        itemList.add(new Item(title,R.drawable.cat0));        itemList.add(new Item(title,R.drawable.cat3));        RecyclerView list = (RecyclerView) findViewById(R.id.listview);    //    LinearLayoutManager mg = new LinearLayoutManager(this);        //水平或垂直擺放,可以不用 HorizontalScrollView   //     mg.setOrientation(LinearLayoutManager.HORIZONTAL);       GridLayoutManager mg = new GridLayoutManager(this,3);//格子擺放     //交錯性的擺放,有點win8那種格子風格,最好使用CardView作為item,有邊框和圓角    //    StaggeredGridLayoutManager mg = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);       list.setLayoutManager(mg);        MyRecyclerAdapter adapter = new MyRecyclerAdapter(this, itemList);        list.setAdapter(adapter);    } }

<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"    tools:context=".MainActivity">    <android.support.v7.widget.RecyclerView        android:id = "@+id/listview"        android:layout_width="fill_parent"        android:layout_height = "fill_parent"        /></RelativeLayout>

幾種運行情況:

正常的List View



水平滾動



格子情況

GridLayoutManager mg = new GridLayoutManager(this,3); //3列



混合交錯的。我們的圖片需要動態大小,才能看到效果。把文字注釋掉,只顯示圖片。

StaggeredGridLayoutManager mg = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

    public void onBindViewHolder(MyViewHolder holder, int position) {        Item item = list.get(position);        TextView text1 = (TextView) holder.itemView.findViewById(R.id.text) ;        text1.setText(item.desc);        ImageView img = (ImageView) holder.itemView.findViewById(R.id.img);        int ww = 60 + (int)(position * new Random().nextInt(100));        if (ww > 300) {            ww = 200;//每個圖片太寬不好看,計算一個恰當值        }        img.getLayoutParams().width = ww;        img.getLayoutParams().height = 50 + (int)(position * new Random().nextInt(50));//隨機高寬        img.setImageResource(item.imgId);    }


交錯的這種 內容大小是不固定的,所以建議用CardView 可以有邊框效果


這個示範較為簡單,基本用法就這些。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.