gpt4 book ai didi

android - 从 ViewHolder 访问菜单项 - android

转载 作者:行者123 更新时间:2023-11-30 00:55:08 26 4
gpt4 key购买 nike

我正在研究回收站。这是我的适配器类:

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.ViewHolder> {

private static List<Notes> mNotes;
private Context mContext;
public NotesAdapter(Context context, List<Notes> notes) {
mNotes = notes;
mContext = context;
}

// Usually involves inflating a layout from XML and returning the holder
@Override
public NotesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);

// Inflate the custom layout
View notesView = inflater.inflate(R.layout.items_notes, parent, false);

// Return a new holder instance
final ViewHolder viewHolder = new ViewHolder(notesView);
notesView.setLongClickable(true);
notesView.setClickable(true);

return viewHolder;
}

// Easy access to the context object in the recyclerview
private Context getContext() {
return mContext;
}

@Override
public void onBindViewHolder(NotesAdapter.ViewHolder viewHolder, final int position) {

// Get the data model based on position
Notes notes = mNotes.get(position);
viewHolder.itemView.setSelected(mNotes.contains(position));

// Set item views based on your views and data model
TextView textView = viewHolder.preTitle;
textView.setText(notes.getTitle());
TextView textView1 = viewHolder.preText;
textView1.setText(notes.getText());
String color=notes.getColor();

CardView preCard=viewHolder.preCard;
preCard.setBackgroundColor(Color.parseColor(color));
ImageView img = viewHolder.preImage;
img.setVisibility(View.GONE);


}

// Returns the total count of items in the list
@Override
public int getItemCount() {
return mNotes.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
public RobotoTextView preTitle, preText;
public ImageView preImage;
public CardView preCard;
public MenuItem delete;

public ViewHolder(final View itemView) {
super(itemView);
itemView.setClickable(true);

preTitle = (RobotoTextView) itemView.findViewById(R.id.preTitle);
preText = (RobotoTextView) itemView.findViewById(R.id.preText);
preImage=(ImageView) itemView.findViewById(R.id.preImage);
preCard=(CardView) itemView.findViewById(R.id.preCard);
delete=(MenuItem) itemView.findViewById(R.id.delete);



itemView.setOnLongClickListener(new View.OnLongClickListener() {
int selected=0;
@Override
public boolean onLongClick(View view) {
int p=getLayoutPosition();
System.out.println("LongClick: "+p);
return true;
}
});

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int p=getLayoutPosition();

Notes notes = mNotes.get(p);

Intent intent = new Intent(view.getContext(),EditNote.class);

Bundle bundle = new Bundle();
bundle.putSerializable("DATA",notes);
intent.putExtras(bundle);
itemView.getContext().startActivity(intent);

// Toast.makeText(getContext(), "Recycle Click" + mNotes.get(viewHolder.getPosition())+" ", Toast.LENGTH_SHORT).show();
System.out.println("Click: "+p);
}
});
}
}

}

在这里,点击监听器工作正常。并长按。我能够获得点击和长按项目位置。但现在我想访问这个类的菜单项。在长按监听器方法中。我想编写代码,访问菜单项,该项目的 id 是 R.id.delete。

长按我想将该项目设置为可见。但是。

delete.setVisible(true);

给我空对象引用错误。任何想法?或建议?

最佳答案

在你的 Activity 类中编写这段代码,

 private Menu mMenu = null;
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
mMenu = menu;
return super.onPrepareOptionsMenu(menu);
}

public Menu getMenu()
{
return mMenu;
}

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mThis = this;
}

private static MainActivity mThis = null;
public MainActivity getThis()
{
return mThis;
}

在 Adapter LongClick 方法中调用 getMenu() 方法。如下所示。

@Override
public boolean onLongClick(View view) {
Menu menu = MainActivity.getThis().getMenu();
menu.findItem(R.d.Delete).setVisible(true);
return true;
}

关于android - 从 ViewHolder 访问菜单项 - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40303856/

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