Android TV开发:使用RecycleView实现横向的Listview并响应点击

6
回复
787
查看
[复制链接]

446

主题

1101

帖子

1988

安币

手工艺人

发表于 2018-5-28 18:19:05 | 显示全部楼层 |阅读模式

            

        本文讲述了android tv开发:使用recycleview实现横向的listview并响应点击事件的代码。分享给大家供大家参考,具体如下:

        1.先贴出自己的效果图(可横向滚动,并响应item点击事件):

        

        2.关于点击事件的实现细节

        核心:使用接口回调

        在adapter中自己定义了个接口,然后在onbindviewholder中去为holder.itemview去设置相应的监听最后回调我们设置的监听。

[Java] 查看源文件 复制代码
class homeadapter extends recyclerview.adapter<homeadapter.myviewholder> 
{ 
//... 
 public interface onitemclicklitener 
 { 
 void onitemclick(view view, int position); 
 void onitemlongclick(view view , int position); 
 } 
 private onitemclicklitener monitemclicklitener; 
 public void setonitemclicklitener(onitemclicklitener monitemclicklitener) 
 { 
 this.monitemclicklitener = monitemclicklitener; 
 } 
 @override 
 public void onbindviewholder(final myviewholder holder, final int position) 
 { 
 holder.tv.settext(mdatas.get(position)); 
 // 如果设置了回调,则设置点击事件 
 if (monitemclicklitener != null) 
 { 
  holder.itemview.setonclicklistener(new onclicklistener() 
  { 
  @override 
  public void onclick(view v) 
  { 
   int pos = holder.getlayoutposition(); 
   monitemclicklitener.onitemclick(holder.itemview, pos); 
  } 
  }); 
  holder.itemview.setonlongclicklistener(new onlongclicklistener() 
  { 
  @override 
  public boolean onlongclick(view v) 
  { 
   int pos = holder.getlayoutposition(); 
   monitemclicklitener.onitemlongclick(holder.itemview, pos); 
   return false; 
  } 
  }); 
 } 
 } 
//... 
} 

        在activity设置监听,并实现接口

[Java] 查看源文件 复制代码
madapter.setonitemclicklitener(new onitemclicklitener() 
 { 
  @override 
  public void onitemclick(view view, int position) 
  { 
  toast.maketext(homeactivity.this, position + " click", 
   toast.length_short).show(); 
  } 
  @override 
  public void onitemlongclick(view view, int position) 
  { 
  toast.maketext(homeactivity.this, position + " long click", 
   toast.length_short).show(); 
   madapter.removedata(position); 
  } 
 }); 

        3.实现横向效果

        核心:linearlayoutmanager

[Java] 查看源文件 复制代码
linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(mcontext,linearlayoutmanager.horizontal,false); 
mrecyclerview.setlayoutmanager(linearlayoutmanager); 

        4. 让recyclerview的子item获得焦点

[Java] 查看源文件 复制代码
mrecyclerview.setonfocuschangelistener(new onfocuschangelistener() { 
  @override 
  public void onfocuschange(view v, boolean hasfocus) { 
  if(hasfocus){ 
   if(mrecyclerview.getchildcount()>0){ 
   mrecyclerview.getchildat(0).requestfocus(); 
   } 
  } 
  } 
 });

        5.代码实现:

