How to make a single item of viewholder non recyclable

自作多情 提交于 2019-12-13 05:59:06

问题


I want to make only one item of the viewholder non recyclable and not the whole viewholder. How can I do so? I dont want the btnSave to be recyclable since its state may be different for each row.

Here is the code of what I have done so far :

public static class AlertViewHolder extends RecyclerView.ViewHolder {
    TextView tvTitle, tvDescription;
    Button btnLink, btnSave;

    AlertViewHolder(View itemView) {
        super(itemView);
        tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
        tvDescription = (TextView) itemView.findViewById(R.id.tvDescription);
        btnLink = (Button) itemView.findViewById(R.id.btnLink);
        btnSave = (Button) itemView.findViewById(R.id.btnSave);

        btnLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(alerts.get(getAdapterposition()).getLink()));
            context.startActivity(browserIntent);
        }
    });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSaveClicked(view);
            }
        });

     this.setIsRecyclable(false);
    }

    void onSaveClicked(View view) {
        DbHelper.getInstance(context).addSavedFeedRecord(alerts.get(getAdapterPosition()));
        Snackbar.make(view, "Item Saved", Snackbar.LENGTH_LONG)
                .setAction("UNDO", null).show();
        view.setEnabled(false);
    }
}

or the other way is:

@Override
public void onBindViewHolder(AlertViewHolder alertViewHolder, final int i) {
    alertViewHolder.setIsRecyclable(false);
    alertViewHolder.tvDescription.setText(alerts.get(i).getDesc());

}

回答1:


you can wrap

this.setIsRecyclable(false);

with

if(getposition()=item_position)



来源:https://stackoverflow.com/questions/32901035/how-to-make-a-single-item-of-viewholder-non-recyclable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!