如何向recyclerView添加自定义视图

时间:2018-01-03 11:12:25

标签: android android-recyclerview

我想做这样的事情(trello)Example,我的意思是容器总是出现在创建列表的末尾,用于创建新的容器。

我有来自DataBase的回收站视图,适配器和数据。

适配器:

class BoardAdapter(private val boards: ArrayList<String>) : RecyclerView.Adapter<BoardAdapter.ViewHolder>() {

    inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        val name = v.findViewById<TextView>(R.id.name)
        val view = v
    }

    override fun getItemCount(): Int {
        return boards.count()
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        holder!!.name.text = boards[position]

        val v = holder.view

        v.setOnClickListener {
            val intent = Intent(v.context, BoardActivity::class.java)
            intent.putExtra("name", holder.name.text.toString())
            v.context.startActivity(intent)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent!!.context).inflate(R.layout.board_list_item,
                parent, false)
        return ViewHolder(v)
    }
}

MainActivity

 class MainActivity : AppCompatActivity() {

        private lateinit var db: SQLiteDatabase
        private lateinit var cursor: Cursor

        private lateinit var adapter: BoardAdapter
        private val boards = ArrayList<String>()
        private val helper = DBHelper(this)

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            try {
                db = helper.readableDatabase
                cursor = db.query("BOARD", arrayOf("name"),
                        null, null, null, null, null)

                while (cursor.moveToNext()) {
                    boards.add(cursor.getString(0))
                }

                cursor.close()
                db.close()
            } catch (e: SQLException) {
                Log.e("SQL ERROR", e.message.toString())
                Toast.makeText(this, R.string.db_error, Toast.LENGTH_SHORT).show()
            }

            adapter = BoardAdapter(boards)
            recyclerView.layoutManager = GridLayoutManager(applicationContext, 2)
            recyclerView.adapter = adapter

        }
}

所以,我有这个:Picture。我想在这个RecyclerView的最后添加一个自定义视图,它将适用于添加新的主板。如何从我的布局创建此视图并将此视图添加到RecyclerView,我的结果将是这样的:Picture

1 个答案:

答案 0 :(得分:0)

您可以在recyclerview的适配器上定义多种视图类型。有了这个,您将能够选择想要在给定位置上膨胀的元素。

这样的事情:

    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class ViewHolder0 extends RecyclerView.ViewHolder {
        ...
        public ViewHolder0(View itemView){
        ...
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        ...
        public ViewHolder2(View itemView){
        ...
    }

    @Override
    public int getItemViewType(int position) {
        // Just as an example, return 0 or 2 depending on position
        // Note that unlike in ListView adapters, types don't have to be contiguous
        return position % 2 * 2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         switch (viewType) {
             case 0: return new ViewHolder0(...);
             case 2: return new ViewHolder2(...);
             ...
         }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        switch (holder.getItemViewType()) {
            case 0:
                ViewHolder0 viewHolder0 = (ViewHolder0)holder;
                ...
                break;

            case 2:
                ViewHolder2 viewHolder2 = (ViewHolder2)holder;
                ...
                break;
        }
    }
}

参考this问题的答案。