如何制作按钮当RecyclerView中有物品时可见?

时间:2017-11-15 15:04:03

标签: android android-recyclerview visibility

我正在制作笔记记录应用。
有一个清除RecyclerView中所有项目的按钮,其可见性设置为Gone。但我希望它只有在应用程序中有Notes时才可见。
我不知道为什么使用
if(notesList.size()> 0){             removeAllButton.setVisibility(View.VISIBLE);
不起作用!我怎样才能做到这一点?

编辑:添加了适配器代码。

这是我的主要课程:

public class MainActivity extends AppCompatActivity {
private List<Notes> notesList = new ArrayList<>();
private static final int REQUEST_CODE = 1001;
final NoteAdapter adapter = new NoteAdapter(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final RecyclerView recyclerView = findViewById(R.id.rv_recyclerView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
    Button addNoteButton = findViewById(R.id.button_main_addNote);
    final Button removeAllButton = findViewById(R.id.button_main_deleteAll);
    addNoteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, NoteEdit.class);
            startActivityForResult(intent, REQUEST_CODE);
        }
    });


    removeAllButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            adapter.removeAll();
            removeAllButton.setVisibility(View.GONE);
        }
    });
    if (adapter.getItemCount()>=1){
        removeAllButton.setVisibility(View.VISIBLE);
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE &&
            resultCode == RESULT_OK &&
            data != null) {
        String noteTitle = data.getStringExtra(NoteEdit.Result_Key_Title);
        String noteDescription = data.getStringExtra(NoteEdit.Result_Key_Description);
        Notes notes = new Notes();
        notes.setNoteTitle(noteTitle);
        notes.setNoteDescription(noteDescription);
        adapter.addNote(notes);
        adapter.getItemCount();
    }
}}


适配器类别:

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.SimpleItemViewHolder> {

private Context context;
private List<Notes> notes = new ArrayList<>();

public NoteAdapter(Context context) {

    this.context = context;
}

@Override
public SimpleItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View view = layoutInflater.inflate(R.layout.item_sample, parent, false);
    return new SimpleItemViewHolder(view);
}

@Override
public void onBindViewHolder(SimpleItemViewHolder holder, int position) {
    holder.bindNote(notes.get(position));

}

@Override
public int getItemCount() {
    return notes.size();
}

public void addNote(Notes note) {
    notes.add(note);
    notifyItemInserted(notes.size() - 1);
}

public void removeAll () {
    notes.clear();
    notifyDataSetChanged();
}

private TextView titleTextView;
private TextView descriptionTextView;

public class SimpleItemViewHolder extends RecyclerView.ViewHolder {

    public SimpleItemViewHolder(View itemView) {
        super(itemView);
        titleTextView = itemView.findViewById(R.id.tv_itemSample_title);
        descriptionTextView = itemView.findViewById(R.id.tv_itemSample_description);
    }


    public void bindNote(Notes note) {
        titleTextView.setText(note.getNoteTitle());
        descriptionTextView.setText(note.getNoteDescription());
    }

}}

2 个答案:

答案 0 :(得分:0)

根据您的准则,您应该尝试使用getItemCount()

  

返回由数据集保存的数据集中的项目总数   适配器。

  if(adapter.getItemCount() != 0)
    {
    // VISIBLE
    }

答案 1 :(得分:0)

我在onActivityResult中看到,您实际上是通过使用适配器将注释添加到RecyclerView。将注释添加到适配器后,您可能会从适配器调用notifydatasetchanged。 在适配器上的活动调用getItemCount()中添加注释后,只有当它等于1时才使按钮可见。 在删除所有按钮的clicklistener上,你应该隐藏按钮。

在addnote to notifydatasetchanged

之后还要更改notifyitemInserted

吉利

{{1}}