简体   繁体   English

在 android 的 Nested Recyclerview 中显示数据

[英]Show data in Nested Recyclerview in android

As I fetched and show the dates (see image) as the title of the main recyclerview.当我获取并显示日期(见图)作为主要回收站视图的标题时。 I want to show the available slots data instead of the 0 1 2 etc elements.我想显示可用的插槽数据而不是 0 1 2 等元素。 The code is attached below代码附在下面

Url for json data Url 用于 json 数据

https://run.mocky.io/v3/c9bd7858-0e41-422f-b1d2-cd490c08583bhttps://run.mocky.io/v3/c9bd7858-0e41-422f-b1d2-cd490c08583b

图片

AppointmentTimeActivity.java约会时间Activity.java

public class AppointmentTimeActivity extends AppCompatActivity {

SharedPrefManager sharedPrefManager;

Button appointmentTimeButton;
private TextView doctorFullName;
ConstraintLayout constraintLayout;


public static List<List<String>> availableSlots;

RecyclerView rvGroup;
public static ArrayList<String> arrayListGroup;
LinearLayoutManager layoutManagerGroup;
GroupAdapter groupAdapter;

private DoctorScheduleResponse timings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.select_appointment_time);

    getSupportActionBar().setTitle("Appointment time");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    sharedPrefManager = new SharedPrefManager(this);

    constraintLayout = findViewById(R.id.constraintLayout);
    appointmentTimeButton = findViewById(R.id.book_video_call_appointment_btn);

    doctorFullName = findViewById(R.id.doctor_full_name);
    rvGroup = findViewById(R.id.rv_group);

    String docName = getIntent().getStringExtra("doctorFullName");

    doctorFullName.setText(docName);

    arrayListGroup = new ArrayList<>();

    fetchAndShowAppointmentsTime();

}

private void fetchAndShowAppointmentsTime() {
    String id = String.valueOf(SpecialityActivity.doctorID);

    Call<DoctorScheduleResponse> call = RetrofitClient.getInstance().getMyInterface().getAppointmentTime("Bearer " + sharedPrefManager.getAccessToken(), id);
    call.enqueue(new Callback<DoctorScheduleResponse>() {
        @Override
        public void onResponse(@NotNull Call<DoctorScheduleResponse> call, @NotNull Response<DoctorScheduleResponse> response) {
            arrayListGroup = new ArrayList<>();

            if (response.isSuccessful()) {
                assert response.body() != null;
                for (List<Slot> slots : response.body().getSlot()) {
                    for (Slot slot : slots) {
                        arrayListGroup.add(slot.getScheduleDate());
                    }
                }
                
                groupAdapter = new GroupAdapter(AppointmentTimeActivity.this, response.body().getSlot());
                layoutManagerGroup = new LinearLayoutManager(getApplicationContext());
                rvGroup.setLayoutManager(layoutManagerGroup);
                rvGroup.setAdapter(groupAdapter);

            }

        }

        @Override
        public void onFailure(@NotNull Call<DoctorScheduleResponse> call, @NotNull Throwable t) {
            Toast.makeText(getBaseContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

} }


DoctorScheduleResponse.java DoctorScheduleResponse.java

public class DoctorScheduleResponse {

@SerializedName("slot")
@Expose
private List<List<Slot>> slot = null;

public List<List<Slot>> getSlot() {
    return slot;
}

public void setSlot(List<List<Slot>> slot) {
    this.slot = slot;
}

} }


GroupAdater.java GroupAdater.java

public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.ViewHolder> {

private Activity activity;
ArrayList<String> arrayListGroup, arrayListMember;
LinearLayoutManager linearLayoutManager;
SharedPrefManager sharedPrefManager;
List<List<Slot>> slotsList;

public GroupAdapter(Activity activity, ArrayList<String> arrayListGroup) {
    this.activity = activity;
    this.arrayListGroup = arrayListGroup;
}

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

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {


    holder.dummyTV.setText(arrayListGroup.get(position));
    arrayListMember = new ArrayList<>();
    sharedPrefManager = new SharedPrefManager(activity);

    for (int i = 0; i < 5; i++) {
        arrayListMember.add(String.valueOf(i));
    }

    CustomAdapter customAdapter = new CustomAdapter(arrayListMember);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
    holder.rvMember.setLayoutManager(linearLayoutManager);
    holder.rvMember.setAdapter(customAdapter);

}


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

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView dummyTV;
    RecyclerView rvMember;


    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        dummyTV = itemView.findViewById(R.id.dummyTextView);
        rvMember = itemView.findViewById(R.id.rv_member);

    }
}

} }


CustomAdapter.java CustomAdapter.java

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.SlotsViewHolder> {

ArrayList<String> slots;


public CustomAdapter(ArrayList<String> slots) {
    this.slots = slots;
}

@NonNull
@Override
public SlotsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.available_slots_list, parent, false);
    return new CustomAdapter.SlotsViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull SlotsViewHolder holder, int position) {
    holder.tvSlots.setText(slots.get(position));
}

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

public class SlotsViewHolder extends RecyclerView.ViewHolder {
    TextView tvSlots;

    public SlotsViewHolder(@NonNull View itemView) {
        super(itemView);

        tvSlots = itemView.findViewById(R.id.tv_slots);

    }
}

} }

Nested recycler view will be more complex in terms of memory.就 memory 而言,嵌套回收器视图将更加复杂。 So you can achieve same UI with different approach.因此,您可以使用不同的方法实现相同的 UI。

You should try with single recycler view with dynamic layout binding.您应该尝试使用具有动态布局绑定的单个回收器视图。 Example: For single item of recycler view, there will be a header(for date) and a viewgroup(may be linear layout) then bind any no.示例:对于单项回收站视图,将有一个标题(用于日期)和一个视图组(可能是线性布局)然后绑定任何编号。 of child views inside that linear layout.该线性布局内的子视图。

The problem is in GroupAdater.java.问题出在 GroupAdater.java 中。 Inside onBindViewHolder method you are adding the value of loop variable to your list that you pass to your nested recycler view adaptor.在 onBindViewHolder 方法中,您将循环变量的值添加到您传递给嵌套回收器视图适配器的列表中。

for (int i = 0; i < 5; i++) {
    arrayListMember.add(String.valueOf(i));
}

You have to add the correct date from arrayListGroup object.您必须从 arrayListGroup object 添加正确的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM