按项目标记获取回收商查看项目?

时间:2015-10-28 10:46:15

标签: android android-recyclerview

我实施了一个聊天应用程序,比如什么应用程序,每当我收到一条消息时,我想更新该特定聊天的未读消息数。

我使用回收站视图显示联系人,现在当我收到聊天推送通知时,我需要更新行的未读消息计数,如下所示。

enter image description here

如果用户点击任何项目我可以获得视图,但我可以动态获取回收者视图项目

如果我知道我可以获得的位置但是用户不会点击任何一行,我需要动态更新特定行。我想为每一行设置标签,但我不知道如何根据标签获取行。

提前致谢

更新代码:

public class JobsFragmentRecyclerViewAdapter extends RecyclerView.Adapter<JobsFragmentRecyclerViewAdapter.JobsViewHolder> {


    private List<PdJob> jobs;


    public  JobsFragmentRecyclerViewAdapter(List<PdJob> jobs){
        this.jobs = jobs;
    }


    public void sortByCustomerName(){
        Comparator<PdJob> myComparator = new Comparator<PdJob>() {
            public int compare(PdJob obj1,PdJob obj2) {
                if(obj1.userName != null && obj2.userName != null){
                    return obj1.userName.compareTo(obj2.userName);
                }
                    return 0;
            }
        };

        Collections.sort(jobs,myComparator);
        this.notifyDataSetChanged();
    }

    public void sortByModel(){
        Comparator<PdJob> myComparator = new Comparator<PdJob>() {
            public int compare(PdJob obj1,PdJob obj2) {
                if(obj1.jobCarName != null && obj2.jobCarName != null){
                    return obj1.jobCarName.compareTo(obj2.jobCarName);
                }
                    return 0;
            }
        };

        Collections.sort(jobs,myComparator);
        this.notifyDataSetChanged();
    }


    @Override
    public JobsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View partnerTypeView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        JobsViewHolder jobsViewHolder = new JobsViewHolder(partnerTypeView);
        return jobsViewHolder;
    }

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

        final PdJob pdJob = jobs.get(position);
        holder.mView.setTag(pdJob.jobId);
        holder.consumerName.setText(pdJob.userName);
        holder.jobIdNumber.setText(pdJob.jobId);
        holder.consumerVehicle.setText( pdJob.jobCarName);
        holder.typeOfService.setText(pdJob.serviceType);
        holder.registrationNumber.setText(pdJob.regNo);
        if(NotificationManager.checkJobChatExists(pdJob.jobId)) {
            holder.textNumber.setText("" + NotificationManager.getJobNotificationCount(pdJob.jobId));
        }
        else {
            holder.textNumber.setText("0");
        }
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //screen navigation
            }
        });

        holder.chatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //screen navigation
            }
        });

    }

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

    static class JobsViewHolder  extends RecyclerView.ViewHolder{

        @InjectView(R.id.registration_number)
        TextView registrationNumber;

        @InjectView(R.id.job_id_number) TextView jobIdNumber;

        @InjectView(R.id.consumer_name) TextView consumerName;

        @InjectView(R.id.consumer_vehicle_model) TextView consumerVehicle;

        @InjectView(R.id.text_number) TextView textNumber;

        @InjectView(R.id.job_type_of_service) TextView typeOfService;



        @InjectView(R.id.chat_button) ImageView chatButton;
        public final View mView;

        JobsViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            ButterKnife.inject(this, itemView);
        }
    }



}

1 个答案:

答案 0 :(得分:0)

如果您不知道新数据的目的是哪个视图,则必须通过再次设置然后调用来重新计算您的视图。

notifyDataSetChanged();

如果没有代码示例,很难给出比这更好的答案,以了解您的适配器当前是如何工作的。

编辑:

如果您在推送通知中收到PdJob ID,则可以执行以下操作:

public void updateJob(int jobId) {
    notifyItemChanged(getPdJobPosition(jobId));
}

private int getPdJobPosition(int jobId) {
    for (int i = 0; i < jobs.size(); i++) {
        if (jobId == jobs.get(i).getId()) {
            return i;
        }
    }
}