当recyclerview为空时如何禁用按钮? [英] how to disable a button when the recyclerview is empty?

查看:92
本文介绍了当recyclerview为空时如何禁用按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,里面有一个文本视图,我有一个EditText和两个按钮(添加按钮删除按钮),<当我按下添加按钮后,c0>从EditText中获取所有值,然后使用 Remove Button 删除RecyclerView中的最后一项,但是当列表为空,然后按删除按钮应用程序崩溃.

I have a RecyclerView with one text view inside it, and I have one EditText and two buttons (Add Button & Remove Button) the RecyclerView takes all the values from the EditText after I press the Add Button, and I delete the last item in the RecyclerView with Remove Button but when the list is empty and I press Remove Button the app crashes.

我想在列表为空时禁用删除按钮.

I would like to disable Remove Button when the list is empty.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.brecyclerview.MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/edit_ten"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Score"
        android:inputType="numberSigned|number" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_add"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="btn_add"
            android:text="Add"
            android:enabled="false"
            tools:ignore="OnClick" />

        <Button
            android:id="@+id/btn_undo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="btn_undo"
            android:text="Undo"
            tools:ignore="OnClick" />

        <Button
            android:id="@+id/btn_new"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="btn_new"
            android:text="New Game" />

    </LinearLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleViewContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        tools:itemCount="8" />


</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/singleRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <ImageView
            android:id="@+id/userImg"
            android:src="@mipmap/ic_launcher"
            android:layout_width="60dp"
            android:layout_height="60dp" />

        <TextView
            android:id="@+id/pNametxt"
            android:text="User Name"
            android:textSize="20sp"
            android:layout_marginTop="6dp"
            android:maxLines="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/userImg"
            android:layout_toEndOf="@+id/userImg"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp" />



    </RelativeLayout>

    <View
        android:layout_below="@+id/singleRow"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f2f2f2" />


</RelativeLayout>

MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    int total = 0;
    Button button;
    Button button1;
    Button button2;
    EditText editText;
    TextView personName;
    RecyclerView recyclerView;
    RecyclerView.Adapter mAdapter;
    RecyclerView.LayoutManager layoutManager;

    List<PersonUtils> personUtilsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.btn_add);
        button1 = (Button) findViewById(R.id.btn_undo);
        button2 = (Button) findViewById(R.id.btn_new);
        editText = (EditText) findViewById(R.id.edit_ten);
        personName = (TextView) findViewById(R.id.pNametxt);

        recyclerView = (RecyclerView) findViewById(R.id.recycleViewContainer);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        personUtilsList = new ArrayList<>();
        mAdapter = new CustomRecyclerAdapter(this, personUtilsList);






        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String addvalue = editText.getText().toString().trim();
                button.setEnabled(!addvalue.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == button2) {
                    recreate();
                }}});

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                for (int i = 0; i < personUtilsList.size(); i++) {
                    total = personUtilsList.get(i).getPersonName();
                }
                total += Integer.parseInt(editText.getText().toString());


                personUtilsList.add(new PersonUtils(total));
                recyclerView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
                editText.setText("");


                button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position  = mAdapter.getItemCount() -1;
                        removeItem(position);
                    }
                });
            }

            public void removeItem(int position) {
                if(position <= 0)
                { recreate();

                }
                else if(position == mAdapter.getItemCount() - 1)
                {personUtilsList.remove(position);
                    mAdapter.notifyItemRemoved(position);

                }
                else
                {

                }
            }

        });}}

CustomRecyclerAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder> {

    private Context context;
    private static List<PersonUtils> personUtils;


    public CustomRecyclerAdapter(Context context, List personUtils) {
        this.context = context;
        this.personUtils = personUtils;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.itemView.setTag(personUtils.get(position));

        PersonUtils pu = personUtils.get(position);


       holder.pName.setText(String.valueOf(pu.getPersonName()));
    }

    @Override
    public int getItemCount() {

        return (personUtils.size()>8)?8:personUtils.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView pName;
        final Button deleteButton;
        public ViewHolder(final View itemView) {
            super(itemView);

            pName = (TextView) itemView.findViewById(R.id.pNametxt);
            deleteButton = (Button) itemView.findViewById(R.id.btn_undo);

        }}}

PersonUtils.java

public class PersonUtils {

    private Integer personName;

    public Integer getPersonName() {
        return personName;
    }

    public void setPersonName(Integer personName) {
        this.personName = personName;
    }

    public PersonUtils(Integer personName) {
        this.personName = personName;
    }

}

推荐答案

尝试一下:

if (personUtilsList==null||(personUtilsList !=null&&personUtilsList.isEmpty()) )
yourButton.setEnabled(false);

或者这个:

if (yourAdapter.getCount()==0)
    yourButton.setEnabled(false);

编辑

在列表中添加或删除项目之后,应调用adapter.notifyDatasetChanged(),在此行之后添加以下代码:

After place you add or remove item from your list, you should call adapter.notifyDatasetChanged(), after this line add these code:

if (yourAdapter.getCount()==0)
        yourButton.setEnabled(false);
else         yourButton.setEnabled(true);

选中此

public class MainActivity extends AppCompatActivity {
    int total = 0;
    Button button;
    Button button1;
    Button button2;
    EditText editText;
    TextView personName;
    RecyclerView recyclerView;
    RecyclerView.Adapter mAdapter;
    RecyclerView.LayoutManager layoutManager;

    List<PersonUtils> personUtilsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.btn_add);
        button1 = (Button) findViewById(R.id.btn_undo);
        button2 = (Button) findViewById(R.id.btn_new);
        editText = (EditText) findViewById(R.id.edit_ten);
        personName = (TextView) findViewById(R.id.pNametxt);

        recyclerView = (RecyclerView) findViewById(R.id.recycleViewContainer);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        personUtilsList = new ArrayList<>();
        mAdapter = new CustomRecyclerAdapter(this, personUtilsList);






        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String addvalue = editText.getText().toString().trim();
                button.setEnabled(!addvalue.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });







        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == button2) {
                    recreate();
                }}});




        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                for (int i = 0; i < personUtilsList.size(); i++) {
                    total = personUtilsList.get(i).getPersonName();
                }
                total += Integer.parseInt(editText.getText().toString());


                personUtilsList.add(new PersonUtils(total));
                recyclerView.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();
                editText.setText("");

                toggleButton();

                button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position  = mAdapter.getItemCount() -1;
                        removeItem(position);
                        toggleButton();
                    }
                });
            }



            public void removeItem(int position) {
                if(position <= 0)
                { recreate();

                }
                else if(position == mAdapter.getItemCount() - 1)
                {personUtilsList.remove(position);
                    mAdapter.notifyItemRemoved(position);
                    toggleButton();
                }
                else
                {

                }
            }

        });}
            public void toggleButton(){
                 if (yourAdapter.getCount()==0)
                        yourButton.setEnabled(false);
                  else  yourButton.setEnabled(true);}
}

这篇关于当recyclerview为空时如何禁用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