实施已分组为hashmap的recycleviwer项目的点击?

时间:2018-06-07 19:14:44

标签: android

我有一个按日期分组的列表,在这样做之后,会被发送到适配器,以便组装布局。 上市代码:

agendamentoVos = (ArrayList<AgendamentoVo>) ((RetornoFilaAtendimento) asyncTaskResult.getResult()).getRetorno();
            HashMap<Date, List<AgendamentoVo>> groupedHashMap = groupDataIntoHashMap(agendamentoVos);

            for (Date date : groupedHashMap.keySet()) {
                DateItem dateItem = new DateItem();
                String date1 = "";
                try {
                    date1 = UtilDateFormat.formatDateToStringAnoMesDia(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                dateItem.setDate(date1);
                listItem.add(dateItem);

                for (AgendamentoVo pojoOfJsonArray : groupedHashMap.get(date)) {
                    GeneralItem generalItem = new GeneralItem();
                    generalItem.setAgendamentoVoGeneral(pojoOfJsonArray);//setBookingDataTabs(bookingDataTabs);
                    listItem.add(generalItem);
                }
            }

此处分组方法:

private HashMap<Date, List<AgendamentoVo>> groupDataIntoHashMap(List<AgendamentoVo> agendamentoVoList) {

    HashMap<Date, List<AgendamentoVo>> groupedHashMap = new HashMap<>();

    for (AgendamentoVo agendamentoVo : agendamentoVoList) {

        Date hashMapKey = UtilDateFormat.removeHora(agendamentoVo.getItemAgenda().getDtConsulta());// dd/MM/yyyy

        if (groupedHashMap.containsKey(hashMapKey)) {
            // The key is already in the HashMap; add the pojo object
            // against the existing key.
            groupedHashMap.get(hashMapKey).add(agendamentoVo);
        } else {
            // The key is not there in the HashMap; create a new key-value pair
            List<AgendamentoVo> list = new ArrayList<>();
            list.add(agendamentoVo);
            groupedHashMap.put(hashMapKey, list);
        }
    }

    return groupedHashMap;
}

发送分组到适配器的内容:

mAdapter = new CustomRecyclerViewAdapterFila(listItem, this, this, new CustomItemClickListener() {
                        @Override
                        public void onItemClick(View v, int position) {
                            itemclick(position);
                        }
                    });
                    // use this setting to improve performance if you know that changes
                    // in content do not change the layout size of the RecyclerView
                    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    //mRecyclerView.setHasFixedSize(true);
                    // use a linear layout manager
                    mRecyclerView.setLayoutManager(layoutManager);
                    mRecyclerView.setAdapter(mAdapter);

在适配器中,按如下方式在图像中安装分组布局:

enter image description here

然后,适配器阵列的大小为5,卡片位于1,2和4位置。然后,对于单击,中断,例如,如果单击第三张卡,即位置4。

错误:

at br.com.sisteplan.evida.atendimento.activities.FilaActivity.itemclick(FilaActivity.java:310)
                                                                                    at br.com.sisteplan.evida.atendimento.activities.FilaActivity.access$500(FilaActivity.java:46)
                                                                                    at br.com.sisteplan.evida.atendimento.activities.FilaActivity$4.onItemClick(FilaActivity.java:291)
                                                                                    at br.com.sisteplan.evida.atendimento.adapters.CustomRecyclerViewAdapterFila$1.onClick(CustomRecyclerViewAdapterFila.java:110)

适配器:

private CustomItemClickListener listener;
@NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(
            @NonNull ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        switch (viewType) {

            case ListItem.TYPE_GENERAL:
                View v1 = inflater.inflate(R.layout.cell_item_fila, parent,
                        false);
                viewHolder = new GeneralViewHolder(v1);
                break;

            case ListItem.TYPE_DATE:
                View v2 = inflater.inflate(R.layout.cell_item_date_fila, parent, false);
                viewHolder = new DateViewHolder(v2);
                break;

        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

        switch (viewHolder.getItemViewType()) {

            case ListItem.TYPE_GENERAL:
                final GeneralItem generalItem = (GeneralItem) mDataset.get(position);
                final GeneralViewHolder generalViewHolder= (GeneralViewHolder) viewHolder;
                generalViewHolder.txtTitle.setText(generalItem.getAgendamentoVoGeneral().getPaciente().getNmPaciente());
                generalViewHolder.linearLayoutCard.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(view, generalViewHolder.getAdapterPosition());
                }
            });
                break;

            case ListItem.TYPE_DATE:

                DateItem dateItem = (DateItem) mDataset.get(position);
                DateViewHolder dateViewHolder = (DateViewHolder) viewHolder;
                dateViewHolder.txtTitle.setText(dateItem.getDate());
                // Populate date item data here
                break;
        }
    }

项目单击:

private void itemclick(int position) {
    Intent it = new Intent(this, AtendimentoPacienteActivity.class);
    Bundle bundle = new Bundle();
    //CRASH CRASH!!!
    pacienteVo = agendamentoVos.get(position).getPaciente(); //CRASH CRASH!!!
    consultaVo.setDtInicio(UtilDateFormat.dataCorrente());
    consultaVo.setAgendamento(agendamentoVos.get(position));
    bundle.putSerializable(getString(R.string.argument_paciente), pacienteVo);
    bundle.putSerializable(getString(R.string.consulta_bundle), consultaVo);
    it.putExtras(bundle);
    startActivity(it);
}

如何实施点击项目? 感谢

1 个答案:

答案 0 :(得分:0)

由于您的项目列表很复杂,因此很难根据位置处理点击,但您始终可以使用视图设置TAG,并且在onClick方法中,您可以使用getTag()方法获取项目并采取必要的操作关于这个项目。

generalViewHolder.linearLayoutCard.setTag(generalItem);
generalViewHolder.linearLayoutCard.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(view,(GeneralItem)v.getTag());
                }
            });

定义:

private void onItemClick(View v, GeneralItem generalItem) {
    //do your handling based on generalItem object
}

或者您可以将列表中项目的实际位置设置为TAG值而不是完整项目。