gpt4 book ai didi

android - ViewPager 类似于 AndroidTV 中 BrowseFragment 中的东西

转载 作者:太空狗 更新时间:2023-10-29 14:46:31 35 4
gpt4 key购买 nike

我是 Android TV 的新手。请查看下面的图片。

enter image description here

这张图片的上方有一个轮播图,所有红色标记的区域都是图片。

我想在 Android TV 中制作这个屏幕。我为此使用了 BrowseFragment。我使用了两个 Presenter,一个用于第一行,另一个用于另一行。

我用过的代码:

HomeFragment.java

    private  CustomListRowPresenter mListRowPresenter;
private void loadRows() {
List<Movie> list = MovieList.setupMovies();
mListRowPresenter = new CustomListRowPresenter(getActivity(),this);
mRowsAdapter = new ArrayObjectAdapter(mListRowPresenter);
CardPresenter cardPresenter = new CardPresenter(getActivity());

mNumberOfRows = NUM_ROWS;
HeaderItem gridHeader = new HeaderItem(0, "");
GridItemPresenter mGridPresenter = new GridItemPresenter();
ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
gridRowAdapter.add(getResources().getString(R.string.grid_view));
gridRowAdapter.add(getString(R.string.error_fragment));
gridRowAdapter.add(getResources().getString(R.string.personal_settings));
mRowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));

int i;
for (i = 1; i < NUM_ROWS; i++) {
if (i != 0) {
Collections.shuffle(list);
}
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
for (int j = 1; j < NUM_COLS; j++) {
listRowAdapter.add(list.get(j % 5));
}
HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i]);
mRowsAdapter.add(new ListRow(header, listRowAdapter));
}



setAdapter(mRowsAdapter);

}

CardPresenter.java

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

private static int CARD_WIDTH = 360;
private static int CARD_HEIGHT = 160;
private static int sSelectedBackgroundColor;
private static int sDefaultBackgroundColor;
private Drawable mDefaultCardImage;
private Activity mActivity;

public CardPresenter(Activity activity){
mActivity = activity;
}

public CardPresenter(){
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
Log.d(TAG, "onCreateViewHolder");

sDefaultBackgroundColor = parent.getResources().getColor(R.color.default_background);
sSelectedBackgroundColor = parent.getResources().getColor(R.color.selected_background);
mDefaultCardImage = parent.getResources().getDrawable(R.drawable.movie);
/*
ImageCardView cardView = new ImageCardView(parent.getContext()) {
@Override
public void setSelected(boolean selected) {
updateCardBackgroundColor(this, selected);
super.setSelected(selected);
}
};

cardView.setFocusable(true);
cardView.setFocusableInTouchMode(true);
cardView
.setBackgroundResource(R.drawable.gridview_cell_border);
updateCardBackgroundColor(cardView, false);*/

View view = mActivity.getLayoutInflater().inflate(R.layout.gridview_cell_rows, parent,
false);
LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.container_grid_view_item);
linearLayout.setBackgroundResource(R.drawable.gridview_cell_border);

view.setFocusable(true);

ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;

}

private static void updateCardBackgroundColor(ImageCardView view, boolean selected) {
int color = selected ? sSelectedBackgroundColor : sDefaultBackgroundColor;
// Both background colors should be set because the view's background is temporarily visible
// during animations.
view.setBackgroundColor(color);
view.findViewById(R.id.info_field).setBackgroundColor(color);
}

@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
Movie movie = (Movie) item;
View cardView = (View) viewHolder.view;
ImageView imageView = (ImageView)cardView.findViewById(R.id.imageView);
LinearLayout linearLayout = (LinearLayout)cardView.findViewById(R.id.container_grid_view_item);
linearLayout.setBackgroundResource(R.drawable.gridview_cell_border);
Log.d(TAG, "onBindViewHolder");
if (movie.getCardImageUrl() != null) {
/* cardView.setTitleText(movie.getTitle());
cardView.setContentText(movie.getStudio());
cardView.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);*/
Glide.with(viewHolder.view.getContext())
.load(movie.getCardImageUrl())
.centerCrop()
.error(mDefaultCardImage)
.into(imageView);
}


}

@Override
public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
Log.d(TAG, "onUnbindViewHolder");
View cardView = (View) viewHolder.view;
// Remove references to images so that the garbage collector can free up memory

}

所以对于第一行,我使用了 CardPresenter,对于其他行,我使用了 GridPresenter。

我的要求是这种 viewpager 类型的东西应该每 5 秒自动旋转一次。

所以请帮我设计这个 fragment 。

最佳答案

Creating a Catalog Browser 中所述,如果您希望自定义 fragment 之间的标题 View ,请使用 Presenter 并实现创建、绑定(bind)和取消绑定(bind) View 持有者的抽象方法。在BrowseFragment显示目录浏览器的实现,使用 setHeaderPresenterSelector() 方法设置行标题的呈现器或 setOnItemViewSelectedListener() 方法设置项目选择监听器,如中所示下面的例子。

setHeaderPresenterSelector(new PresenterSelector() {
@Override
public Presenter getPresenter(Object o) {
return new IconHeaderItemPresenter();
}
});

然后,使用 Timer 设置每个页面呈现器的持续时间,如本 GitHub post 所示.

对于示例代码实现,此 tutorial也可能有帮助。

关于android - ViewPager 类似于 AndroidTV 中 BrowseFragment 中的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39633682/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com