2017-04-26 109 views
-1

我有一个recycleView,我设置了它的适配器,但我不知道它为什么没有显示任何内容。我认为我做的我做了,每次我都用它同样的事情......下面是代码: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方法正确地解雇,但没有别的正在发生的事情。

回答

1

从您的代码看来,初始化您的回收站视图(dayAppointments)后,您忘记添加布局管理器。

尝试添加以下代码,希望这有助于

dayAppointments.setLayoutManager(new LinearLayoutManager(this));// since you have activity 

如果你想在片段中使用它,

dayAppointments.setLayoutManager(new LinearLayoutManager(getActivity())); 
1

你只需要列表初始化水平后添加布局管理

dayAppointments.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); 

。您也可以使其垂直或使用GridLayoutManager