AndroidTV开发11使用kotlin开发TV项目

阅读: 评论:0

AndroidTV开发11使用kotlin开发TV项目

AndroidTV开发11使用kotlin开发TV项目

目录

前言:

1.初始化变量:

(1)java写法:

(2)Kotlin写法:

2.初始化标题适配器:

(2-1)java写法:

(2-2)Kotlin写法:

3.初始化内容列表:

(3-1)java写法:

(3-2)kotlin写法:

4.初始化数据:

(4-1)java写法:

(4-2)kotlin写法:

5.点击标题item刷新数据:

(5-1)java写法:

(5-2)kotlin写法:

6.activity_main布局代码:

7.最后实现的效果如下:

8.可以发现Kotlin写法简洁很多,当然你如果对java比较熟悉,公司没有强行要求一定要用kotlin,你可以还是用java.个人建议可以业余学习一下kotlin不喜勿喷,毕竟Google官方文档和项目已经逐步转换为kotlin,如果不学习的话,后面看官方文档和项目都比较吃力,毕竟技不压身,多学习一个技能比较好~~

9.最后放上demo源码地址:

前言:

最近TV项目开发都是使用的kotlin,于是总结了一下,直接上代码:

1.初始化变量:

(1)java写法:

private MyRecycleViewAdapter adapter;

private RecyclerView recyclerView, rvGameList,rvTimeList;

//标题

private String[] titles = {"首页", "游戏", "教育", "生活", "娱乐", "新闻", "直播", "我的"};

private GameListAdapter gameListAdapter;

private List<GameListBean> gameListBeans;

(2)Kotlin写法:

private var adapter: MyRecyclerViewAdapter? = null
​
//标题
private val titles =arrayOf("首页", "游戏", "教育", "生活", "娱乐", "新闻", "直播", "我的")
private var gameListAdapter: GameListAdapter? = null
private var gameListBeans: MutableList<GameListBean>? = null

2.初始化标题适配器:

(2-1)java写法:

/**

* 初始化Adapter

*/

private void initAdapter() {

adapter = new MyRecycleViewAdapter(this, titles);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

recyclerView.setLayoutManager(linearLayoutManager);

recyclerView.setAdapter(adapter);

adapter.setOnFocusChangeListener(this);

adapter.setOnItemClickListener(this);

}

(2-2)Kotlin写法:

/*** 初始化标题*/
private fun initAdapter() {adapter = MyRecyclerViewAdapter(this)rv_tab!!.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)rv_tab!!.adapter = adapterrv_tab!!.requestFocus()adapter!!.setData(titles)adapter!!.setOnFocusChangeListener(this)adapter!!.setOnItemClickListener(this)
​
}

3.初始化内容列表:

(3-1)java写法:

 

private void initTimeAdapter() {

setData();

gameListAdapter = new GameListAdapter(gameListBeans, this, new GameListAdapter.OnItemClickListener() {

@Override

public void onItemClick(View view, int position) {

}

}) {

@Override

protected void onItemFocus(View itemView) {

itemView.setSelected(true);

View view = itemView.findViewById(R.id.iv_bg);

view.setSelected(true);

}

@Override

protected void onItemGetNormal(View itemView) {

itemView.setSelected(true);

View view = itemView.findViewById(R.id.iv_bg);

view.setSelected(true);

}

};

rvTimeList.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));

rvTimeList.addItemDecoration(new SpaceDecoration(30));

rvTimeList.setAdapter(gameListAdapter);

}

(3-2)kotlin写法:

/*** 初始化内容列表*/
private fun initTimeAdapter() {setData()gameListAdapter =object : GameListAdapter(gameListBeans, this, object : OnItemClickListener {override fun onItemClick(view: View?, position: Int) {}}) {override fun onItemFocus(itemView: View?) {itemView?.isSelected = trueval view: View = itemView!!.findViewById(R.id.iv_bg)view.isSelected = true}
​override fun onItemGetNormal(itemView: View?) {itemView?.isSelected = trueval view: View = itemView!!.findViewById(R.id.iv_bg)view.isSelected = true}}
​rv_time_list.layoutManager =LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)rv_time_list.addItemDecoration(SpaceDecoration(30))rv_time_list.adapter = gameListAdapter
}

4.初始化数据:

(4-1)java写法:

