当我尝试更新适配器的单个项目时,我正在使用RecyclerView.Adapter
。呼叫notifyItemChanged
。它没有任何改变。我附上代码。
我正在使用一个组件来显示任务堆栈,并且在一定比例的滑动中,我需要显示一个批准的图标,在布局顶部被拒绝,我正在设法捕获它,但是在更新项目时回收站不会更新它。
片段:
public class PushMissionsFragment extends BoostFragment implements PushMissionsAdapter.MissionCardListener, FavouriteListener {
@Inject
Navigator navigator;
@Inject
UserViewModel userViewModel;
@Inject
FinalUserViewModel finalUserViewModel;
@Inject
MissionViewModel missionViewModel;
@Inject
MissionRepository missionRepository;
@BindView(R.id.stack)
SwipeStack stack;
private PushMissionsAdapter adapter;
private Location myLocation;
private final FavouriteListener favouriteListener = this;
public static PushMissionsFragment newInstance(){
return new PushMissionsFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GPSTracker gpsTracker = new GPSTracker(getContext(),getBoostActivity());
myLocation = gpsTracker.getLocation();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater,container,savedInstanceState);
adapter = new PushMissionsAdapter(getContext(), myLocation, this);
final AdapterConverter<PushMissionsAdapter, PushMissionsAdapter.ViewHolder, Mission> adapterPRO = new AdapterConverter<>(getContext(), adapter);
finalUserViewModel.getFinalUser(userViewModel.getUserId()).observe(this, new Observer<FinalUser>() {
@Override
public void onChanged(@Nullable FinalUser finalUser) {
if (finalUser != null){
adapter.setList(finalUser.getAvailableMissions());
adapter.notifyDataSetChanged();
stack.setAdapter(adapterPRO);
}
}
});
stack.setSwipeProgressListener(new SwipeStack.SwipeProgressListener() {
@Override
public void onSwipeStart(int position) {
}
@Override
public void onSwipeProgress(int position, float progress) {
Log.d("PROGRESS", String.valueOf(progress));
if (progress >= 0.5f){
final Mission mission = adapter.getItemAt(position);
mission.setAccepted(1);
adapter.getList().set(position, mission);
adapter.notifyItemChanged(adapter.getList().indexOf(mission));
} else {
}
}
@Override
public void onSwipeEnd(int position) {
}
});
stack.setListener(new SwipeStack.SwipeStackListener() {
@Override
public void onViewSwipedToLeft(int position) {
if (adapter.getList() == null || adapter.getList().size() == 0)
return;
adapter.getList().remove(position == 0 ? 0 : adapter.getList().size() - position);
adapter.notifyDataSetChanged();
}
@Override
public void onViewSwipedToRight(int position) {
if (adapter.getList() == null || adapter.getList().size() == 0)
return;
String missionId = adapter.getItemAt(0).get_id();
missionRepository.saveFavouriteMission(missionId, favouriteListener);
adapter.getList().remove(position == 0 ? 0 : adapter.getList().size() - position);
adapter.notifyDataSetChanged();
}
@Override
public void onStackEmpty() {
}
});
return view;
}
@OnClick(R.id.accept)
public void accept(){
stack.onViewSwipedToRight();
}
@OnClick(R.id.cancel)
public void cancel(){
stack.onViewSwipedToLeft();
}
@Override
protected int getViewId() {
return R.layout.fragment_push_missions;
}
@Override
protected void injectDependencies(BoostComponents boostComponents) {
boostComponents.inject(this);
}
@Override
public void onMissionClick(Mission mission) {
if (mission.getCategory() == Mission.category.presential){
if (mission.getContactPoints().size()==1){
navigator.showFirstMissionStartActivity(this, mission, mission.getContactPoints().get(0).get_id());
}else {
navigator.showMissionPDVsActivity(this, mission.get_id());
}
} else if (mission.getCategory() == Mission.category.prediction){
navigator.showFindMissionFormPredictiveActivity(getBoostActivity(), mission.get_id());
}
}
@Override
public void onFavouriteSaved() {
SyncroManager.setUploadScheduler(getContext(), userViewModel.getUserId());
}}
适配器:
public class PushMissionsAdapter extends AdapterConverter.Adapter<PushMissionsAdapter.ViewHolder, Mission>{
private final Context context;
private final Location myLocation;
private MissionCardListener mListener;
private final List<Mission> list = new ArrayList<>();
public PushMissionsAdapter(Context context, Location myLocation, MissionCardListener mListener){
this.context = context;
this.myLocation = myLocation;
this.mListener = mListener;
}
public List<Mission> getList() {
return list;
}
public void setList(@NonNull final List<Mission> missionList){
this.list.clear();
this.list.addAll(missionList);
}
public Context getContext() {
return context;
}
@Override
public Mission getItemAt(int position) {
return list.get(position);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_push_missions, parent, false);
return new ViewHolder(v, mListener);
}
@Override
public int getItemCount() {
if (list == null)
return 0;
return list.size();
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Mission mission = list.get(position);
holder.mission = mission;
Glide.with(context).load(mission.getCardImageUrl()).into(holder.imagen);
if (mission.getAccepted() == 1){
holder.card_accepted.setVisibility(View.VISIBLE);
} else {
holder.card_accepted.setVisibility(View.GONE);
}
if (mission.getCanceled() == 1){
holder.card_canceled.setVisibility(View.VISIBLE);
} else {
holder.card_canceled.setVisibility(View.GONE);
}
holder.marca.setText(mission.getBrand().getName());
holder.donde.setText(mission.getPlace().toString());
holder.descripcion.setText(mission.getDescription());
holder.puntos.setText(Integer.valueOf(mission.getPoints().intValue()) + " PTS");
if (!mission.getCategory().toString().equals("presential")) {
holder.linear.setVisibility(View.GONE);
holder.imagen.setVisibility(View.GONE);
holder.relative_predictive.setVisibility(View.VISIBLE);
holder.linear_predictive.setVisibility(View.VISIBLE);
} else {
holder.linear.setVisibility(View.VISIBLE);
holder.imagen.setVisibility(View.VISIBLE);
holder.relative_predictive.setVisibility(View.GONE);
holder.linear_predictive.setVisibility(View.GONE);
Location mLocation = new Location("pdv");
mLocation.setLatitude(mission.getContactPoints().get(0).getLatitude());
mLocation.setLongitude(mission.getContactPoints().get(0).getLongitude());
float distanceInMeters = myLocation != null ? myLocation.distanceTo(mLocation) : 0f;
holder.distancia.setText("A " + (distanceInMeters == 0f ? "undefined" : Math.round(distanceInMeters)) + " Mts");
}
}
public static class ViewHolder extends AdapterConverter.ViewHolder {
private MissionCardListener cardListener;
Mission mission;
@BindView(R.id.imagenMision)
ImageView imagen;
@BindView(R.id.image_accept_card)
ImageView card_accepted;
@BindView(R.id.image_cancel_card)
ImageView card_canceled;
@BindView(R.id.marca)
TextView marca;
@BindView(R.id.donde)
TextView donde;
@BindView(R.id.descripcion)
TextView descripcion;
@BindView(R.id.puntos)
TextView puntos;
@BindView(R.id.linear)
LinearLayout linear;
@BindView(R.id.relative_predictive)
RelativeLayout relative_predictive;
@BindView(R.id.linear_predictive)
LinearLayout linear_predictive;
@BindView(R.id.distancia)
TextView distancia;
ViewHolder(View itemView, final MissionCardListener cardListener){
super(itemView);
this.cardListener = cardListener;
ButterKnife.bind(this, itemView);
}
@OnClick(R.id.relative_predictive)
public void onClickPredictive(){
cardListener.onMissionClick(mission);
}
@OnClick(R.id.imagenMision)
public void onClickPresential(){
cardListener.onMissionClick(mission);
}
}
public interface MissionCardListener{
void onMissionClick(Mission mission);
}
}
答案 0 :(得分:0)
只需致电
adapter.notifyItemChanged(position)
代替
adapter.notifyItemChanged(adapter.getList().indexOf(mission));