【问题标题】:How to make a button Visible when RecyclerView has items in it?当 RecyclerView 中有项目时如何使按钮可见?
【发布时间】:2018-04-28 20:58:25
【问题描述】:

我正在做笔记应用程序。
RecyclerView 中有一个清除所有项目的按钮,并且它的 Visibility 设置为 Gone。但我希望它只有在应用中有注释时才可见。
我不知道为什么要使用
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());
    }

}}

【问题讨论】:

    标签: android android-recyclerview visibility


    【解决方案1】:

    根据您的代码,您应该尝试使用getItemCount()

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

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

    【讨论】:

    • 我在这里问之前做了同样的事情,但令人惊讶的是它不起作用,无论应用程序中是否有项目,按钮都会保持消失!我将适配器代码添加到问题中。看看有没有什么问题
    • @Hamed nope,您可以在其中添加与 notesList 相关的值。你做了adapter.addNote(notes);
    • @Hamed if(adapter.getItemCount() != 0) { // VISIBLE }
    • @IntelliJ-Amiliya 他们都没有工作:/按钮保持消失
    • @Hamed if (adapter.getItemCount()&gt;0)
    【解决方案2】:

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

    在添加注释后将notifyitemInserted更改为notifydatasetchanged

    吉利

        package com.example.gilad.myapplication;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.Button;
    
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
        private List<Notes> notesList = new ArrayList<>();
        private static final int REQUEST_CODE = 1001;
        final NoteAdapter adapter = new NoteAdapter(this);
        private View mRemoveAllButton;
    
        @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);
            mRemoveAllButton = 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);
                }
            });
    
    
            mRemoveAllButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    adapter.removeAll();
                    mRemoveAllButton.setVisibility(View.GONE);
                }
            });
    
        }
        @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);
                final int adapterCount = adapter.getItemCount();
                if (adapterCount == 1){
                    mRemoveAllButton.setVisibility(View.VISIBLE);
                }
            }
        }}
    

    【讨论】:

    • 我照你说的做了。但没有用:/只有按钮在被点击后变为 Gone
    • 我已经添加了代码。希望它会帮助你。请注意,我已将删除所有按钮更改为成员。
    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多