基于Android中JSON数据动态实现嵌套RecyclerView的问题

时间:2016-11-07 09:57:30

标签: android json android-recyclerview retrofit2 nestedrecyclerview

我正在创建一个会议应用程序,我将会议活动详细信息作为JSON.iam使用改造进行网络呼叫。 基于JSON数据,iam相应地填充我的recyclerview。基本上它是一个垂直Recyclerview内部的水平回收视图。我试图实现的是将所有同时在水平回收视图中的事件分组,而将其他事件分组在垂直回收视图上。请帮助我实现这个。下面是我的垂直和水平适配器代码

垂直适配器

public class VerticalAdapter extends RecyclerView.Adapter {

private ArrayList<EventModel> mEventList;
private Context mContext;
private Map<Integer, Parcelable> scrollStatePositionsMap = new HashMap<>();
private static final String TAG = VerticalAdapter.class.getSimpleName();
private ArrayList<EventModel> tempList;

public VerticalAdapter(ArrayList<EventModel> mEventList, Context mContext) {
    this.mEventList = mEventList;
    this.mContext = mContext;
    this.tempList = new ArrayList<>();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    VerticalItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.vertical_item, parent, false);
    return new EventViewHolder(binding);
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);



    ((EventViewHolder) holder).mBinding.horizontalGridView.setLayoutManager(linearLayoutManager);
    ((EventViewHolder) holder).mBinding.horizontalGridView.setAdapter(new HorizontalAdapter(mEventList, mContext));
    ((EventViewHolder) holder).setPosition(position);
    if (scrollStatePositionsMap.containsKey(position)) {
        ((EventViewHolder) holder).mBinding.horizontalGridView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                ((EventViewHolder) holder).mBinding.horizontalGridView.getViewTreeObserver().removeOnPreDrawListener(this);
                ((EventViewHolder) holder).mBinding.horizontalGridView.getLayoutManager().onRestoreInstanceState(scrollStatePositionsMap.get(position));
                return false;
            }
        });
    }
}

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

private class EventViewHolder extends RecyclerView.ViewHolder {
    public int position;
    private VerticalItemBinding mBinding;

    public EventViewHolder(VerticalItemBinding binding) {
        super(binding.getRoot());
        this.mBinding = binding;
        mBinding.horizontalGridView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    scrollStatePositionsMap.put(position, recyclerView.getLayoutManager().onSaveInstanceState());
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

    }

    public void setPosition(int position) {
        this.position = position;
    }
}
}

水平适配器

public class HorizontalAdapter extends RecyclerView.Adapter {

private ArrayList<EventModel> mEventList;
private Context mContext;
private static String TAG = HorizontalAdapter.class.getSimpleName();

public HorizontalAdapter(ArrayList<EventModel> mEventList, Context mContext) {
    this.mEventList = mEventList;
    this.mContext = mContext;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    HorizontalItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.horizontal_item, parent, false);
    return new EventViewHolder(binding);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    StartsOn startsOn = mEventList.get(position).getStartsOn();
    EndsOn endsOn = mEventList.get(position).getEndsOn();


    ((EventViewHolder) holder).mBinding.textTime.setText(startsOn.getDate().substring(11, 16));
    ((EventViewHolder) holder).mBinding.textEndtime.setText(endsOn.getDate().substring(11, 16));
    ((EventViewHolder) holder).mBinding.setEvents(mEventList.get(position));
}

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

private class EventViewHolder extends RecyclerView.ViewHolder {

    private HorizontalItemBinding mBinding;

    public EventViewHolder(HorizontalItemBinding binding) {
        super(binding.getRoot());
        this.mBinding = binding;
    }
}
}

我的网络电话

 NetworkInterface networkInterface = retrofit.create(NetworkInterface.class);
    Call<ArrayList<EventModel>> call = networkInterface.getEvents(query);
    Log.d(TAG, "populateFirstEvent: making call to "+query);
    call.enqueue(new Callback<ArrayList<EventModel>>() {
        @Override
        public void onResponse(Call<ArrayList<EventModel>> call, Response<ArrayList<EventModel>> response) {
            if (response.isSuccessful()) {
                mBinding.verticalList.setVisibility(View.VISIBLE);
                mBinding.emptyView.setVisibility(View.GONE);

                mEventList.addAll(response.body());
                mBinding.verticalList.setAdapter(new HorizontalAdapter(mEventList,getApplicationContext()));
                mBinding.verticalList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            }
        }
        @Override
        public void onFailure(Call<ArrayList<EventModel>> call, Throwable t) {
            Log.d(TAG, "onFailure: " + t.getLocalizedMessage());
            mBinding.verticalList.setVisibility(View.GONE);
            mBinding.emptyView.setVisibility(View.VISIBLE);
        }
    });

1 个答案:

答案 0 :(得分:1)

您可以使用单个RecycleView和单个适配器。使用GridLayoutManager作为RecylcerView的布局管理器。看看它的构造函数

GridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout)

将HORIZONTAL作为方向,并且spanCount是行数 - “将所有同时发生的事件分组”。所以你应该对事件进行排序。如果不同的行有不同的事件编号 - 用一些虚假的空事件填补空白。

您应该将RecycleView放入ScrollView以使其垂直滚动。