private void setData() {

gameListBeans = new ArrayList<>();

for (int i = 0; i < 15; i++) {

GameListBean gameListBean = new GameListBean();

gameListBean.setGameName("王者荣耀");

gameListBean.setImg("2");

gameListBeans.add(gameListBean);

}

}

(4-2)kotlin写法:

/*** 初始化数据*/
private fun setData() {gameListBeans = ArrayList()for (i in 0..14) {val gameListBean = GameListBean("2", "王者荣耀")(gameListBeans as ArrayList<GameListBean>).add(gameListBean)}
}

5.点击标题item刷新数据:

(5-1)java写法:

@Override

public void onItemClick(View view, int position) {

updateData(titles[position]);

}

 

private void updateData(String title) {

if (gameListBeans != null && gameListBeans.size() > 0) {

gameListBeans.clear();

}

for (int i = 0; i < 15; i++) {

GameListBean gameListBean = new GameListBean();

gameListBean.setGameName(title);

gameListBean.setImg("2");

gameListBeans.add(gameListBean);

}

}

(5-2)kotlin写法:

override fun onItemClick(view: View?, position: Int) {updateData(titles[position])
}
/*** 更新数据,这里是模拟数据,真实的项目是请求接口刷新数据*/
private fun updateData(title: String) {if (gameListBeans != null && gameListBeans!!.size > 0) {gameListBeans!!.clear()}for (i in 0..14) {val gameListBean = GameListBean("1", title)gameListBeans!!.add(gameListBean)}gameListAdapter!!.notifyDataSetChanged()
}

6.activity_main布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorPrimary"android:orientation="vertical"tools:context=".MainActivity">
​&lerview.widget.RecyclerViewandroid:id="@+id/rv_tab"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:focusable="true" />
​<HorizontalScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content">
​&le.tvrecyclerviewkotlin.view.DrawBorderGridLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content">
​
​<TextViewandroid:layout_width="wrap_content"android:layout_height="100dp"android:background="@drawable/text_selector"android:focusable="true"android:gravity="center"android:text="芒果TV"android:textSize="36sp" />
​<TextViewandroid:layout_width="130dp"android:layout_height="60dp"android:layout_marginLeft="10dp"android:background="@drawable/text_selector"android:focusable="true"android:gravity="center"android:text="小米TV"android:textSize="36sp" />
​<TextViewandroid:layout_width="wrap_content"android:layout_height="170dp"android:layout_marginTop="30dp"android:background="@drawable/text_selector"android:clickable="true"android:focusable="true"android:focusableInTouchMode="true"android:gravity="center"android:text="极米TV"android:textSize="44sp" />
​<TextViewandroid:layout_width="wrap_content"android:layout_height="90dp"android:layout_marginLeft="15dp"android:background="@drawable/text_selector"android:clickable="true"android:focusable="true"android:focusableInTouchMode="true"android:gravity="center"android:text="腾讯TV"android:textSize="36sp" />
​<TextViewandroid:layout_width="wrap_content"android:layout_height="92dp"android:layout_marginLeft="30dp"android:layout_marginTop="35dp"android:background="@drawable/text_selector"android:focusable="true"android:focusableInTouchMode="true"android:gravity="center"android:text="魔百盒"android:textSize="40sp" />
​<TextViewandroid:layout_width="140dp"android:layout_height="119dp"android:layout_marginLeft="38dp"android:background="@drawable/text_selector"android:clickable="true"android:focusable="true"android:focusableInTouchMode="true"android:gravity="center"android:text="PPTV"android:textSize="44sp" /></ample.tvrecyclerviewkotlin.view.DrawBorderGridLayout>
​</HorizontalScrollView>
​&le.tvrecyclerviewkotlin.view.FocusRecyclerViewandroid:id="@+id/rv_time_list"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:clipChildren="false"android:focusable="true" />
​
</LinearLayout>

7.最后实现的效果如下:

8.可以发现Kotlin写法简洁很多,当然你如果对java比较熟悉,公司没有强行要求一定要用kotlin,你可以还是用java.个人建议可以业余学习一下kotlin不喜勿喷,毕竟Google官方文档和项目已经逐步转换为kotlin,如果不学习的话,后面看官方文档和项目都比较吃力,毕竟技不压身,多学习一个技能比较好~~

9.最后放上demo源码地址:

TVRecyclerViewKotlin: Kotlin版TV开发

本文发布于:2024-02-05 06:44:14,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170726564064011.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:项目   AndroidTV   TV   kotlin
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23