我正在开发一个用于餐厅管理的应用程序,但是我需要在RecyclerView内点击项目时更新textview(在RecyclerView之外)的值。
这是我的适配器,考虑一下这个适配器外的textView,并在项目点击Plus和Minus ImageViews上设置一个值,如下面的代码所示......
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {
ArrayList<MenuItem> MenuDetailList;
Typeface font;
public MyRecyclerAdapter(ArrayList<MenuItem> MenuDetailArray){
this.MenuDetailList = MenuDetailArray;
}
@Override
public MyRecyclerAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_menu_row_item, null);
return new MyHolder(v);
}
@Override
public void onBindViewHolder(final MyRecyclerAdapter.MyHolder holder, final int position) {
holder.txtSubMenuTitle.setText(MenuDetailList.get(position).getTitle());
holder.txtSubMenuPrice.setText(MenuDetailList.get(position).getPrice());
holder.txtSubMenuCount.setText(""+MenuDetailList.get(position).getItemCount());
setCustomTypeface(holder.txtSubMenuTitle, font);
setCustomTypeface(holder.txtSubMenuPrice,font);
holder.ImgViewSubMenuMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//holder.OrderCount = Integer.parseInt(holder.txtSubMenuCount.getText().toString());
int ItemCount = MenuDetailList.get(position).getItemCount();
if (ItemCount > 0) {
ItemCount = ItemCount - 1;
holder.txtSubMenuCount.setText("" + ItemCount);
MenuDetailList.get(position).setItemCount(ItemCount);
notifyDataSetChanged();
}
}
});
holder.ImgViewSubMenuPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//holder.OrderCount = Integer.parseInt(holder.txtSubMenuCount.getText().toString());
int ItemCount = MenuDetailList.get(position).getItemCount();
if (ItemCount < 20) {
ItemCount = ItemCount + 1;
holder.txtSubMenuCount.setText("" + ItemCount);
MenuDetailList.get(position).setItemCount(ItemCount);
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return MenuDetailList.size();
}
public static class MyHolder extends RecyclerView.ViewHolder {
TextView txtSubMenuTitle;
TextView txtSubMenuPrice;
ImageView ImgViewSubMenuMinus;
ImageView ImgViewSubMenuPlus;
TextView txtSubMenuCount;
private MyHolder(View row) {
super(row);
this.txtSubMenuTitle = (TextView) row.findViewById(R.id.list_txt_sub_menu_title);
this.txtSubMenuPrice = (TextView) row.findViewById(R.id.list_txt_sub_menu_price);
this.txtSubMenuCount = (TextView) row.findViewById(R.id.list_txt_sub_menu_count);
this.ImgViewSubMenuMinus = (ImageView) row.findViewById(R.id.list_img_sub_menu_minus);
this.ImgViewSubMenuPlus = (ImageView) row.findViewById(R.id.list_img_sub_menu_plus);
}
}
private void setCustomTypeface(TextView textView, Typeface font) {
textView.setTypeface(font);
}
}
提前致谢...
答案 0 :(得分:5)
接受的答案是黑客攻击。这不是正确的方法。您可以创建一个界面,将点击事件传递给您的活动。
在适配器类中创建此接口。
public interface OnItemClickListener {
void onItemClick(View view, int position, String tag);
}
然后在适配器的类级别创建一个实例。
private OnItemClickListener mItemClickListener;
以及设置此侦听器的方法。
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
现在在您的活动中执行此操作。
AdapterClassName.OnItemClickListener onItemClickListener = new StoryListAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position, String tag) {
//Do whatever you want to do on item click in your activity
}
}
mAdapter.setOnItemClickListener(onItemClickListener);
//mAdapter is your adapter instance
然后最后一件事就是在你的视图上设置了点击监听器。我假设您的视图仅存在于适配器类中。
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(View itemView) {
RelativeLayout mainHolder = (RelativeLayout) itemView.findViewById(R.id.main_holder);
mainHolder.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(mItemClickListener != null) {
mItemClickListener.onItemClick(itemView, getAdapterPosition());
}
}
}
答案 1 :(得分:3)
1.在您的活动中初始化TextView
TextView myTextview = (TextView) findViewByItd(R.id.textView);
2.在适配器的构造函数中添加参数TextView
3.将TextView传递给适配器
MyRecyclerAdapter adapter = new MyRecyclerAdapter(menu, myTextView);
4.根据需要操作
ArrayList<MenuItem> MenuDetailList;
Typeface font;
TextView mTextView;
public MyRecyclerAdapter(ArrayList<MenuItem> MenuDetailArray, TextView tv){
this.MenuDetailList = MenuDetailArray;
mTextView = tv;
}
mTextView.setText("Eat more pizza");
答案 2 :(得分:1)
设置标记onCreateViewHolder()作为你的持有者:
MyHolder holder = new MyHolder(v);
v.setTag(holder);
return holder;
现在您可以在点击监听器中获取数据:
MyHolder holder = (MyHolder)v.getTag();
答案 3 :(得分:0)
一个愚蠢的解决方案是在您的活动中声明它时尝试将文本视图传递给适配器。当然应该在传递之前定义文本视图。因此,您必须向适配器添加一个新成员,其类型为 TextView ,并且您必须将类型 TextView 的参数添加到适配器构造函数中... .. 我希望这个技巧可以提供帮助。
答案 4 :(得分:0)
在这种情况下,您需要使用接口的概念。让我们举个例子。让我们说textview处于活动状态,并在其中创建适配器实例,并将适配器设置为recycleler view
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyHolder> {
ArrayList<MenuItem> MenuDetailList;
Typeface font;
MyInterface myinterface;
public MyRecyclerAdapter(ArrayList<MenuItem> MenuDetailArray, MyInterface myinterface){
this.MenuDetailList = MenuDetailArray;
this.myinterface = myinterface;
}
现在在你的持有人中你需要添加onClickholder方法:
public static class MyHolder extends RecyclerView.ViewHolder {
TextView txtSubMenuTitle;
TextView txtSubMenuPrice;
ImageView ImgViewSubMenuMinus;
ImageView ImgViewSubMenuPlus;
TextView txtSubMenuCount;
private final View holderView;
private MyHolder(View row) {
super(row);
// save this view
holderView = row;
this.txtSubMenuTitle = (TextView) row.findViewById(R.id.list_txt_sub_menu_title);
this.txtSubMenuPrice = (TextView) row.findViewById(R.id.list_txt_sub_menu_price);
this.txtSubMenuCount = (TextView) row.findViewById(R.id.list_txt_sub_menu_count);
this.ImgViewSubMenuMinus = (ImageView) row.findViewById(R.id.list_img_sub_menu_minus);
this.ImgViewSubMenuPlus = (ImageView) row.findViewById(R.id.list_img_sub_menu_plus);
}
}
现在在你的onBindViewHolder上设置clicklistener:
holder.onClickHolder(new View.OnClickListener() {
@Override
public void onClick(View v) {
myinterface.myMethod("I want to return string");
}
});
现在您的活动必须实现此界面。因此它会在你的活动中调用myMethod。现在,您可以在活动中执行任何操作。在界面中添加任意数量的方法并享受回调。 这解释了接口的强大功能。