gpt4 book ai didi

android - 修复recyclerview选中项的位置

转载 作者:行者123 更新时间:2023-12-05 07:47:29 35 4
gpt4 key购买 nike

我正在 Android TV 上制作应用程序。我有一个 RecyclerView 显示水平方向的视频缩略图,由 DPAD 控件选择。我已经通过 apdapter 中的 DAPD 控件处理了 Recyclerview 的选择。它工作正常。现在,当我们滚动 DPAD 控件时,我想在第一个位置显示 RecylcerViw 的选定项目。我该怎么做?

下面是适配器类

public class TrendingAdapter extends RecyclerView.Adapter<TrendingAdapter.MyViewHolder>{
Activity context;
int focusedItem = 0;
ArrayList<VideoContentModel> catVideoList;

public TrendingAdapter(Activity context,ArrayList<VideoContentModel> catVideoList) {
super();
this.context = context;
this.catVideoList = catVideoList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.trending_item_row,parent,false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.itemView.setSelected(focusedItem == position);

VideoContentModel model = catVideoList.get(position);
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
holder.thumbnailView.setImageUrl(model.getVideoThumbnailUrl(), imageLoader);
//holder.thumbnailView.setImageUrl("http://www.calkinsmobile.com/uploads/hd-image/1436441227_87302_cook_this___1.jpg", imageLoader);

}

@Override
public int getItemCount() {
return catVideoList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{
NetworkImageView thumbnailView;

public MyViewHolder(View itemView) {
super(itemView);

thumbnailView = (NetworkImageView)itemView.findViewById(R.id.trend_thumbnail);


itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Redraw the old selection and the new
// notifyItemChanged(focusedItem);
// focusedItem = getLayoutPosition();
// notifyItemChanged(focusedItem);
}
});

}

}



@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
// TODO Auto-generated method stub
super.onAttachedToRecyclerView(recyclerView);

recyclerView.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();

// Return false if scrolled to the bounds and allow focus to move off the list
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
return tryMoveSelection(lm, 1);
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
return tryMoveSelection(lm, -1);
}else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
//Toast.makeText(context, "Item click on positon ="+focusedItem, Toast.LENGTH_LONG).show();

Intent intent = new Intent(context,VideoDetailsActivity.class);
intent.putParcelableArrayListExtra("videoList",catVideoList);
context.startActivity(intent);

}
}


return false;
}
});

}

private boolean tryMoveSelection(RecyclerView.LayoutManager lm, int direction) {
int tryFocusItem = focusedItem + direction;

// If still within valid bounds, move the selection, notify to redraw, and scroll
if (tryFocusItem >= 0 && tryFocusItem < getItemCount()) {
focusedItem = tryFocusItem;
notifyItemChanged(focusedItem);
lm.scrollToPosition(focusedItem);
return true;
}

return false;
}

}

下面是我的recyclerview

recyclerView = (RecyclerView)findViewById(R.id.trending_recylcer);
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
TrendingAdapter adapter = new TrendingAdapter(this, videoList);
recyclerView.setAdapter(adapter);

最佳答案

@Override
public int getItemViewType(int position)
{
return position;
}

@Override
public long getItemId(int position)
{
return (long) position;
}

关于android - 修复recyclerview选中项的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39654862/

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