recyclerview未更新1

f2uvfpb9  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(189)

我有一个列表,它们显示为一个回收视图。我有两种适配器。当接收到列表时,它从第一个适配器的开头显示,确认后转到第二个适配器。问题是,当列表到达第一个适配器时,它会用确认按钮显示它的旧对象。
我的碎片列表

package fragment;

    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.annotation.Nullable;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.Toast;

    import java.util.List;
    import java.util.Objects;

    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;
    import R;
    import activity.LoginActivity;
    import activity.MainActivity;
    import adapter.template.JuiListAdapter;
    import core.entity.DutyResponseDTO;
    import core.entity.JuiResponseDTO;
    import core.service.DutyService;
    import core.service.JuiService;
    import core.service.UserService;
    import fragment.template.AppFragment;

    /**
     * Created by n.u.abdysamat@gmail.com on 8/17/16.
     * Update by b.keneshbekov@gmail.com on 7/22/19.
     */
    public class JuiListFragment extends AppFragment implements JuiService.onResponseListener<JuiResponseDTO>, DutyService.onResponseListener<DutyResponseDTO> {
        private final String TAG = "JuiListFragment";
        private String MOVE_URL = "url";
        private String JUI_BATTALION_ID = "juiBattalionId";
        private String REASON_ID = "reasonId";
        private final int requestPeriod = 30000; // 5 seconds by default, can be changed later
        private Handler m_handler;
        private static JuiListFragment instance;
        private JuiListAdapter juiListAdapter;

        public static JuiListFragment getInstance() {
            if (instance == null)
                instance = new JuiListFragment();
            return instance;
        }

        public JuiListFragment() {
            this.activityTitle = "Список ЖУИ";
            this.activitySubTitle = "Смена началась";
        }

        private MainActivity activi

        ty;

            @BindView(R.id.list_recycler)
            RecyclerView recyclerView;

            @BindView((R.id.logout))
            Button button;

            @BindView(R.id.overlay)
            View overlay;

            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_jui_list, container, false);
                activity = (MainActivity) getActivity();
                ButterKnife.bind(this, rootView);
                settings();
                setListeners();
                getCurrentDuty();

                return rootView;
            }

            @Override
            public void onCreate(Bundle bundle) {
                super.onCreate(bundle);
                m_handler = new Handler();
            }

            Runnable m_statusChecker = new Runnable() {
                @Override
                public void run() {
                    getCurrentDuty(); //this function can change value of m_interval.
                    m_handler.postDelayed(m_statusChecker, requestPeriod);
                }
            };

            private void getCurrentDuty() {
                if (juiListAdapter.getItemCount() >= 0) juiListAdapter.clearItems();
                JuiService.getInstance().getAllBattalionJui(this);
                settings();
            }

            private void settings() {
                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
                JuiListAdapter.RecyclerViewClickListener listener = (view, position, url, juiBattalionId, reasonId) -> {

                    if (reasonId == 1104) {
                        JuiReportFragment fragment = new JuiReportFragment();
                        Bundle bundle = new Bundle();
                        bundle.putString(MOVE_URL, url);
                        bundle.putLong(JUI_BATTALION_ID, juiBattalionId);
                        bundle.putLong(REASON_ID, reasonId);
                        fragment.setArguments(bundle);
                        activity.pushFragment(fragment);
                    } else {
                        JuiService.getInstance().moveBattalionJui(url,
                                juiBattalionId, reasonId, this);
                    }
                };
                juiListAdapter = new JuiListAdapter(listener);
                recyclerView.setAdapter(juiListAdapter);
                m_statusChecker.run();
            }

            private void setListeners() {
            }

            @OnClick(R.id.testB)
            public void refreshJuiList() {
                loadStart();
                getCurrentDuty();
            }

            @OnClick(R.id.logout)
            public void endDuty() {
                loadStart();
                try {
                    DutyService.getInstance().endCurrentDuty(this);
                    m_handler.removeCallbacks(m_statusChecker);
                } catch (Exception ex) {
                    m_handler.removeCallbacks(m_statusChecker);
                }
            }

            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void loadStart() {
                activity.hideKeyboard();
                if (!isLoading()) {
                    overlay.setVisibility(View.VISIBLE);
                    setLoading(true);
                }
            }

            @Override
            public void loadStop() {
                if (isLoading()) {
                    overlay.setVisibility(View.GONE);
                    setLoading(false);
                }
            }

            @Override
            public void onSuccess(List<JuiResponseDTO> response) {
                if(response != null && getActivity() != null) {
                    loadStop();
                    juiListAdapter.clearItems();
                    juiListAdapter.setItems(response);
                }
            }

            @Override
            public void onSuccess(JuiResponseDTO response) {
                if(getActivity() != null) {
                    getCurrentDuty();
                }
            }

            @Override
            public void onSuccessDuty(DutyResponseDTO response) {

            }

            @Override
            public void onLogoutSuccess(DutyResponseDTO response) {
                loadStop();
                Toast.makeText(getContext(), "Смена завершена", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);
            }

            @Override
            public void onLogoutFail(DutyResponseDTO response) {
                Toast.makeText(getContext(), "У вас еще есть незавершенные ЖУИ " +
                        "или ваша смена еще не закончилась!", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNetworkError(Throwable t) {
                loadStop();
            }

            @Override
            public void onRowNotFound(String message) {

            }

            @Override
            public void onCancel(UserService.onCancelListener onCancelListener) {

            }

            // to disable back button in view
            @Override
            public void onResume() {

                super.onResume();
                Objects.requireNonNull(this.getView()).setFocusableInTouchMode(true);
                this.getView().requestFocus();
                this.getView().setOnKeyListener(new View.OnKeyListener() {

                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        return keyCode == KeyEvent.KEYCODE_BACK;
                    }
                });
            }
        }

