您的位置:首页 > 其它

RecyclerView的加载显示多种布局

2016-10-05 18:03 260 查看
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来实现。

点击下载仿百度贴吧子评论加载更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: