-1

I want to do something like this (trello) Example, I mean the container that always appear in the end of created lists for creating a new one.

I have a recycler view, adapter and data from DataBase.

Adapter:

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

        }
}

So, I have this: Picture. And I want to add in the end of this RecyclerView a custom view, which would be apply to add new board. How can I create this view from my layout and add this View to RecyclerView that my result will be something like this: Picture

Hollow
  • 3
  • 1
  • 4
  • not clear, Please explain little detail – Sanjay Kumar Jan 03 '18 at 11:15
  • Create a custom view(Layout) and inflate the layout. – Animesh Patra Jan 03 '18 at 11:16
  • 1
    Your question is more of a opinionated question which is not recommended. Please do some more research and code your idea; if you do get stuck, you are more than welcome to ask again but with code. – Nero Jan 03 '18 at 11:17
  • I need to make View that always appear in the end of RecyclerView like a last element of the RecyclerView, look the example picture – Hollow Jan 03 '18 at 11:18

1 Answers1

0

You can define multiple view types on your recyclerview's adapter. With this, you will be able to choose which element want to inflate on the given position.

Something like this :

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

Ref to this question's answer.

Robert Banyai
  • 1,329
  • 12
  • 14
  • But I haven't this element in my dataSet which go in adapter, how can I return second ViewHolder if this element haven't a position? If I get you right this code will work only for elements in my dataSet. – Hollow Jan 03 '18 at 11:57
  • Of course I can add the mock element in the end of my dataSet in MainActivity and then handle it by that way, but I think it's a wrong solution. – Hollow Jan 03 '18 at 12:03
  • if the view has to be inflated always at the end then the override fun getItemCount(): Int { return boards.count()+1 } check for the last position in your getItemViewType() and return the view otherwise return the item view – Amardeep Jan 03 '18 at 12:13
  • @Hollow did you try – Amardeep Jan 03 '18 at 12:50