我的适配器:

// todo: Надо полностью переписать через абстракт когда будет время
    private TextView incidentType;
    private TextView incidentAddress;
    private TextView fabula;
    private TextView incidentStatus;

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

    @Override
    public void onBindViewHolder(JuiListViewHolder holder, int position) {
        holder.bind(juiResponseDTOS.get(position));
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    class JuiListViewHolder extends RecyclerView.ViewHolder {
        private RecyclerViewClickListener mListener;

        private JuiListViewHolder(View view, RecyclerViewClickListener listener) {
            super(view);
            mListener = listener;
            incidentType = (TextView) view.findViewById(R.id.incident_type);
            incidentAddress = (TextView) view.findViewById(R.id.incident_address);
            fabula = (TextView) view.findViewById(R.id.fabula);
            incidentStatus = (TextView) view.findViewById(R.id.incident_status);
        }

        void bind(JuiResponseDTO juiBattalionDTO) {
            LinearLayout ll = (LinearLayout)itemView.findViewById(R.id.jui_layout);
            ll.removeAllViews();
            for (ActionsDTO actionItem : juiBattalionDTO.getActionsDTOList())
            {

                Button newMoveButton = new Button(itemView.getContext());
                newMoveButton.setText(actionItem.getBtnName());

                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                ll.addView(newMoveButton, lp);
                newMoveButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mListener.onClick(view, JuiListViewHolder.this.getAdapterPosition(), actionItem.getUrl(),
                                juiBattalionDTO.getJuiBattalionDutyId(), actionItem.getReasonId());
                        ll.removeAllViewsInLayout();
                    }
                });
            }

            incidentType.setText(juiBattalionDTO.getIncidentType());
            incidentAddress.setText(juiBattalionDTO.getAddress());
            fabula.setText(juiBattalionDTO.getFabula());
            incidentStatus.setText(juiBattalionDTO.getStatus());
        }
    }

    public void setItems(Collection<JuiResponseDTO> juiBattalionDTOCollection) {
        juiResponseDTOS.addAll(juiBattalionDTOCollection);
        notifyDataSetChanged();
    }

    public void clearItems() {
        juiResponseDTOS.clear();
        notifyDataSetChanged();
    }

    public interface RecyclerViewClickListener {
        void onClick(View view, int position, String url, Long juiBattalionId, Long reasonId);
    }
}

谢谢大家的回答。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题