我在RecyclerView中有一个锁定图像,我想在长按时隐藏/显示它。 我希望隐藏/显示根据它的位置,如果我长时间按回收器视图中的第一项并单击隐藏它将隐藏该项目的图像而不是其他人我已经尝试过这个问题我面临的问题只隐藏/显示最后一个位置项目中的图像。请帮助我提前感谢。
RecyclerViewAdapter
public class RecycleViewAdapter extends
RecyclerView.Adapter<RecycleViewHolder> {
private List<FileName> fileNames;
private Context context;
private OnItemClicked onClick;
String Name;
RecycleViewHolder mainHolder;
public RecycleViewAdapter(Context context,List<FileName> fileNames) {
this.context = context;
this.fileNames = fileNames;
}
public interface OnItemClicked {
void onItemClick(int position);
}
@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);
mainHolder = (RecycleViewHolder) 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) {
}
});
}
@Override
public int getItemCount() {
return (null != fileNames ? fileNames.size() : 0);
}
public void setOnClick(OnItemClicked onClick)
{
this.onClick=onClick;
}
public void getItemSelected(MenuItem item)
{
if (item.getItemId() == 0) {
mainHolder.imageView.setVisibility(View.VISIBLE);
} else {
mainHolder.imageView.setVisibility(View.INVISIBLE);
}
}
}
RecyclerViewHolder:
public class RecycleViewHolder extends RecyclerView.ViewHolder implements
View.OnLongClickListener,View.OnCreateContextMenuListener {
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);
view.setOnCreateContextMenuListener(this);
}
public void setLongClickListener(LongClickListener lc)
{
this.longClickListener = lc;
}
@Override
public boolean onLongClick(View v) {
this.longClickListener.onItemLongClick(getLayoutPosition());
return false;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
menu.add(0,0,0,"Lock");
menu.add(0,1,0,"UnLock");
}
}
我从谷歌找到了这个来检查Bindview上的位置,但我不知道如何在getItem中选择实现
if (position == eventsList.size() -1){
holder.profileImage.setVisibility(View.INVISIBLE)
}else{
holder.profileImage.setVisibility(View.VISIBLE)
}
MainActvity:
public class MainActivity extends AppCompatActivity implements
RecycleViewAdapter.OnItemClicked{
List<FileName> filenames;
DBhelper dBhelper;
SQLiteDatabase sqLiteDatabase;
ListViewAdapter listViewAdapter;
ListView listView;
ImageView lock;
String name,note;
String col;
private static RecyclerView recyclerView;
RecycleViewAdapter recycleViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkFirstRun();
//listView = (ListView)findViewById(R.id.lv_filename);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
lock =(ImageView)findViewById(R.id.lock);
dBhelper = new DBhelper(getApplicationContext());
sqLiteDatabase =dBhelper.getReadableDatabase();
filenames = dBhelper.getItemFromDatabase(sqLiteDatabase);
initViews();
if(filenames != null) {
recycleViewAdapter = new
RecycleViewAdapter(MainActivity.this,filenames);
recyclerView.setAdapter(recycleViewAdapter);
//listViewAdapter = new ListViewAdapter(getApplicationContext(),
R.layout.activity_main, filenames);
//listView.setAdapter(listViewAdapter);
}
recycleViewAdapter.setOnClick(this);
// registerForContextMenu(listView);
/* listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = filenames.get(position).getName();
note = filenames.get(position).getShorttext();
Alert();
}
});*/
}
private void initViews() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,
StaggeredGridLayoutManager.VERTICAL));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
Intent intent = new Intent(MainActivity.this, Note.class);
startActivity(intent);
return true;
case R.id.action_pass:
Intent i = new Intent(MainActivity.this, ChangePass.class);
startActivity(i);
return true;
case R.id.action_list:
Intent intent2 = new Intent(MainActivity.this, ToDo.class);
startActivity(intent2);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void Alert()
{
View mview = getLayoutInflater().inflate(R.layout.dialog_pass,null);
final EditText mEdittext = (EditText) mview.findViewById(R.id.Epass);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setView(mview);
mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
mBuilder.setNegativeButton("Cancel",null);
mBuilder.setCancelable(false);
final AlertDialog dialog = mBuilder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String col = mEdittext.getText().toString();
String password = dBhelper.searchpass(col);
if (col.equals(""))
{
mEdittext.setError("Please fill the blank field");
}
else if (col.equals(password))
{
Intent intent = new Intent(MainActivity.this,Note2.class);
intent.putExtra("Name",name);
intent.putExtra("Note",note);
startActivity(intent);
dialog.dismiss();
}
else {
Toast temp = Toast.makeText(MainActivity.this, "Password does not match", Toast.LENGTH_SHORT);
temp.show();
}
}
});
}
public void checkFirstRun() {
boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", true);
if (isFirstRun){
View view = getLayoutInflater().inflate(R.layout.first_run_dialog,null);
final EditText FirstPass = (EditText) view.findViewById(R.id.Fpass);
final Button submit = (Button)view.findViewById(R.id.ok);
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setView(view);
final AlertDialog dialog1 = builder.create();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
col = FirstPass.getText().toString();
if (col.equals(""))
{
FirstPass.setError("Please fill the field");
}
else{
savePass();
Toast.makeText(MainActivity.this, "Password Created", Toast.LENGTH_SHORT).show();
dialog1.dismiss();
}
}
});
dialog1.show();
dialog1.setCanceledOnTouchOutside(false);
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("isFirstRun", false)
.apply();
}
}
public void savePass()
{
dBhelper = new DBhelper(this);
sqLiteDatabase= dBhelper.getWritableDatabase();
dBhelper.storePass(col,sqLiteDatabase);
}
@Override
public void onItemClick(int position) {
name = filenames.get(position).getName();
note = filenames.get(position).getShorttext();
Alert();
}
@Override
public boolean onContextItemSelected(MenuItem item) {
recycleViewAdapter.getItemSelected(item);
return super.onContextItemSelected(item);
}
}
我的InterFace:
public interface LongClickListener {
void onItemLongClick(int position);
}
答案 0 :(得分:2)
您需要跟踪适配器中的锁定或解锁项目位置,如
ArrayList<Integer> lockedItems = new ArrayList<Interger>;
和 添加,删除项目。
或者您可以在模型Boolean
FileName
字段
实际上,你需要一种方法来识别给定位置的图像是否需要隐藏或显示
如果您知道是否需要隐藏/显示给定位置,那么您可以更新onBindViewHolder
中的视图
@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
final FileName model = fileNames.get(position);
RecycleViewHolder recycleViewHolder = (RecycleViewHolder) holder;
recycleViewHolder.title.setText(model.getShorttext());
recycleViewHolder.note.setText(model.getName());
if (model.isLocked()) { // or if (lockedItems.contains(position))
recycleViewHolder.imageView.setVisibility(View.VISIBLE);
} else {
recycleViewHolder.imageView.setVisibility(View.INVISIBLE);
}
<强>被修改强>
将字段selectedItem
添加到适配器以跟踪长按项目位置&amp;长按<项目位置
holder.setLongClickListener(new LongClickListener() {
@Override
public void onItemLongClick(int position) {
selectedItem = position
}
});
在getItemSelected
中使用该位置并将其添加到lockedItemsList
或在boolean
&amp;中添加FileName
字段更新其价值&amp;用它来知道给定位置的物品是锁定还是解锁。