[Java] 查看源文件 复制代码
//定义自己的布局文件 
public class easylayout extends linearlayout{ 
 private myadapter madapter; 
 private recyclerview mrecyclerview; 
 private context mcontext; 
 private list<string> mdatas; 
 public easylayout(context context) { 
 super(context); 
 mdatas = new arraylist<string>(); 
 for (int i=0;i<20;i++){ 
  string str = "图片:"+i; 
  mdatas.add(str); 
 } 
 this.setbackgroundcolor(color.parsecolor("#407eaf")); 
 this.mcontext = context; 
 this.setorientation(vertical); 
 createlayout(); 
 } 
 private void createlayout() { 
 //空出上边留白 
 textview te = new textview(mcontext); 
 viewgroup.layoutparams lp = new layoutparams(viewgroup.layoutparams.match_parent,160); 
 this.addview(te,lp); 
 mrecyclerview = new recyclerview(mcontext); 
 lp = new layoutparams(viewgroup.layoutparams.match_parent,viewgroup.layoutparams.wrap_content); 
 this.addview(mrecyclerview,lp); 
 
 linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(mcontext,linearlayoutmanager.horizontal,false); 
 mrecyclerview.setlayoutmanager(linearlayoutmanager); 
 //设置适配器 
 madapter = new myadapter( mcontext,mdatas); 
 mrecyclerview.setadapter(madapter); 
 //监听点击事件 
 madapter.setmonitemclicklitener(new myadapter.onitemclicklitener() { 
  @override 
  public void onitemclick(view view, int position) { 
  toast.maketext(mcontext,"点击了图片:"+position,toast.length_short).show(); 
  } 
 }); 
 
 //让recyclerview的子item获得焦点 
 mrecyclerview.setonfocuschangelistener(new onfocuschangelistener() { 
  @override 
  public void onfocuschange(view v, boolean hasfocus) { 
  if(hasfocus){ 
   if(mrecyclerview.getchildcount()>0){ 
   mrecyclerview.getchildat(0).requestfocus(); 
   } 
  } 
  } 
 }); 
 } 



[Java] 查看源文件 复制代码
//定义适配器类 
 public static class myadapter extends recyclerview.adapter<myadapter.viewholder> 
 { 
 private context mcontext; 
 private list<string> mdatas; 
 private layoutinflater minflater; 
 private onitemclicklitener monitemclicklitener; 
 //定义点击事件的接口 
 public interface onitemclicklitener 
 { 
  void onitemclick(view view, int position); 
 } 
 public myadapter( context mcontext,list<string> datats) 
 { 
  this.mcontext = mcontext; 
  this.mdatas = datats; 
  minflater = layoutinflater.from(this.mcontext); 
 } 
 public void setmonitemclicklitener(onitemclicklitener monitemclicklitener) 
 { 
  this.monitemclicklitener = monitemclicklitener; 
 } 
 @override 
 public int getitemcount() 
 { 
  return mdatas.size(); 
 } 
 /** 
  * 创建viewholder 
  */ 
 @override 
 public viewholder oncreateviewholder(viewgroup parent, int i) 
 { 
  view view = minflater.inflate(r.layout.easy_item,null); 
  viewholder viewholder = new viewholder(view); 
  return viewholder; 
 } 
 /** 
  * 设置值 
  */ 
 @override 
 public void onbindviewholder(final viewholder viewholder, final int position) 
 { 
  viewholder.btn.settext(mdatas.get(position)); 
  viewholder.btn.settextsize(40); 
  viewholder.btn.setgravity(gravity.center); 
  //设置回调 
  if (monitemclicklitener != null){ 
  viewholder.btn.setonclicklistener(new onclicklistener() { 
   @override 
   public void onclick(view v) { 
   int pos = viewholder.getlayoutposition(); 
   monitemclicklitener.onitemclick( viewholder.btn,pos); 
   } 
  }); 
  } 
 } 
 public class viewholder extends recyclerview.viewholder //我们需要inflate的item布局需要传入 
 { 
  button btn; 
  public viewholder(view arg0) 
  { 
  super(arg0); 
  btn = (button) arg0.findviewbyid(r.id.btn); 
  } 
 } 
 } 



[Java] 查看源文件 复制代码
//在activity加载自己的布局 
public class gallarrecyactivity extends appcompatactivity { 
 @override 
 protected void oncreate(@nullable bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 easylayout easylayout = new easylayout(this); 
 setcontentview(easylayout); 
 } 
} 

        easy_item.xml文件

[Java] 查看源文件 复制代码
<

        6.源码及apk






        

434

主题

1095

帖子

473

安币

手工艺人

发表于 2018-5-28 20:29:44 | 显示全部楼层
楼主是好人,回个帖会有安币吗?

3

主题

7201

帖子

1748

安币

Android大神

Rank: 6Rank: 6

发表于 2018-5-29 00:44:12 | 显示全部楼层
不错不错,楼主辛苦了。。。

501

主题

1186

帖子

2060

安币

手工艺人

发表于 2018-5-29 05:08:14 | 显示全部楼层
感谢分享,楼主V5~

350

主题

984

帖子

784

安币

手工艺人

发表于 2018-5-29 08:19:25 | 显示全部楼层
支持,感谢,祝巴士越来越好~

4

主题

9666

帖子

795

安币

代码手工艺人

Rank: 4

发表于 2018-5-29 13:11:24 | 显示全部楼层
支持楼主,支持安卓巴士!

501

主题

1198

帖子

317

安币

手工艺人

发表于 2018-5-29 16:21:37 | 显示全部楼层
感谢分享,安卓巴士有你更精彩:lol
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )