我已经尝试了很多但是无法让这件事发挥作用。我有一些网格图片,我想用d-pad导航它。为此,我实现了setOnKeyListerner,但它给了我这个错误。 "无法解析setOnKeyListener(匿名方法android.view.View.OnKeyListener)"
public class GridView_Holder extends RecyclerView.ViewHolder implements
OnClickListener {
// View holder for gridview recycler view as we used in listview
public TextView grid_title, grid_location, grid_date;
public ImageView grid_imageView, grid_delete;
public RelativeLayout gridLayout;
private RecyclerView_OnClickListener.OnClickListener onClickListener;
public GridView_Holder(View view) {
super(view);
// Find all views ids
this.grid_title = (TextView) view.findViewById(R.id.grid_title);
this.grid_location = (TextView) view.findViewById(R.id.grid_location);
this.grid_date = (TextView) view.findViewById(R.id.grid_dateconstructed);
this.grid_imageView = (ImageView) view.findViewById(R.id.grid_imageview);
this.grid_delete = (ImageView) view.findViewById(R.id.grid_delete);
this.gridLayout = (RelativeLayout) view.findViewById(R.id.grid_layout);
// Implement click listener over views that we want
this.gridLayout.setOnClickListener(this);
this.grid_delete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// set custom listener
if (onClickListener != null) {
onClickListener.OnItemClick(v, getAdapterPosition());
}
}
// Setter for listener
public void setClickListener(
RecyclerView_OnClickListener.OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
适配器代码
public class GridView_Recycler_Adapter extends RecyclerView.Adapter<GridView_Holder> {
private ArrayList<Data_Model> arrayList;
private Context context;
public GridView_Recycler_Adapter(Context context,
ArrayList<Data_Model> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getItemCount() {
return (null != arrayList ? arrayList.size() : 0);
}
@Override
public void onBindViewHolder(GridView_Holder holder, int position) {
// Now in this method the items will set and click listener will occur
final Data_Model model = arrayList.get(position);
GridView_Holder gridHolder = (GridView_Holder) holder;// Holder
Bitmap image = BitmapFactory.decodeResource(context.getResources(),
model.getImage());// Converting drawable into bitmap
// setting data over views
gridHolder.grid_title.setText(model.getTitle());
gridHolder.grid_location.setText(model.getLocation());
gridHolder.grid_date.setText(model.getYear());
gridHolder.grid_imageView.setImageBitmap(image);
gridHolder.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_RIGHT:
Log.e("right button", "yes");
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
Log.e("left button", "yes");
break;
case KeyEvent.KEYCODE_DPAD_UP:
Log.e("up", "yes");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Log.e("down button", "yes");
break;
default:
break;
}
return false;
}
});
}
@Override
public GridView_Holder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
// This method will inflate the layout and return as viewholder
LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());
ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
R.layout.grid_customview, viewGroup, false);
GridView_Holder gridHolder = new GridView_Holder(mainGroup);
return gridHolder;
}
}
答案 0 :(得分:0)
使用RecyclerView.Adapter<MenuAdapter.MyViewHolder> implements View.OnKeyListener
并且
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return false;
}