欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现腾讯新闻的新闻类别导航效果

时间:2022-04-15 09:30:13|栏目:Android代码|点击:

效果图如下所示:

 1、在Adapter中加入如下代码

<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'OCR A Std';font-size:10.2pt;"><span style="color:#cc7832;">private int </span><span style="color:#9876aa;">clickTemp </span>= -<span style="color:#6897bb;">1</span><span style="color:#cc7832;">;</span><span style="color:#808080;">//</span><span style="color:#a8c023;"><em>TODO </em></span><span style="color:#a8c023;font-family:'宋体';"><em>被选择的</em></span><span style="color:#a8c023;"><em>item</em></span><span style="color:#a8c023;font-family:'宋体';"><em>下标</em></span></pre><p></p> 
<pre style="background-color:#2b2b2b; color:#a9b7c6; font-family:'OCR A Std'; font-size:10.2pt"><span style="color:#629755"><em>/** 
</em></span><span style="color:#629755"><em> * </em></span><span style="color:#a8c023"><em>TODO </em></span><span style="color:#a8c023; font-family:'宋体'"><em>传入下标,设置被选择的</em></span><span style="color:#a8c023"><em>item 
</em></span><span style="color:#a8c023"><em> </em></span><span style="color:#629755"><em>* 
</em></span><span style="color:#629755"><em> * </em></span><span style="color:#629755"><strong><em>@param </em></strong></span><span style="color:#8a653b"><em>position 
</em></span><span style="color:#8a653b"><em> </em></span><span style="color:#629755"><em>*/ 
</em></span><span style="color:#cc7832">public void </span><span style="color:#ffc66d">setSelection</span>(<span style="color:#cc7832">int </span>position) { 
 <span style="color:#9876aa">clickTemp </span>= position<span style="color:#cc7832">; 
</span>}</pre> 
<br> 
<pre></pre> 

2、在Adapter的getView方法中加入如下代码 

<p></p> 
<p></p><pre name="code" class="java"><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'OCR A Std';font-size:10.2pt;"><span style="color:#808080;">//</span><span style="color:#a8c023;"><em>TODO </em></span><span style="color:#a8c023;font-family:'宋体';"><em>点击改变</em></span><span style="color:#a8c023;"><em>item</em></span><span style="color:#a8c023;font-family:'宋体';"><em>底部控件状态 
</em></span><span style="color:#cc7832;">if </span>(<span style="color:#9876aa;">clickTemp </span>== position) {<span style="color:#808080;">//</span><span style="color:#a8c023;"><em>TODO </em></span><span style="color:#a8c023;font-family:'宋体';"><em>选中的</em></span><span style="color:#a8c023;"><em>item</em></span><span style="color:#a8c023;font-family:'宋体';"><em>显示底部控件 
</em></span><span style="color:#a8c023;font-family:'宋体';"><em> </em></span>typeHolder.<span style="color:#9876aa;">item_gv_viewBottom</span>.setVisibility(View.<span style="color:#9876aa;"><em>VISIBLE</em></span>)<span style="color:#cc7832;">; 
</span>} <span style="color:#cc7832;">else </span>{<span style="color:#808080;">//</span><span style="color:#a8c023;"><em>TODO </em></span><span style="color:#a8c023;font-family:'宋体';"><em>未选中的</em></span><span style="color:#a8c023;"><em>item</em></span><span style="color:#a8c023;font-family:'宋体';"><em>隐藏底部控件 
</em></span><span style="color:#a8c023;font-family:'宋体';"><em> </em></span>typeHolder.<span style="color:#9876aa;">item_gv_viewBottom</span>.setVisibility(View.<span style="color:#9876aa;"><em>INVISIBLE</em></span>)<span style="color:#cc7832;">; 
</span>}</pre><p></p> 
<pre></pre> 

3、修改OnItemClickListener代码<br> 

<pre name="code" class="java"><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'OCR A Std';font-size:10.2pt;"><span style="color:#629755;"><em>/** 
</em></span><span style="color:#629755;"><em> * item</em></span><span style="color:#629755;font-family:'宋体';"><em>点击事件 
</em></span><span style="color:#629755;font-family:'宋体';"><em> </em></span><span style="color:#629755;"><em>*/ 
</em></span>AdapterView.OnItemClickListener <span style="color:#9876aa;">mItemClickListener </span>= <span style="color:#cc7832;">new </span>AdapterView.OnItemClickListener() { 
 <span style="color:#bbb529;">@Override 
</span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">onItemClick</span>(AdapterView<?> parent<span style="color:#cc7832;">, </span>View view<span style="color:#cc7832;">, int </span>position<span style="color:#cc7832;">, long </span>id) { 
  <span style="color:#9876aa;">myGridViewAdapter</span>.setSelection(position)<span style="color:#cc7832;">;</span><span style="color:#808080;">//</span><span style="color:#a8c023;"><em>TODO </em></span><span style="color:#a8c023;font-family:'宋体';"><em>传入当前点击</em></span><span style="color:#a8c023;"><em>item</em></span><span style="color:#a8c023;font-family:'宋体';"><em>的下标 
</em></span><span style="color:#a8c023;font-family:'宋体';"><em>  </em></span><span style="color:#9876aa;">myGridViewAdapter</span>.notifyDataSetChanged()<span style="color:#cc7832;">;</span><span style="color:#808080;">//</span><span style="color:#a8c023;"><em>TODO </em></span><span style="color:#a8c023;font-family:'宋体';"><em>刷新</em></span><span style="color:#a8c023;"><em>Adapter 
</em></span><span style="color:#a8c023;"><em> </em></span>} 
}<span style="color:#cc7832;">;</span></pre> 
<pre></pre> 
<br> 
<br> 
<p></p> 
<p><br> 
<br> 
</p>  
</pre></pre> 

上一篇:Android手势滑动实现两点触摸缩放图片

栏    目:Android代码

下一篇:Android自定义控件实现时钟效果

本文标题:Android实现腾讯新闻的新闻类别导航效果

本文地址:http://www.codeinn.net/misctech/199198.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有