使用sort从arraylist动态添加标头到recyclerView

时间:2016-06-07 02:33:54

标签: android arraylist android-recyclerview

我有json转换成模型

alpha

所以我想在json中按类型添加标题,所以如果arraylist中有2个不同的类型,那么在recyclerView中将有2个标题,并且相关数据将作为列表。所以当前的例子我有2个typ2 - 1)名称2)值,所以我如何将2个标题添加到recyclerView,然后所有数据的类型为名称

json

// Make sure this matches the initial hidden state of your table view.
// So if your table view starts at alpha == 0.0, this should be true.
private var tableViewHidden: Bool = false

@IBAction func clickButtonAction(sender: AnyObject) {
    tableViewHidden = !tableViewHidden
    UIView.animateWithDuration(10.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
        if tableViewHidden {
            self.tableView?.alpha = 0.0
        }
        else {
            self.tableView?.alpha = 1.0
        }
    }, completion: { (finished:Bool) -> Void in

    })
}

} See image fro details

1 个答案:

答案 0 :(得分:7)

我认为您的布局设计已准备好作为给定的图像链接。基本上你需要02个布局设计,其中一个用于标题和放大器。另一个是每个项目的页脚。现在,您可以使用适配器动态添加标头,如下所示:

public class YourRecyclerViewAdapter extends RecyclerView.Adapter {

    private final int VIEW_ITEM_HEADER = 1;  // type: name/value
    private final int VIEW_ITEM_FOOTER = 0;

    private List<PojoModel> mData;
    private Context mContext;

    public YourRecyclerViewAdapter(Context context, List<PojoModel> data)     {
        this.mContext = context;
        this.mData = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        if (viewType == VIEW_ITEM_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_header, parent, false);
            viewHolder = new HeaderHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer, parent, false);
            viewHolder = new FooterHolder(view);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PojoModel item = mData.get(position);
        if (holder instanceof HeaderHolder) {
            HeaderHolder mHolder = (HeaderHolder)holder;
            // do your stuff
        } else {
            FooterHolder mHolder = (FooterHolder)holder;
            // do your stuff
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mData.get(position) != null ? VIEW_ITEM_HEADER : VIEW_ITEM_FOOTER;
    }

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

    public class HeaderHolder extends RecyclerView.ViewHolder {

        public HeaderHolder(View itemView) {
            super(itemView);
            // declare your header views
        }
    }

    public class FooterHolder extends RecyclerView.ViewHolder {

        public FooterHolder(View itemView) {
            super(itemView);
            // declare your footer views
        }
    }
}