fastadapter无法保存状态

时间:2018-06-29 17:04:16

标签: android fastadapter

我在viewHolder中有一个喜欢的按钮,当我在快速适配器中显示它时,用户喜欢一个帖子,然后向下滚动然后回到顶部,类似的突然消失,但请求已成功发送到服务器,并且如果刷新该应用程序的“喜欢”按钮可以正常工作,但是如果您再次不喜欢,则会显示问题。 BindView是否像按钮状态一样保持这种状态,还是我应该使用onBindViewHolder()

public class TestAppAdapterHolder extends AbstractItem<TestAppAdapterHolder,ViewHolder> {
    private Result result;

    public TestAppAdapterHolder(Result result) {
        this.result = result;
    }

    public TestAppAdapterHolder() {
    }

    public Result getResult() {
        return result;
    }

    @Override
    public ViewHolder getViewHolder(View v) {
        return new ViewHolder(v);
    }

    // Fast Adapter methods
    @Override
    public int getType() {
        return R.id.parent_testApp_list_item;
    }


    @Override
    public void bindView(final ViewHolder viewHolder, List<Object> payloads) {
        super.bindView(viewHolder, payloads);
        final Context context = viewHolder.itemView.getContext();

        if (result.getLike()) {
            viewHolder.testApp_item_like.setVisibility(View.VISIBLE);
            viewHolder.testApp_item_notlike.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.testApp_item_notlike.setVisibility(View.VISIBLE);
            viewHolder.testApp_item_like.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.testApp_list_item;
    }


    // Manually create the ViewHolder class
    protected static class ViewHolder extends RecyclerView.ViewHolder {

        protected ImageView testApp_item_like;
        protected ImageView testApp_item_notlike;


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

            testApp_item_like = view.findViewById(R.id.testApp_item_like);
            testApp_item_notlike = view.findViewById(R.id.testApp_item_notlike);

            testApp_item_like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    unlike();
                    String token = Utility.getSharedPref(itemView.getContext(), R.string.shared_preferences_token);
                    RequestInterface request = Utility.getRequerst();
                    Call<ResponseJson> call = request.unlike("TOKEN " + token, testAppId + "");
                    call.enqueue(new Callback<ResponseJson>() {
                        @Override
                        public void onResponse(Call<ResponseJson> call, Response<ResponseJson> response) {
                            if (response.isSuccessful()) {
                                if (response.body().getSuccess()) {
                                    testApp_item_like.setVisibility(View.INVISIBLE);
                                    testApp_item_notlike.setVisibility(View.VISIBLE);
                                } else {
                                    testApp_item_like.setVisibility(View.VISIBLE);
                                    testApp_item_notlike.setVisibility(View.INVISIBLE);
                                }
                            } else {
                                testApp_item_like.setVisibility(View.VISIBLE);
                                testApp_item_notlike.setVisibility(View.INVISIBLE);
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseJson> call, Throwable t) {
                            testApp_item_like.setVisibility(View.VISIBLE);
                            testApp_item_notlike.setVisibility(View.INVISIBLE);
                        }
                    });
                }
            });
            testApp_item_notlike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    like();
                    String token = Utility.getSharedPref(itemView.getContext(), R.string.shared_preferences_token);
                    RequestInterface request = Utility.getRequerst();
                    Call<ResponseJson> call = request.like("TOKEN " + token, testAppId + "");
                    call.enqueue(new Callback<ResponseJson>() {
                        @Override
                        public void onResponse(Call<ResponseJson> call, Response<ResponseJson> response) {
                            if (response.isSuccessful()) {
                                if (response.body().getSuccess()) {
                                    testApp_item_like.setVisibility(View.VISIBLE);
                                    testApp_item_notlike.setVisibility(View.INVISIBLE);
                                } else {
                                    testApp_item_like.setVisibility(View.INVISIBLE);
                                    testApp_item_notlike.setVisibility(View.VISIBLE);
                                }
                            } else {
                                testApp_item_like.setVisibility(View.INVISIBLE);
                                testApp_item_notlike.setVisibility(View.VISIBLE);
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseJson> call, Throwable t) {
                            testApp_item_like.setVisibility(View.INVISIBLE);
                            testApp_item_notlike.setVisibility(View.VISIBLE);
                        }
                    });
                }
            });


        public void unlike(){
            testApp_item_like.setVisibility(View.INVISIBLE);
            testApp_item_notlike.setVisibility(View.VISIBLE);
            testApp_item_like_count.setText(Functions.entofa(Functions.fatoen(testApp_item_like_count.getText().toString()) - 1 + ""));
            YoYo.with(Techniques.Pulse)
                    .duration(500)
                    .playOn(this.testApp_item_notlike);
        }
    }

}

0 个答案:

没有答案