首页 存档 技术 查看内容

Android TV开发总结(七)构建一个TV app中的剧集列表控件

2018-3-30 13:00 |来自: 互联网 337 0

摘要: 前言:剧集类控件,在TV app中非常常见,今天将介绍构建一个TV app中的剧集列表控件,此控件上传到我的Github:https://github.com/hejunlin2013/EpisodeListView,点击【阅读原文】,可看对应的github, 喜欢可以star ...

前言:剧集类控件,在TV app中非常常见,今天将介绍构建一个TV app中的剧集列表控件,此控件上传到我的Github:https://github.com/hejunlin2013/EpisodeListView,点击【阅读原文】,可看对应的github, 喜欢可以star。Agenda如下:

  • 效果图

  • 效果图gif

  • 实现思路

  • 代码分析

效果图

效果图gif:

实现思路:

  • 1、用两个RecycleView作为控件横向布局

  • 2、PopupWindow作为该集剧情简介

  • 3、当焦点到达Parent时,对Child进行禁用词语,并发生变化,同理,如果Child超过10个时,通知Parent

代码分析:

EpisodeListView.java 作用:

  • 负责组配两个RecycleView填充对应的数据

  • 焦点禁用词语及获焦情况


  1. public class EpisodeListView extends RelativeLayout implements View.OnFocusChangeListener {

  2. public static final String TAG = EpisodeListView.class.get**Name();

  3. private Context mContext;

  4. private RelativeLayout mContentPanel;

  5. private RecyclerView mChildrenView;

  6. private RecyclerView mParentView;

  7. private LinearLayoutManager mEpisodesLayoutManager;

  8. private LinearLayoutManager mGroupLayoutManager;

  9. private EpisodeListViewAdapter mEpisodeListAdapter;

  10. private ChildrenAdapter mChildrenAdapter;

  11. private ParentAdapter mParentAdapter;

  12. private Handler mHandler = new Handler(Looper.getMainLooper());

  13. public EpisodeListView(Context context) {

  14. this(context, null);

  15. }

  16. public EpisodeListView(Context context, AttributeSet attrs) {

  17. this(context, attrs, 0);

  18. }

  19. public EpisodeListView(Context context, AttributeSet attrs, int defStyleAttr) {

  20. super(context, attrs, defStyleAttr);

  21. if (!isInEditMode()) {

  22. mContext = context;

  23. init();

  24. }

  25. }

  26. private void init() {

  27. LayoutInflater inflater = LayoutInflater.from(mContext);

  28. inflater.inflate(R.layout.episodelist_layout, this, true);

  29. mChildrenView = (RecyclerView) findViewById(R.id.episodes);

  30. mParentView = (RecyclerView) findViewById(R.id.groups);

  31. mEpisodesLayoutManager = new LinearLayoutManager(mContext, LinearLayout.HORIZONTAL, false);

  32. mGroupLayoutManager = new LinearLayoutManager(mContext, LinearLayout.HORIZONTAL, false);

  33. mChildrenView.setLayoutManager(mEpisodesLayoutManager);

  34. mParentView.setLayoutManager(mGroupLayoutManager);

  35. mChildrenView.setItemAnimator(new DefaultItemAnimator());

  36. mParentView.setItemAnimator(new DefaultItemAnimator());

  37. mChildrenView.setOnFocusChangeListener(this);

  38. mParentView.setOnFocusChangeListener(this);

  39. this.setOnFocusChangeListener(this);

  40. }

  41. public void setAdapter(final EpisodeListViewAdapter adapter) {

  42. mEpisodeListAdapter = adapter;

  43. mChildrenAdapter = adapter.getEpisodesAdapter();

  44. mParentAdapter = adapter.getGroupAdapter();

  45. mChildrenView.setAdapter(mChildrenAdapter);

  46. mParentView.setAdapter(mParentAdapter);

  47. mParentAdapter.setOnItemClickListener(new ParentAdapter.OnItemClickListener() {

  48. @Override

  49. public void onGroupItemClick(View view, int position) {

  50. mEpisodesLayoutManager.scrollToPositionWithOffset(adapter.getChildrenPosition(position), 0);

  51. }

  52. });

  53. mParentAdapter.setOnItemFocusListener(new ParentAdapter.OnItemFocusListener() {

  54. @Override

  55. public void onGroupItemFocus(View view, int position, boolean hasFocus) {

  56. int episodePosition = adapter.getChildrenPosition(position);

  57. mChildrenAdapter.setCurrentPosition(episodePosition);

  58. mEpisodesLayoutManager.scrollToPositionWithOffset(adapter.getChildrenPosition(position), 0);

  59. }

  60. });

  61. mChildrenAdapter.setOnItemFocusListener(new ChildrenAdapter.OnItemFocusListener() {

  62. @Override

  63. public void onEpisodesItemFocus(View view, int position, boolean hasFocus) {

  64. if (hasFocus) {

  65. int groupPosition = adapter.getParentPosition(position);

  66. mGroupLayoutManager.scrollToPositionWithOffset(groupPosition, 0);

  67. mParentAdapter.setCurrentPosition(adapter.getParentPosition(groupPosition));

  68. }

  69. }

  70. });

  71. mChildrenAdapter.setOnItemClickListener(new ChildrenAdapter.OnItemClickListener() {

  72. @Override

  73. public void onEpisodesItemClick(View view, int position 声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系 [邮箱地址] 删除


路过

雷人

握手

鲜花

鸡蛋

相关分类

返回顶部