<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                由Recyclerview制作的火車車次信息查詢結果頁是 ![](https://box.kancloud.cn/97a4a44d0e4ef4c3704ce9ccec92c504_1228x1920.png =360x540) recyclerview通常能實現以下效果: **特效1:上下滑動特效(經典上下list列表)** ![](images/A55A1557-8003-41F5-9886-F414A2F76961.png) **特效2:左右滑動特效(經典的手指觸摸水平滑動列表)** ![](images/AEDADFC2-9350-4EC1-9F18-878E15F0D00A.png) #### **一、簡單的RecyclerView使用方法** 本節所示示例是一個最簡單的使用方法,在接下來幾節中將會介紹更多RecyclerView的別的一些屌爆的用法。作者用的環境是Android Studio 0.8.6。 1、添加依賴 在AS的build.gradle中添加依賴,然后同步一下就可以引入依賴包: dependencies { ... compile 'com.android.support:recyclerview-v7:21.0.+' } 2、編寫代碼 添加完依賴之后,就開始寫代碼了,與ListView用法類似,也是先在xml布局文件中創建一個RecyclerView的布局: <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/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"/> </RelativeLayout> 創建完布局之后在MainActivity中獲取這個RecyclerView,并聲明LayoutManager與Adapter,代碼如下: mRecyclerView = (RecyclerView)findViewById(R.id.my_recycler_view); //創建默認的線性LayoutManager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); //如果可以確定每個item的高度是固定的,設置這個選項可以提高性能 mRecyclerView.setHasFixedSize(true); //創建并設置Adapter mAdapter = newMyAdapter(getDummyDatas()); mRecyclerView.setAdapter(mAdapter); 接下來的問題就是Adapter的創建: public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { public String[] datas = null; public MyAdapter(String[] datas) { this.datas = datas; } //創建新View,被LayoutManager所調用 @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false); ViewHolder vh = new ViewHolder(view); return vh; } //將數據與界面進行綁定的操作 @Override public void onBindViewHolder(ViewHolder viewHolder, int position) { viewHolder.mTextView.setText(datas[position]); } //獲取數據的數量 @Override public int getItemCount() { return datas.length; } //自定義的ViewHolder,持有每個Item的的所有界面元素 public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(View view){ super(view); mTextView = (TextView) view.findViewById(R.id.text); } } } 3、運行 寫完這些代碼這個例子既可以跑起來了。從例子也可以看出來,RecyclerView的用法并不比ListView復雜,反而更靈活好用,它將數據、排列方式、數據的展示方式都分割開來,因此可定制型,自定義的形式也非常多,非常靈活。 橫向布局 如果想要一個橫向的List只要設置LinearLayoutManager如下就行,注意要聲明mLayoutManager的類型是LinearLayoutManager而不是父類LayoutManager: mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); Grid布局 如果想要一個Grid布局的列表,只要聲明LayoutManager為GridLayoutManager即可: mLayoutManager = new GridLayoutManager(context,columNum); mRecyclerView.setLayoutManager(mLayoutManager); 注意,在Grid布局中也可以設置列表的Orientation屬性,來實現橫向和縱向的Grid布局。 瀑布流布局 瀑布流就使用StaggeredGridLayoutManager吧,具體方法與上面類似,就不做介紹啦。 總結 本節介紹的是一個最最簡單的RecyclerView的使用方法,
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看