Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

android studio - Hide Button if not data in recyclerview

I had created a app in which i have multiple edittext and one buttonwith recyclerviuew and one button which are setvisibilty as Gone.On Button click it add all my data in my recyclerview list with by chnaging visibality of my recycelr view and my button.In my Custom Adapter i am using a image view to delete the item from postion now i want that if it delete all data from list then it should automatically hide my Button and recyclerview.

//Recycler_view Adapter

 public class Myviewholder extends RecyclerView.ViewHolder {
        public TextView  land_Detail, area_detail, s_no1;
        public Button delete;


        public Myviewholder(View view) {
            super(view);

            land_Detail = view.findViewById(R.id.hint31);
            area_detail = view.findViewById(R.id.hint21);
            s_no1 = view.findViewById(R.id.hint11);
            delete = view.findViewById(R.id.hint41);

        }
    }

    public Land_adapters(List<Land_list> land_list, Context context) {
        this.laand_list = land_list;
        this.context = context;
    }


    @NonNull
    @Override
    public Land_adapters.Myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.land_list, parent, false);

        return new Myviewholder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull Myviewholder holder, final int position) {
        final Land_list current_year = laand_list.get(position);

        holder.area_detail.setText("District :" + current_year.getDistrict_name() + "
" + "Village :" + current_year.getVillage_name());

        holder.s_no1.setText(String.valueOf(position + 1));
        holder.land_Detail.setText("Acre :" + current_year.getAcre() + "
" + "Kanal :" + current_year.getKanal() + "
" + "Marla :" + current_year.getMarla());
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
                Land_list theRemovedItem = laand_list.get(position);
                // remove your item from data base
                laand_list.remove(position);  // remove the item from list
                notifyItemRemoved(position); // notify the adapter about the removed item

            }
        });
    }


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

//how i am using my recycler in mainactivity class

   public void final_step() {
        // marla_edit.setError(null);
        recyclerView = findViewById(R.id.recycler_view_last1);


        mAdapter1 = new Land_Adapter(last_Year1);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(mAdapter1);


        Land_list last_year1 = new Land_list(land_acre, land_kanal, village_selected1, land_marla, district_selected1, teshil_selected1, block_selected1, block_code, teshil_code1, village_code1, district_code);
        last_Year1.add(last_year1);
        mAdapter1.notifyDataSetChanged();
if (!(last_Year1.size() == 0)) {
    RelativeLayout linearLayout = findViewById(R.id.linear123);


    recyclerView.setVisibility(View.VISIBLE);
    next_button.setVisibility(View.VISIBLE);

}
else
{
    next_button.setVisibility(View.INVISIBLE);

}

// i just want that when it press delete image in recycelr view it should hide my button automatically.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use an interface to inform the fragment/activity, the list is empty. Adapter like as below:

public class CustomAdapter extends RecyclerView.Adapter<CustomViewHolder> {

        AdapterListener listener;

        public void setListener(AdapterListener listener) {
            this.listener = listener;
        }

        ...

        @Override
        public void onBindViewHolder(@NonNull Myviewholder holder, final int position) {
            ...
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ...
                    // remove your item from data base
                    laand_list.remove(position);  // remove the item from list
                    notifyItemRemoved(position); // notify the adapter about the removed item
                    if (laand_list.size()==0)
                        listener.onDataListIsEmpty();

                }
            });
        }

        public interface AdapterListener {
            void onDataListIsEmpty();
        }

    }

The Activity/Fragment like as below:

class MyActivity extends Activity implements CustomAdapter.AdapterListener {

        CustomAdapter adapter;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
            adapter = new CustomAdapter();
            adapter.setListener(this);
            ...
        }

        @Override
        public void onDataListIsEmpty() {
            // set visible or gone views 
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...