基于Android Tv制作一个Tv桌面(五)

基于Android Tv制作一个Tv桌面(五)

这一章是对之前的做的界面感到不满意,图标样式过于单一,都是图片加文字,并且TextView的高度不能改变,特别的Low。为此,我付出耗费两根头发巨大代价搞了个纯图片的图标。
我个人觉得纯图片的图标远比图片+文字的逼格高出许多,界面也会更加简洁些,简洁而高逼格的界面是最难做的!!!

新建一个SmallCardPresenter.java

package com.example.androidtv;

import android.graphics.drawable.Drawable;
import androidx.leanback.widget.Presenter;
import androidx.core.content.ContextCompat;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import java.util.List;


public class Small_CardPresenter extends Presenter {
    private static final String TAG = "CardPresenter";

    private static final int CARD_WIDTH = 200;
    private static final int CARD_HEIGHT = 120;
    private Drawable mDefaultCardImage;
    
    private List<Drawable> apps_icon = MainActivity.icon;
    private List<String> apps_name = MainActivity.name;
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {

        ImageView imageView = new ImageView(parent.getContext());

        
        FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(CARD_WIDTH, CARD_HEIGHT);
        
        //设置控件间距
        layout.setMargins(15, 15, 15, 15);
        
        imageView.setLayoutParams(layout);
        imageView.setFocusable(true);
        imageView.setFocusableInTouchMode(true);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        return new ViewHolder(imageView);
    }

    @Override
    public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
		
		//设置图标
        for(int i=0; i<apps_name.size(); i++)
        {
            if(((Movie) item).getTitle() == apps_name.get(i))
            {
                mDefaultCardImage = apps_icon.get(i);
                viewHolder.view.setBackground(mDefaultCardImage);
            }
        }
    }

    @Override
    public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
    }
}

再到MainFragment.java的loadRow()中,在哪个位置修改或插入可以自己决定,我这里就修改了header2下面的for循环(不记得在哪里就翻翻 第三章
改动的地方不多

Small_CardPresenter small_cardPresenter = new Small_CardPresenter();

HeaderItem header2 = new HeaderItem(7, MovieList.MOVIE_CATEGORY[1]);

for (i = 0; i < my_app_layer; i++) {
    Log.d(TAG, "my_app_layer:==================== " + my_app_layer);

    ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(small_cardPresenter);
    for (int j = 0; j < 8 && ((8*i + j) < my_apps_name.size()); j++)
    {
        listRowAdapter.add(list.get(8*i + j + sys_apps_name.size()));
    }
    if(i == 0)
    {
        rowsAdapter.add(new ListRow(header2, listRowAdapter));
    }
    else
    {
        rowsAdapter.add(new ListRow(listRowAdapter));
    }

}

大概的亚子是这样的
在这里插入图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页