1. 程式人生 > >RecyclerView的載入顯示多種佈局

RecyclerView的載入顯示多種佈局

RecyclerView是對ListView的封裝,所以ListView上能用的方法對RecyclerView同樣適用,並且會更簡單

在實際開發中,我們可能需要一個列表,顯示多種佈局,getItemViewType()方法完美解決了這個問題,在BaseAdapter中還有getViewTypeCount()這個方法。這裡我們使用RecyclerView.Adapter。只用getItemViewType就可以了。

先來看這個方法
@Override
public int getItemViewType(int position) {
}

引數position代表RecyclerView的位置,而int型的返回值代表了佈局的型別,我們可以用0,1,2…..來表示判斷,例如:
@Override
public int getItemViewType(int position) {
if (position == 0){
return 0;
} else if (position == 1){
return 1;
} else {
return 2;
}
}


我們需要在
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

方法中判斷view的型別。該方法的第二個引數viewType就是getItemViewType返回值的型別,通過判斷值0,1,2…..來根據不同型別載入顯示不同的佈局,例如

<code class="hljs cs has-numbering">@Override
    <span class="hljs-keyword">public</span> ViewHolder <span class="hljs-title">onCreateViewHolder</span>(ViewGroup parent, <span class="hljs-keyword">int</span> viewType) {
        View view = <span class="hljs-keyword">null</span>;
        ViewHolder holder = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">switch</span> (viewType){
            <span class="hljs-keyword">case</span> <span class="hljs-number">0</span>:
                view = LayoutInflater.<span class="hljs-keyword">from</span>(mContext).inflate(android.R.layout.simple_list_item_1, parent, <span class="hljs-keyword">false</span>);
                holder = <span class="hljs-keyword">new</span> ViewHolderOne(view);
                <span class="hljs-keyword">break</span>;
            <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:
                view = LayoutInflater.<span class="hljs-keyword">from</span>(mContext).inflate(android.R.layout.simple_list_item_2, parent, <span class="hljs-keyword">false</span>);
                holder = <span class="hljs-keyword">new</span> ViewHolderTwo(view);
                <span class="hljs-keyword">break</span>;
            <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:
                view = LayoutInflater.<span class="hljs-keyword">from</span>(mContext).inflate(android.R.layout.simple_list_item_2, parent, <span class="hljs-keyword">false</span>);
                holder = <span class="hljs-keyword">new</span> ViewHolderThree(view);
                <span class="hljs-keyword">break</span>;
        }
        <span class="hljs-keyword">return</span> holder;
    }</code><ul class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul><div class="save_code tracking-ad" style="display: none;" data-mod="popu_249"><a target=_blank target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png" alt="" /></a></div><ul class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul>

最後在

<code class="hljs avrasm has-numbering">@Override
    public void onBindViewHolder(final RecyclerView<span class="hljs-preprocessor">.ViewHolder</span> holder, int position) {
        switch (getItemViewType(position)){
            case <span class="hljs-number">0</span>:
                final ViewHolderOne holderOne = (ViewHolderOne) holder<span class="hljs-comment">;</span>
                holderOne<span class="hljs-preprocessor">.text</span>1<span class="hljs-preprocessor">.setText</span>(beans<span class="hljs-preprocessor">.get</span>(position)<span class="hljs-preprocessor">.getTxt</span>())<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
            case <span class="hljs-number">1</span>:
                ViewHolderTwo holderTwo = (ViewHolderTwo) holder<span class="hljs-comment">;</span>
                holderTwo<span class="hljs-preprocessor">.text</span>1<span class="hljs-preprocessor">.setText</span>(beans<span class="hljs-preprocessor">.get</span>(position)<span class="hljs-preprocessor">.getTxt</span>())<span class="hljs-comment">;</span>
                holderTwo<span class="hljs-preprocessor">.text</span>2<span class="hljs-preprocessor">.setText</span>(beans<span class="hljs-preprocessor">.get</span>(position)<span class="hljs-preprocessor">.getTxt</span>())<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
            case <span class="hljs-number">2</span>:
                ViewHolderThree holderTwo = (ViewHolderTwo) holder<span class="hljs-comment">;</span>
                holderThree<span class="hljs-preprocessor">.text</span>1<span class="hljs-preprocessor">.setText</span>(beans<span class="hljs-preprocessor">.get</span>(position)<span class="hljs-preprocessor">.getTxt</span>())<span class="hljs-comment">;</span>
                holderThree<span class="hljs-preprocessor">.text</span>2<span class="hljs-preprocessor">.setText</span>(beans<span class="hljs-preprocessor">.get</span>(position)<span class="hljs-preprocessor">.getTxt</span>())<span class="hljs-comment">;</span>
                <span class="hljs-keyword">break</span><span class="hljs-comment">;</span>
        }
    }</code><ul class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul><div class="save_code tracking-ad" style="display: none;" data-mod="popu_249"><a target=_blank target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png" alt="" /></a></div><ul class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul>

在這裡,我們要定義3個ViewHolder,我們的Adapter繼承RecyclerView.Adapter就可以,以上就是列表顯示多種佈局的處理,靈活運用,舉一反三。比如百度貼吧子評論的顯示更多,就可以這樣處理,通過控制getItemCount和getItemViewType來實現。
點選下載仿百度貼吧子評論載入更多