适配器中有两个视口

时间:2019-12-04 10:34:46

标签: android android-recyclerview android-viewholder

我解析XML并从两个API获取数据。

arrival用于公共汽车站,position用于公共汽车的位置。

问题是views被添加为与总线数(即positionInfoArrayListSize)一样多的数目。 (图片中有七辆公共汽车。)

picture

视图是否与返回的getItemCount()一样多?

我该如何解决?

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_ARRIVAL = 0;
    private static final int VIEW_TYPE_POSITION = 1;

    private ArrayList<ArrivalInfo> arrivalInfoArrayList;
    private ArrayList<PositionInfo> positionInfoArrayList;

    private int arrivalInfoArrayListSize;
    private int positionInfoArrayListSize;

    public MyAdapter(ArrayList<ArrivalInfo> arrivalInfoArrayList, ArrayList<PositionInfo> positionInfoArrayList) {
        this.arrivalInfoArrayList = arrivalInfoArrayList;
        this.positionInfoArrayList = positionInfoArrayList;

        arrivalInfoArrayListSize = arrivalInfoArrayList.size();
        positionInfoArrayListSize = positionInfoArrayList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.test2, parent, false);

        if (viewType == VIEW_TYPE_ARRIVAL) {
            return new ArrivalViewHolder(view);
        }

        if (viewType == VIEW_TYPE_POSITION) {
            return new PositionViewHolder(view);
        }

        return null;
    }

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

        if (holder instanceof ArrivalViewHolder) {
            ((ArrivalViewHolder) holder).bind(arrivalInfoArrayList.get(position));
        }

        if (holder instanceof PositionViewHolder) {
            ((PositionViewHolder)holder).bind(positionInfoArrayList.get(position - arrivalInfoArrayListSize));

        }

    }

    @Override
    public int getItemCount() {
        return arrivalInfoArrayListSize + positionInfoArrayListSize;
    }

    @Override
    public int getItemViewType(int position){
        if(position < arrivalInfoArrayListSize){
            return VIEW_TYPE_ARRIVAL;
        }

        if(position - arrivalInfoArrayListSize < positionInfoArrayListSize){
            return VIEW_TYPE_POSITION;
        }

        return -1;
    }

    public class ArrivalViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_stNm;
        private TextView tv_arsId;

        public ArrivalViewHolder(View v) {
            super(v);
            tv_stNm = v.findViewById(R.id.stNm);
            tv_arsId = v.findViewById(R.id.arsId);
        }

        public void bind(ArrivalInfo arrivalInfo) {
            tv_arsId.setText(arrivalInfo.getArsId());
            tv_stNm.setText(arrivalInfo.getStNm());
        }

    }

    public class PositionViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_bus1;
        private ImageView iv_bus2;

        public PositionViewHolder(View v) {
            super(v);
            iv_bus1 = v.findViewById(R.id.busImage1);
            iv_bus2 = v.findViewById(R.id.busImage2);
        }

        public void bind(PositionInfo positionInfo) {
            if(positionInfo.getStopFlag().equals("1")) iv_bus1.setVisibility(positionInfo.getSectOrd());
            if(positionInfo.getStopFlag().equals("0")) iv_bus2.setVisibility(positionInfo.getSectOrd());

        }
    }

}

1 个答案:

答案 0 :(得分:0)

创建的视图回收器视图数量为  您在getItemCount中返回的数字(忽略回收过程) 我从您的问题中了解到您想要做这样的事情

公共汽车到达信息 位置信息 位置信息 巴士到达资讯 ...等等 列表将混合到达信息和位置信息,因此您无法先显示到达信息,然后再显示位置信息。 如果我理解的是正确的。那么我建议您使用抽象类Info,它将实现可比性,以便您可以对数组列表进行排序并具有gettype方法 并且在适配器中,您具有抽象类的arraylist,并且可以以适当的方式使用它,如果可以将代码添加到github或任何开源平台,我可以为您提供进一步的帮助