Android电视不显示Recyclerview

问题描述 投票:0回答:1

[我在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>

我还需要做一些其他事情才能在电视上显示它吗?

android android-recyclerview android-tv
1个回答
0
投票

您的代码看起来不错,但请检查以下可能引起问题的内容

1。尝试添加此行并检查。

rvSelector.setHasFixedSize(true); 

2。同样为了进行尝试,请从以下行中删除true,然后将false和LinearLayoutManager.HORIZONTAL传递给RecylerView.HORIZONTAL此处

而不是 rvSelector.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,true));

更改为

rvSelector.setLayoutManager(new LinearLayoutManager(this,RecyclerView.HORIZONTAL, false));

我不知道您为什么使用此rvSelector.setNestedScrollingEnabled(false),请尝试将其删除并检查。

© www.soinside.com 2019 - 2024. All rights reserved.