elmontoya7
elmontoya7

Reputation: 276

RecyclerView - first row EditText

I been thinking a lot and can't figure out how to achieve this: I'm making a simple to-do app and saw on dribbble a beautiful design. When you add a to-do, the first row is an EditText:

enter image description here

Once saved, the todos are shown as TextView. Any ideas how to achieve this?

Upvotes: 1

Views: 166

Answers (2)

Abhi
Abhi

Reputation: 2295

You have to create different viewHolder class for different layout and attach different view in onCreateVIewHolder. Here is an example:

private Activity currActivity;
private Context currContext;
private List<CommonCategory> itemList;

public HomeFeedRecyclerAdapter(Activity currActivity, Context currContext, ArrayList<CommonCategory> itemList) {
    this.itemList = itemList;
    this.currActivity = currActivity;
    this.currContext = currContext;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
RecyclerView.ViewHolder viewHolder;

switch (viewType) {
    case TYPE_ONE: {
        view = getLayoutView(parent, R.layout.list_one);
        viewHolder = new EditTextViewHolders(view, currActivity);
        break;
    }
    case TYPE_TWO: {
        view = getLayoutView(parent, R.layout.list_two);
        viewHolder = new TextViewHolders(view, currActivity, getItem(viewType));
        break;
    }
    default: {
        viewHolder = null;
    }

}
return viewHolder;

}

Then create two different viewHolder class(in this case EditTextViewHolders and TextViewHolders) and define your views there. Then on entering text update the ArrayList and RecyclerView(notifyDataSetChanged()).

Upvotes: 1

Mrinmoy
Mrinmoy

Reputation: 1368

So my approach would be some thing like this.

public void onBindViewHolder(HolderClass holder, int position) {
if (position == 0){
editTest.setVisibility(View.VISIBLE);
textView.setVisibility(View.GONE);
} else {
editTest.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
textView.setText(listItem.getTitle());
}

For first position Keep editext visible and for rest position keep textview visible.

Make layout file like this:-

<RelativeLayout
width="match_parent"
height="50dp">

<EditText
width="match_parent"
height="wrap_content"/>

<TextView
width="match_parent"
height="wrap_content"/>

</RelativeLayout>

Hope it helps.

Upvotes: 0

Related Questions