菜单项的选项隐藏?
menuitem's option hide?
public class yeniAdapter extends RecyclerView.Adapter<yeniAdapter.MyViewHolder> {
ArrayList<Urun> urunListesi;
private Context mContext;
private OnItemClickListener mListener;
public yeniAdapter(Context context, List<Urun> urunListesi) {
mContext = context;
this.urunListesi = (ArrayList<Urun>) urunListesi;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.custom_satir, parent, false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Urun urunBilgileri = urunListesi.get(position);
String yorumyapanId=urunBilgileri.getKisiId();
holder.kisiIsim.setText(urunBilgileri.getKisiAd());
holder.urunYorum.setText(urunBilgileri.getUrunYorum());
}
@Override
public int getItemCount() {
return urunListesi.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener{
TextView kisiIsim,urunYorum;
public MyViewHolder(View itemView) {
super(itemView);
kisiIsim = (TextView) itemView.findViewById(R.id.ad);
urunYorum = (TextView) itemView.findViewById(R.id.yorum);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
});
itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
switch (item.getItemId()) {
case 1:
mListener.onWhatEverClick(position);
return true;
case 2:
mListener.duzenleClick(position);
return true;
}
}
}
return false;
}
@Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Ne Yapmak İstiyorsun?");
MenuItem doWhatever = menu.add(Menu.NONE, 1, 1, "Seç");
MenuItem duzenle = menu.add(Menu.NONE, 2, 2, "Düzenle");
doWhatever.setOnMenuItemClickListener(this);
duzenle.setOnMenuItemClickListener(this);
}
}
public interface OnItemClickListener {
void onItemClick(int position);
void onWhatEverClick(int position);
void duzenleClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
}
我有一个用户可以登录的应用程序。用户可以评论。在 Comments 页面上,如果评论属于本人,他将在 Menuitem 中看到他从 customm 适配器获取的选项。如果发表评论的人不是他自己的,他点击后会看到另一个选项,其他选项会被隐藏。我该怎么做?
我不知道你的菜单是否真的需要适配器,这种菜单的常用方法是 res/menu 下的 xml 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_edit_recipe"
android:title="Edit"/>
<item
android:id="@+id/option_one"
android:title="Option"></item>
</menu>
在您的 activity 中覆盖此方法,检查您的条件,并设置可见性
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
val yourMenuItem = menu!!.findItem(R.id.option_one)
val yourCondition = true
yourMenuItem.isVisible = !yourCondition
return true
}
添加菜单项后,能否在 onCreateContextMenu 中尝试使用以下行,
doWhatever.setVisible(false);
或
menu.findItem(1).setVisible(false);
您可以在添加项目后在onCreateContextMenu 中放置一个条件,并放置上面的行(其中任何一行)以隐藏上下文菜单的菜单项。请尝试让我知道。
public class yeniAdapter extends RecyclerView.Adapter<yeniAdapter.MyViewHolder> {
ArrayList<Urun> urunListesi;
private Context mContext;
private OnItemClickListener mListener;
public yeniAdapter(Context context, List<Urun> urunListesi) {
mContext = context;
this.urunListesi = (ArrayList<Urun>) urunListesi;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.custom_satir, parent, false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Urun urunBilgileri = urunListesi.get(position);
String yorumyapanId=urunBilgileri.getKisiId();
holder.kisiIsim.setText(urunBilgileri.getKisiAd());
holder.urunYorum.setText(urunBilgileri.getUrunYorum());
}
@Override
public int getItemCount() {
return urunListesi.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener{
TextView kisiIsim,urunYorum;
public MyViewHolder(View itemView) {
super(itemView);
kisiIsim = (TextView) itemView.findViewById(R.id.ad);
urunYorum = (TextView) itemView.findViewById(R.id.yorum);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
});
itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
switch (item.getItemId()) {
case 1:
mListener.onWhatEverClick(position);
return true;
case 2:
mListener.duzenleClick(position);
return true;
}
}
}
return false;
}
@Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Ne Yapmak İstiyorsun?");
MenuItem doWhatever = menu.add(Menu.NONE, 1, 1, "Seç");
MenuItem duzenle = menu.add(Menu.NONE, 2, 2, "Düzenle");
doWhatever.setOnMenuItemClickListener(this);
duzenle.setOnMenuItemClickListener(this);
}
}
public interface OnItemClickListener {
void onItemClick(int position);
void onWhatEverClick(int position);
void duzenleClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
}
我有一个用户可以登录的应用程序。用户可以评论。在 Comments 页面上,如果评论属于本人,他将在 Menuitem 中看到他从 customm 适配器获取的选项。如果发表评论的人不是他自己的,他点击后会看到另一个选项,其他选项会被隐藏。我该怎么做?
我不知道你的菜单是否真的需要适配器,这种菜单的常用方法是 res/menu 下的 xml 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_edit_recipe"
android:title="Edit"/>
<item
android:id="@+id/option_one"
android:title="Option"></item>
</menu>
在您的 activity 中覆盖此方法,检查您的条件,并设置可见性
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
val yourMenuItem = menu!!.findItem(R.id.option_one)
val yourCondition = true
yourMenuItem.isVisible = !yourCondition
return true
}
添加菜单项后,能否在 onCreateContextMenu 中尝试使用以下行,
doWhatever.setVisible(false);
或
menu.findItem(1).setVisible(false);
您可以在添加项目后在onCreateContextMenu 中放置一个条件,并放置上面的行(其中任何一行)以隐藏上下文菜单的菜单项。请尝试让我知道。