我想要将焦点设置在特定的RecyclerView项上。 我尝试过
recyclerView.scrollToPosition(1);
在我的initializeRecyclerView方法中,我也尝试了
holder.setSelected(selectedItem==position)
没有成功。
你能帮我吗?
这是我的适配器:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.radio_item, parent, false);
return new ViewHolder(v);
}
/**
* Set the properties of a RecyclerView item
* @param holder RecyclerView item
* @param position Position of the item in the items list
*/
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Radio radio = radios.get(position);
holder.radio = radio;
ImageHelper.setImage(context, holder.imageButton, radio.getLogoPath(), context.getDrawable(Const.DEFAULT_RADIO_LOGO_DRAWABLE) );
holder.imageButton.setBackground(context.getDrawable(R.drawable.button));
holder.imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(clickListener != null){
clickListener.onRadioClick(holder.getAdapterPosition());
}
}
});
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageButton imageButton;
public Radio radio;
private ViewHolder(View itemView) {
super(itemView);
imageButton = itemView.findViewById(R.id.radioButton);
}
}
在我的Fragment中:
if(getView()!=null){
RadiosAdapter recyclerAdapter = new RadiosAdapter(radios, getActivity());
RecyclerView recyclerView = getView().findViewById(R.id.radios_recycler);
recyclerView.setAdapter(recyclerAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), columnNumber, LinearLayoutManager.VERTICAL, false));
if(currentRadio !=null){
recyclerView.scrollToPosition(currentRadio.getPosition());
}
else{
recyclerView.scrollToPosition(1);
}
if(getActivity() != null && getActivity() instanceof OnClickListener){
OnClickListener listener = (OnClickListener)getActivity();
recyclerAdapter.setClickListener(listener);
}
}
感谢!