Я создал представление переработчика в своем телевизионном приложении android, но оно ничего не показывает, я сделал это, как и для мобильных приложений, проверил отладку с использованием точек останова, и там отображаются данные представления обработчика.
Мой код выглядит следующим образом
<?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:id="@+id/main_browse_fragment"
android:name="com.rimapps.alrahabeachhotel.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity"
tools:deviceIds="tv"
tools:ignore="MergeRootFrame"
android:background="@drawable/home_bg"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/relTopLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_marginLeft="50dp">
<ImageView
android:layout_width="230dp"
android:layout_height="120dp"
android:scaleType="fitXY"
android:src="@drawable/home_logo"></ImageView>
<TextView
android:id="@+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:inputType="date"
android:text="11:47 PM"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:id="@+id/tvdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvTime"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:inputType="date"
android:text="Sunday 02/02/2020"
android:textColor="#fff"
android:textSize="12sp" />
<ImageView
android:id="@+id/ivWeather"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_below="@id/tvdate"
android:src="@drawable/weather"
android:layout_marginTop="20dp"
android:layout_marginRight="140dp"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="28 C / 68 F"
android:textColor="#fff"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_below="@id/tvdate"
android:textSize="16sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="0dp"
android:background="@drawable/chip"
android:text="@string/welcome"
android:textColor="#fff"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:background="@drawable/chip"
android:orientation="vertical" >
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_home_selector"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
Основная деятельность
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.rimapps.myapplication.activity;
import android.app.Activity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.rimapps.myapplication.R;
;
import com.rimapps.myapplication.adapter.HomeSelectorAdapter;
import com.rimapps.myapplication.model.ModelHomeSelector;
import java.util.ArrayList;
import java.util.List;
import static java.security.AccessController.getContext;
/*
* Main Activity class that loads {@link MainFragment}.
*/
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rvSelector = (RecyclerView)findViewById(R.id.rv_home_selector);
rvSelector.setNestedScrollingEnabled(false);
List<ModelHomeSelector> homeSelectorList = new ArrayList();
ModelHomeSelector object1 = new ModelHomeSelector();
object1.setSelectorTitle("Guest Directory");
object1.setSelectorImg(R.drawable.guestdirector);
homeSelectorList.add(object1);
ModelHomeSelector object2 = new ModelHomeSelector();
object2.setSelectorTitle("Restaurant & Bar");
object2.setSelectorImg(R.drawable.restaurandandbar);
homeSelectorList.add(object2);
ModelHomeSelector object3 = new ModelHomeSelector();
object3.setSelectorTitle("Leisure Facility");
object3.setSelectorImg(R.drawable.leisurefacility);
homeSelectorList.add(object3);
ModelHomeSelector object4 = new ModelHomeSelector();
object4.setSelectorTitle("Go Green");
object4.setSelectorImg(R.drawable.go_green_bg);
homeSelectorList.add(object4);
ModelHomeSelector object5 = new ModelHomeSelector();
object5.setSelectorTitle("Promotions");
object5.setSelectorImg(R.drawable.promotions);
homeSelectorList.add(object5);
ModelHomeSelector object6 = new ModelHomeSelector();
object6.setSelectorTitle("Tv Guide");
object6.setSelectorImg(R.drawable.tvguide);
homeSelectorList.add(object6);
rvSelector.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
HomeSelectorAdapter adapter = new HomeSelectorAdapter(homeSelectorList, getApplicationContext());
rvSelector.setAdapter(adapter);
}
}
Класс модели
package com.rimapps.myapplication.model;
public class ModelHomeSelector {
String selectorTitle;
int selectorImg;
public String getSelectorTitle() {
return selectorTitle;
}
public void setSelectorTitle(String selectorTitle) {
this.selectorTitle = selectorTitle;
}
public int getSelectorImg() {
return selectorImg;
}
public void setSelectorImg(int selectorImg) {
this.selectorImg = selectorImg;
}
}
Класс адаптера
package com.rimapps.myapplication.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.leanback.widget.ImageCardView;
import androidx.recyclerview.widget.RecyclerView;
import com.rimapps.myapplication.R;
import com.rimapps.myapplication.model.ModelHomeSelector;
import java.util.ArrayList;
import java.util.List;
public class HomeSelectorAdapter extends RecyclerView.Adapter<HomeSelectorAdapter.ViewHolder> {
private List<ModelHomeSelector> homeSelectorList = new ArrayList<>();
Context context;
public HomeSelectorAdapter(List<ModelHomeSelector> homeSelectorList, Context applicationContext) {
this.homeSelectorList = this.homeSelectorList;
this.context = context;
}
@NonNull
@Override
public HomeSelectorAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_home, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
final String selectorTitle = homeSelectorList.get(position).getSelectorTitle();
holder.selectorTitle.setText(selectorTitle);
holder.selectorImg.setImageResource(Integer.parseInt(String.valueOf(homeSelectorList.get(position).getSelectorImg())));
}
@Override
public int getItemCount() {
return homeSelectorList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView selectorTitle;
public ImageView selectorImg;
public ImageCardView selectorCard;
public ViewHolder(View itemView) {
super(itemView);
selectorCard = (ImageCardView) itemView.findViewById(R.id.selector_card);
selectorTitle = (TextView) itemView.findViewById(R.id.tv_home_seletor);
selectorImg=(ImageView) itemView.findViewById(R.id.iv_home_selector);
}
}}
макет в режиме рециркуляции
<?xml version="1.0" encoding="utf-8"?>
<androidx.leanback.widget.ImageCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/selector_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle">
<ImageView
android:id="@+id/iv_home_selector"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="@drawable/gogreeen" />
<TextView
android:id="@+id/tv_home_seletor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Guest Directory"
android:textColor="#fff"
android:textSize="12sp"
android:textStyle="bold"
android:layout_below="@id/iv_home_selector"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</androidx.leanback.widget.ImageCardView>
Что еще нужно сделать, чтобы отобразить его на телевизоре?