我在我的回收视图的自定义适配器中有一个图像视图,我通过长按回收视图设置图像视图的可见性它正常工作我面临的问题当设置可见性为真时关闭应用程序或转到第二个活动可见性变为虚假我关闭应用程序或更改活动时所需的事情可见性保持不变..我是Android的新手
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewAdapter.RecycleViewHolder> {
private List<FileName> fileNames;
private Context context;
private OnItemClicked onClick;
public RecycleViewAdapter(Context context, List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
}
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
ViewGroup mainGroup = (ViewGroup) mInflater.inflate(R.layout.grid_item, parent, false);
RecycleViewHolder listHolder = new RecycleViewHolder(mainGroup);
return listHolder;
}
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
final FileName model = fileNames.get(position);
RecycleViewHolder mainHolder = holder;
mainHolder.title.setText(model.getShorttext());
mainHolder.note.setText(model.getName());
mainHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(position);
}
});
mainHolder.setLongClickListener(new LongClickListener() {
@Override
public void onItemLongClick(int position) {
}
});
if (fileNames.get(position).isVisible()) {
holder.imageView.setVisibility(View.VISIBLE);
} else {
holder.imageView.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return (null != fileNames ? fileNames.size() : 0);
}
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick;
}
private void showPopUpMenu(final int position, final View view) {
final PopupMenu popup = new PopupMenu(context, view);
popup.getMenuInflater().inflate(R.menu.custom_popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.menuLock) {
fileNames.get(position).setVisible(true);
notifyDataSetChanged();
} else {
AlertDialog.Builder alertbox = new AlertDialog.Builder(view.getRootView().getContext());
final EditText input = new EditText(context);
alertbox.setView(input);
alertbox.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertbox.setNegativeButton("CANCEL",null);
final AlertDialog dialog = alertbox.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String collect = input.getText().toString();
DBhelper dbhelper = new DBhelper(context);
String pass = dbhelper.searchpass(collect);
if(collect.equals(pass))
{
fileNames.get(position).setVisible(false);
notifyDataSetChanged();
dialog.dismiss();
}
else if (collect.equals(""))
{
input.setError("Hello");
}
else {
Toast.makeText(context, "Password does not match", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
});
}
return true;
}
});
popup.show();
}
public interface OnItemClicked {
void onItemClick(int position);
}
public class RecycleViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
public TextView title;
public TextView note;
public CardView cardView;
public ImageView imageView;
LongClickListener longClickListener;
public RecycleViewHolder(View view) {
super(view);
this.title = (TextView) view.findViewById(R.id.title);
this.note = (TextView) view.findViewById(R.id.note);
this.cardView = (CardView) view.findViewById(R.id.card_view);
this.imageView = (ImageView) view.findViewById(R.id.lock);
imageView.setVisibility(View.INVISIBLE);
view.setOnLongClickListener(this);
}
public void setLongClickListener(LongClickListener lc) {
this.longClickListener = lc;
}
@Override
public boolean onLongClick(View v) {
showPopUpMenu(getAdapterPosition(), v);
this.longClickListener.onItemLongClick(getLayoutPosition());
return false;
}
}
}
答案 0 :(得分:0)
在上面的代码中你隐藏了每个图像,如果列表显然你必须在列表中保存图像可见性状态。你不应该隐藏图像。长按一下。
在FileName中添加一个新变量boolean,如:imagevisibility
在onBindViewHolder()
中添加条件 if(model.getImageVisibility())
{
holder.imageView.setVisibility(View.VISIBLE);
}
else
{
holder.imageView.setVisibility(View.GONE);
}
mainHolder.setLongClickListener(new LongClickListener() {
@Override
public void onItemLongClick(int position) {
}
});
if (fileNames.get(position).isVisible()) {
FileName model = fileNames.get(position);
model.setImageVisibility(true);
} else {
model.setImageVisibility(false);
}
fileNames.set(position,model)
notifydatasetChanged();
}