如何在RecyclerView.Adapter中添加自定义视图

时间:2017-03-16 07:44:47

标签: android android-recyclerview android-linearlayout

我有一个RecyclerView并成功调整了一些数据。我有个问题。在onBindViewHolder方法中,我试图在线性布局中添加自定义视图。自定义视图已成功添加,但在向上或向下滚动视图中是重复的。我想只用项目添加一次。

我的代码:

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

    public ArrayList<Train> trains;
    public Context context;
    private LayoutInflater inflater;
    public OnItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }


    public class GenericViewHolder extends RecyclerView.ViewHolder {
        public TextView stationName;
        public LinearLayout vagoncontainer;

        public GenericViewHolder(View view) {
            super(view);
            stationName = (TextView) view.findViewById(R.id.station_item_name);
            vagoncontainer = (LinearLayout) view.findViewById(R.id.vagon_container);


        }
    }

    public TrainsAdapter(Context context, ArrayList<Train> trains) {
        this.context = context;
        this.trains = trains;
        inflater = LayoutInflater.from(context);


    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.trains_adapter_item, parent, false);
        GenericViewHolder holder = new GenericViewHolder(view);
        return holder;


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof GenericViewHolder) {
            GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
            Train train = trains.get(position);
            ArrayList<Vagons> vagons = train.getVagons();
            for (int i = 0; i < vagons.size(); i++) {
                LayoutInflater layoutInflater = LayoutInflater.from(context);
                LinearLayout vagonView = (LinearLayout) layoutInflater.inflate(R.layout.trains_vagon_child, null);
                Button button = (Button) vagonView.findViewById(R.id.button);
                if (vagons.get(i).getEnable().equals("1"))
                    button.setVisibility(View.VISIBLE);
                else
                    button.setVisibility(View.GONE);
                button.setText(vagons.get(i).getName() + " " + vagons.get(i).getAmount());
                genericViewHolder.vagoncontainer.addView(vagonView);
            }


            genericViewHolder.stationName.setText(train.getDeparture());

        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

    public Train getItem(int position) {
        return trains.get(position);
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}

如何解决我的问题?

3 个答案:

答案 0 :(得分:0)

这将解决您的问题,但它无效(对于有大量阴道的火车,它不会顺利滚动 - 膨胀视图很昂贵),因为您将删除并重新创建{{1}中的视图代表LinearLayout中的行。重新考虑行布局或在行视图中创建一些伪回收。

ListView

答案 1 :(得分:0)

您可以使用core adapter

这是一个简单的库,可以在recyclerview中支持任何类型的项目而不会产生任何痛苦。

答案 2 :(得分:0)

基本上RecyclerView会重用您的viewHolder。因此,根据RecyclerView文档here您必须在onBindViewHolder()方法中每次重置视图,

并基于此

  

但是在向上或向下滚动视图中是公开的。我想只添加一次项目

您需要在removeAllViews视图组

vagonContainer

所以你的绑定持有人看起来像

if (holder instanceof GenericViewHolder) {
        GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
        Train train = trains.get(position);
        ArrayList<Vagons> vagons = train.getVagons();

        // this is very important. removing all view
        genericViewHolder.vagoncontainer.removeAllViews();
......

类似问题here