Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android5.0(lollipop)新特性介紹(一)

Android5.0(lollipop)新特性介紹(一)

編輯:關於Android編程

今年6月的Google I/O大會上,Android L的初次見面我相信讓會讓很多android粉絲有些小激動和小期待,當然作為開發者的我來說,激動不言而喻,畢竟這是自08年以來改變最大的一個版本。新的設計語言(Material Design),5000多個新增api。廢話不多說,今天要說的基本都是在Android5.0中很常見,也算是對自己學習的一種記錄。

1.CardView

顧名思義,CardView 卡片視圖,繼承自framelayout,可以通過設置圓角以及陰影來展示帶有像卡片一樣的效果

 

\

用法:在布局聲明CardView(注意:需要引入android-support-v7-cardview.jar)


            
在Activity中通過調用mCardView.setRadius(progress)與mCardView.setElevation(progress)方法來分別設置CardView的圓角和陰影。

 

 

2.RecyclerView

\

RecyclerView的出現可以替代ListView和GridView,它標准化了ViewHolder,之前我們在寫ListView的Adapter時需要自定義ViewHolder來提升ListView速度。使用RecyclerView的時候必須要設置LayoutManager,它會通知系統以什麼樣的布局來展示RecyclerView,目前系統提供了2種LayoutManager,LinearLayoutManager和GridLayoutManager對應著線性和格子,當然了也可以自定義LayoutManager來滿足各種需求

用法:布局文件中聲明RecyclerView(注意:需要引入android-support-v7-recyclerview.jar)


    
        
        
    

    

 

 

在Activity中調用如下代碼

 

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
//類似listview
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//類似grdiview
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
mAdapter = new CustomAdapter(mDataset);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);

 

CustomAdapter代碼如下

 

public class CustomAdapter extends RecyclerView.Adapter {
    private static final String TAG = CustomAdapter;

    private String[] mDataSet;


    /**
     * Provide a reference to the type of views that you are using (custom ViewHolder)
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;

        public ViewHolder(View v) {
            super(v);
            // Define click listener for the ViewHolder's View.
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            textView = (TextView) v.findViewById(R.id.textView);
        }

        public TextView getTextView() {
            return textView;
        }
    }


    /**
     * Initialize the dataset of the Adapter.
     *
     * @param dataSet String[] containing the data to populate views to be used by RecyclerView.
     */
    public CustomAdapter(String[] dataSet) {
        mDataSet = dataSet;
    }


    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view.
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.text_row_item, viewGroup, false);

        return new ViewHolder(v);
    }



    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        // Get element from your dataset at this position and replace the contents of the view
        // with that element
        viewHolder.getTextView().setText(mDataSet[position]);
        viewHolder.getTextView().setPadding(0, position, 0, 0);
    }


    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataSet.length;
    }
}

今天先介紹到這裡,後續還會介紹FloatActionBar,觸摸反饋,Activity過場動畫等。

 

 

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