2017-06-03 70 views
0

我在我的回收視圖的自定義適配器中有一個圖像視圖,我通過長按回收視圖來設置圖像視圖的可見性其工作正常,問題我我面臨的是,當設置可見性是真實的,並關閉應用程序或去第二次活動的可見性去我需要的東西,當我關閉應用程序或更改活動的可見性仍然是一樣的..我是新來的android如何保存在自定義適配器中的imageview狀態

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

使用sharedpreference或任何靜態變量來保存圖像的狀態。這樣任何你訪問你的活動,你看到你的形象在你保存的最後一個狀態。 –

+0

你能幫我用我從來沒有使用sharedpreference的代碼嗎? –

回答

0

在上面的代碼中,如果列表中隱藏了每個圖像,那麼顯然您必須在列表中保存圖像可見性狀態。你應該不需要隱藏圖像。長時間點擊。

FileName中添加新的變量作爲布爾等: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(); 
} 
+0

cann u排列代碼? –

+0

你能告訴我你的適配器代碼嗎? –

+0

我從來沒有與Sharedpreference合作,你可以給我的工作代碼..它會幫助我很多 –

相關問題