简体   繁体   中英

bottom sheet recyclerview holder

I have a recyclerview that gets data as the realm results and then displays it as recyclerview item. My idea is that when a user clicks on an item a bottomsheet pops up and shows deatils.

Is there a way that in onBindViewHolder I can pass the data to bottomsheet and then in onClick method show bottomsheet? Thanks

Here's my code

public class EmployeeRVAdapterTable extends RecyclerView.Adapter<EmployeeRVAdapterTable.EmployeeViewHolderTable> implements RealmChangeListener {
    private RealmResults<Predavanja> mEmployees;


    public EmployeeRVAdapterTable(RealmResults<Predavanja> employee) {
        this.mEmployees = employee;
        mEmployees.addChangeListener(this);
    }

    @Override
    public EmployeeViewHolderTable onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_row_item, parent, false);
        return new EmployeeViewHolderTable(view);
    }

    @Override
    public void onBindViewHolder(EmployeeViewHolderTable holder, int position) {

        Predavanja predavanja = mEmployees.get(position);
        holder.tablename.setText(predavanja.getPredmetPredavanja());
        holder.tabletype.setText(predavanja.getRasponVremena());
        holder.tablemjesto.setText(predavanja.getDvorana());


        switch (predavanja.getPredavanjeIme()){
                case("Predavanja,"):
                    holder.tableboja.setBackgroundResource(R.color.blue_nice);
                    break;
                case("Auditorne vježbe,"):
                    holder.tableboja.setBackgroundResource(R.color.green_nice);
                    break;
                case("Kolokviji,"):
                    holder.tableboja.setBackgroundResource(R.color.purple_nice);
                    break;
                case("Laboratorijske vježbe,"):
                    holder.tableboja.setBackgroundResource(R.color.red_nice);
                    break;
                case("Konstrukcijske vježbe,"):
                    holder.tableboja.setBackgroundResource(R.color.grey_nice);
                    break;
            case("Seminar,"):
                    holder.tableboja.setBackgroundResource(R.color.blue_nice);
                    break;
                case("Ispiti,"):
                    holder.tableboja.setBackgroundResource(R.color.purple_dark);
                    break;
        }


    }

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

    public class EmployeeViewHolderTable extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView tablename, tabletype, tablemjesto;
        RelativeLayout tableboja;


        public EmployeeViewHolderTable(View itemView) {
            super(itemView);
            tablename = (TextView) itemView.findViewById(R.id.table_name);
            tabletype = (TextView) itemView.findViewById(R.id.table_type);
            tableboja = (RelativeLayout)itemView.findViewById(R.id.colorMe);
            tablemjesto = (TextView)itemView.findViewById(R.id.table_mjesto);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Context context = view.getContext();


            TextView info, info2,info3;


            final BottomSheetDialog dialog;
            View views = LayoutInflater.from(context).inflate(R.layout.bottom_sheep, null);

            info = (TextView) views.findViewById(R.id.predavanjeImeDialog);


            dialog = new BottomSheetDialog(context);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            dialog.setContentView(views);
            dialog.show();
        }
    }


    @Override
    public void onChange(Object element) {
        notifyDataSetChanged();
    }
}

The best approach is to get the position of the item and then get that item in the collection and use the data you want.

@Override
    public void onClick(View v) {
        int position= getAdapterPosition();
        String detail=  mEmployees.get(position).getDetail();
        // Do what you want with the detail
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM