Вставка строк в RecyclerView последовательно одна за другой
Я пытаюсь вставить динамические строки последовательно в представление переработчика. Я попытался использовать обработчик, но он не загружается один за другим, а отображает все данные за один раз.
Это код, где я установил адаптер во фрагмент
public class HolidayFragment extends Fragment implements IHolidayView {
@BindView(R.id.list)
RecyclerView recyclerView;
@BindView(R.id.adView)
AdView adView;
private HolidayAdapter adapter;
private HolidayPresenter presenter;
private Handler handler = new Handler();
public HolidayFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_holiday, container, false);
ButterKnife.bind(this, view);
setMObileAdMobAdView();
presenter = new HolidayPresenter(this);
presenter.getHolidayList(getActivity());
return view;
}
private void setMObileAdMobAdView() {
AdView adView = new AdView(getActivity());
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(getResources().getString(R.string.adUnitId));
AdRequest request = new AdRequest.Builder()
.addTestDevice(Utility.getDeviceId(getActivity()))
.build();
adView.loadAd(request);
}
@OnClick(R.id.imgBack)
public void openDashboard() {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onSuccess(final List<Holidays> holidays) {
adapter = new HolidayAdapter(getActivity());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
for (final Holidays holiday : holidays) {
adapter.addItems(holiday);
handler.postDelayed(new Runnable() {
public void run() {
}
}, 3000);
}
}
@Override
public void onError(String errorMessage) {
Utility.showErrorMessage(getActivity(), errorMessage);
}
}
Ниже приведен код адаптера, где метод addItem в адаптере используется для обновления данных в списке.
public class HolidayAdapter extends RecyclerView.Adapter<HolidayAdapter.ViewHolder> {
private List<Holidays> holidays = new ArrayList<>();
private Context context;
// Allows to remember the last item shown on screen
private int mExpandedPosition = -1;
private int previousExpandedPosition = -1;
public HolidayAdapter(Context context) {
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.holiday_list_row, parent, false);
return new ViewHolder(v);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(final Holidays object) {
holidays.add(object);
notifyDataSetChanged();
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Holidays holiday = holidays.get(position);
// Set default view
holder.txtName.setText(holiday.getTitle());
holder.txtDate.setText(Utility.getFormattedDateWithSuffix(Utility.formatDate(holiday.getDate())));
// set expanded view
final boolean isExpanded = holder.getAdapterPosition() == mExpandedPosition;
holder.expandedLayout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
holder.expandedLayout.setActivated(isExpanded);
if (isExpanded)
previousExpandedPosition = holder.getAdapterPosition();
// Here you apply the animation when the view is bound
if (mExpandedPosition == holder.getAdapterPosition()) {
setAnimationForExpandedView(holder.parentLayout, holder.getAdapterPosition());
} else if (mExpandedPosition != previousExpandedPosition) {
resetAnimation(holder.parentLayout, holder.getAdapterPosition());
} else {
setAnimationForCollapsedView(holder.parentLayout, holder.getAdapterPosition());
}
if (holiday.getImage() == null || holiday.getImage().isEmpty()) {
Picasso.get()
.load(R.drawable.ic_profile_placeholder)
.centerCrop()
.transform(new RoundImage())
.resize(100, 100)
.into(holder.img);
} else {
Picasso.get()
.load(holiday.getImage())
.centerCrop()
.transform(new RoundImage())
.resize(100, 100)
.into(holder.img);
}
holder.description.setText(holiday.getDescription());
holder.rootLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1 : position;
notifyItemChanged(previousExpandedPosition);
notifyItemChanged(position);
}
});
}
private void setAnimationForCollapsedView(final View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
Animation animation = AnimationUtils.loadAnimation(context, R.anim.bottom_up);
viewToAnimate.startAnimation(animation);
}
private void setAnimationForExpandedView(final View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_down);
viewToAnimate.startAnimation(animation);
}
private void resetAnimation(final View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
}
@Override
public int getItemCount() {
return holidays.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.parentLayout)
CardView parentLayout;
@BindView(R.id.expandedLayout)
LinearLayout expandedLayout;
@BindView(R.id.rootLayout)
LinearLayout rootLayout;
@BindView(R.id.thumb_img)
AppCompatImageView img;
@BindView(R.id.description)
AppCompatTextView description;
@BindView(R.id.txtName)
AppCompatTextView txtName;
@BindView(R.id.txtDate)
AppCompatTextView txtDate;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
1 ответ
Ваш адаптер использует holidaysList
adapter = new HolidayAdapter(getActivity(), holidaysList);
и вы устанавливаете его перед началом цикла:
recyclerView.setAdapter(adapter);
Затем цикл добавляет элементы в другой список под названием праздничные дни:
holidays.add(object);
а также notifyDataSetChanged();
не влияет на адаптер
То, что вы делаете, никак не влияет на представление о переработчике.
Если вы разместите свой полный код, может быть, есть решение