目录
前言:
最近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);
recyclerView.requestFocus();
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 = adapter rv_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 = true val view: View = itemView!!.findViewById(R.id.iv_bg) view.isSelected = true } override fun onItemGetNormal(itemView: View?) { itemView?.isSelected = true val 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);
}
gameListAdapter.notifyDataSetChanged();
}
(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="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" android:orientation="vertical" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_tab" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="true" /> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <com.example.tvrecyclerviewkotlin.view.DrawBorderGridLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android: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" /> <TextView android: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" /> <TextView android: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" /> <TextView android: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" /> <TextView android: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" /> <TextView android: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" /> </com.example.tvrecyclerviewkotlin.view.DrawBorderGridLayout> </HorizontalScrollView> <com.example.tvrecyclerviewkotlin.view.FocusRecyclerView android: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开发
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/kotlinkf/1127.html