Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use edit text in Expandable Recycler
package com.ryanbrooks.expandablerecyclerviewsample.linear.vertical;
public class Ingredient {
private String mName;
public Ingredient(String name) {
mName = name;
}
public void setmName(String mName) {
this.mName = mName;
}
public String getName() {
return mName;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/standard_material_padding">
<EditText android:id="@+id/ingredient_texedit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/standard_text_size"/>
</LinearLayout>
package com.ryanbrooks.expandablerecyclerviewsample.linear.vertical;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.bignerdranch.expandablerecyclerview.ViewHolder.ChildViewHolder;
import com.ryanbrooks.expandablerecyclerviewsample.R;
public class IngredientViewHolder extends ChildViewHolder {
private EditText mIngredientTextView;
private IngridientTextChangeListener mListener;
public IngredientViewHolder(View itemView, IngridientTextChangeListener listener) {
super(itemView);
mIngredientTextView = (EditText) itemView.findViewById(R.id.ingredient_texedit);
mListener = listener;
mIngredientTextView.addTextChangedListener(mListener);
}
public void bind(Ingredient ingredient) {
mListener.updatePosition(ingredient); // set this first because unless it will be fired
mIngredientTextView.setText(ingredient.getName());
}
}
package com.ryanbrooks.expandablerecyclerviewsample.linear.vertical;
import android.text.Editable;
import android.text.TextWatcher;
/**
* Created by mserge on 27.02.2016.
*/
public class IngridientTextChangeListener implements TextWatcher {
private Ingredient position;
public void updatePosition(Ingredient position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// no op
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if(null != position) position.setmName(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
// no op
}
}
package com.ryanbrooks.expandablerecyclerviewsample.linear.vertical;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter;
import com.bignerdranch.expandablerecyclerview.Model.ParentListItem;
import com.ryanbrooks.expandablerecyclerviewsample.R;
import java.util.List;
public class RecipeAdapter extends ExpandableRecyclerAdapter<RecipeViewHolder, IngredientViewHolder> {
private LayoutInflater mInflator;
public RecipeAdapter(Context context, @NonNull List<? extends ParentListItem> parentItemList) {
super(parentItemList);
mInflator = LayoutInflater.from(context);
}
@Override
public RecipeViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
View recipeView = mInflator.inflate(R.layout.recipe_view, parentViewGroup, false);
return new RecipeViewHolder(recipeView);
}
@Override
public IngredientViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
View ingredientView = mInflator.inflate(R.layout.ingredient_view, childViewGroup, false);
return new IngredientViewHolder(ingredientView, new IngridientTextChangeListener());
}
@Override
public void onBindParentViewHolder(RecipeViewHolder recipeViewHolder, int position, ParentListItem parentListItem) {
Recipe recipe = (Recipe) parentListItem;
recipeViewHolder.bind(recipe);
}
@Override
public void onBindChildViewHolder(IngredientViewHolder ingredientViewHolder, int position, Object childListItem) {
Ingredient ingredient = (Ingredient) childListItem;
ingredientViewHolder.bind(ingredient);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment