我有一个使用Recycler视图的项目列表。当选择项目时,我需要更改项目的文本颜色(文本视图颜色)以指示选择了特定项目。下面是我的适配器代码。请建议我如何实现这一目标。
@Override
public GridView_Holder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.grid_customview, viewGroup, false);
gridHolder = new GridView_Holder(itemView);
ColorDrawable colorDrawableSelected = new ColorDrawable(context.getResources().getColor(R.color.borderColor));
// create StateListDrawable object and define its states
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, colorDrawableSelected);
stateListDrawable.addState(StateSet.WILD_CARD, null);
// set the StateListDrawable as background of the item view
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
itemView.setBackgroundDrawable(stateListDrawable);
}
else {
itemView.setBackground(stateListDrawable);
}
return new GridView_Holder(itemView);
}
@Override
public void onBindViewHolder(GridView_Holder holder, int position) {
model = arrayList.get(position);
gridHolder = (GridView_Holder) holder;// Holder
gridHolder.tvTitle.setText(model.getVideoName()); //set title
Glide.with(context).load(model.getVideoImageThumbnail())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(gridHolder.imageView);
}
@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
recyclerView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
GridLayoutManager lm = ((GridLayoutManager) recyclerView.getLayoutManager());
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
{
if(mCurrentSelectedPosition + 6 < getItemCount())
{
gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition);
gridHolder.itemView.setSelected(false);
notifyItemChanged(mCurrentSelectedPosition);
int lastItem = lm.findLastCompletelyVisibleItemPosition();
if((mCurrentSelectedPosition+6) > lastItem)
{
recyclerView.smoothScrollToPosition(mCurrentSelectedPosition+6);
}
else
{
gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition+6);
gridHolder.itemView.setSelected(true);
}
mCurrentSelectedPosition += 6 ;
}
return true;
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
{
}
else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if(mCurrentSelectedPosition + 1 < getItemCount())
{
gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition);
gridHolder.itemView.setSelected(false);
notifyItemChanged(mCurrentSelectedPosition);
int lastItem = lm.findLastCompletelyVisibleItemPosition();
if((mCurrentSelectedPosition+1) > lastItem)
{
recyclerView.smoothScrollToPosition(mCurrentSelectedPosition+1);
}
else
{
gridHolder = (GridView_Holder) mRecyclerView.findViewHolderForPosition(mCurrentSelectedPosition+1);
gridHolder.itemView.setSelected(true);
}
mCurrentSelectedPosition += 1 ;
}
return true;
}
return false;
}
});
}
答案 0 :(得分:0)
在GridView_Holder类中:
public final TextView textView,
public GridView_Holder(View itemView, int type) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.tv);
}
@Override
public void onItemSelected() {
textView.setTextColor(ContextCompat.getColor(context, R.color.accent));
}
@Override
public void onItemClear() {
textView.setTextColor(ContextCompat.getColor(context, R.color.primary));
}