-2

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

垂直适配器

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);
        }
    });
4

1 回答 1

1

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

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

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

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

于 2016-11-08T07:11:33.520 回答