我有一个recycleView,我设置了一个适配器,但我不知道为什么它什么也没显示。我想我每次都要用它做同样的事情......以下是代码:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/iron">
<android.support.v7.widget.RecyclerView
android:id="@+id/appointment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
活动:
在这里,我使用了一个给我一个列表的演示者。我调试它,它的工作原理。
public class AppointmentDateSelectionView extends NotificationActivity implements MVP.View {
RecyclerView dayAppointments;
AppointmentDateSelectionPresenter presenter;
private ListAdapter adapter;
List<ResponseAppointmentHours> availableHours;
private int idDoctor;
@Override
protected void onCreate(@Nullable Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.activity_appointment_date_selection);
BaseManager<String> managerToken = new TokenManager(getApplicationContext());
dayAppointments = (RecyclerView) findViewById(R.id.appointment_list);
Intent i = getIntent();
idDoctor = i.getIntExtra(Params.INTENT.ID_DOCTOR, 0);
presenter = new AppointmentDateSelectionPresenter(managerToken, idDoctor);
presenter.onViewCreated(this);
presenter.initialize();
dayAppointments.addItemDecoration(new SimpleDividerItemDecoration(this));
Calendar endDate = Calendar.getInstance();
endDate.add(Calendar.MONTH, 5);
}
@Override
public void setDoctorAppointmentList(List<ResponseAppointmentHours> hoursList) {
availableHours = hoursList;
adapter = new ListAdapter(availableHours);
dayAppointments.setAdapter(adapter);
dayAppointments.setItemAnimator(new CustomDefaultItemAnimator());
dayAppointments.setHasFixedSize(true);
adapter.notifyDataSetChanged();
}
ListAdapter:
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder> {
private final OnDeleteClickListener listener1;
private static List<ResponseAvailableHours> list = new ArrayList<>();
static class ItemViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.availability_from)
TextView availabilityText;
@BindView(R.id.delete)
ImageView delete;
ItemViewHolder(View v) {
super(v);
ButterKnife.bind(this, v);
}
public void bind(final ResponseAvailableHours item, final OnDeleteClickListener listener) {
delete.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
list.remove(item);
listener.onItemClick(item);
}
});
}
}
ListAdapter(OnDeleteClickListener listener1, List<ResponseAvailableHours> data) {
this.listener1 = listener1;
list = data;
}
@Override
public ListAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.avaliability_item, parent, false);
return new ItemViewHolder(v);
}
@Override
public void onBindViewHolder(ListAdapter.ItemViewHolder holder, int position) {
final ResponseAvailableHours hour = list.get(position);
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
try {
SimpleDateFormat newFormat = new SimpleDateFormat("HH:mm");
Date endDate = formatter.parse(hour.getTimeEnd());
Date iniTime = formatter.parse(hour.getTimeIni());
String available = "Cada " + WeekDays.getDays(hour.getWeekday()) + " de " + newFormat.format(iniTime) + "h a " + newFormat.format(endDate) + "h";
holder.availabilityText.setText(available);
} catch (ParseException e) {
e.printStackTrace();
}
holder.bind(hour, listener1);
}
@Override
public int getItemCount() {
return list.size();
}
在从adapter = new ListAdapter(availableHours);
传递的活动中,ListAdapter方法被正确触发,但没有其他任何事情发生。
答案 0 :(得分:3)
在您的代码中,似乎在初始化您的回收站视图(dayAppointments)之后,您忘记添加布局管理器。
尝试添加以下代码,希望它有所帮助
dayAppointments.setLayoutManager(new LinearLayoutManager(this));// since you have activity
如果你想在片段中使用它,
dayAppointments.setLayoutManager(new LinearLayoutManager(getActivity()));
答案 1 :(得分:1)
您只需添加LayoutManager
即可dayAppointments.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
列表初始化为水平。您也可以将其设置为垂直或使用GridLayoutManager