I have been able to create an expanded view using the following library: http://ift.tt/1LzAlWT
Now that I have this, I have created a checkbox in each of my child views. I am able to check and uncheck and expand and retract easy. However the next part is to keep track of all the selections made and put it in an object.
For instance I want the recipe for Tacos that have the ingredients of beef, cheese, lettuce. But I choose to only want the cheese, and lettuce. Now these new selection of ingredients for my Taco I want to hold that as an object.
So what I have done is in my activity I have created a prepare selection method that will be called from my viewholder onClick when a checkbox is selected.
At first I was just trying to get a simple counter and display that just to know that its working. However I get a null pointer error when I click on the checkboxes.
Activity
public class CreateMyTeamActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private RecyclerView mRecyclerView;
private CustomRecyclerAdapterCreateTeam adapter;
private RecipeAdapter mAdapter;
List<Recipe> recipes = new ArrayList<>();
ArrayList<Integer> selection_list = new ArrayList<>();
int count = 0;
Context context;
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_my_team);
context = this;
mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
Ingredient beef = new Ingredient("beef");
Ingredient cheese = new Ingredient("cheese");
Ingredient salsa = new Ingredient("salsa");
Ingredient tortilla = new Ingredient("tortilla");
Ingredient ketchup = new Ingredient("ketchup");
Ingredient bun = new Ingredient("bun");
Recipe taco = new Recipe("taco", Arrays.asList(beef, cheese, salsa, tortilla));
Recipe quesadilla = new Recipe("quesadilla", Arrays.asList(cheese, tortilla));
Recipe burger = new Recipe("burger", Arrays.asList(beef, cheese, ketchup, bun));
recipes = Arrays.asList(taco, quesadilla, burger);
mAdapter = new RecipeAdapter(this, recipes);
mAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
@Override
public void onListItemExpanded(int position) {
Recipe expandedRecipe = recipes.get(position);
Toast.makeText(CreateMyTeamActivity.this,
expandedRecipe.getName(),
Toast.LENGTH_SHORT)
.show();
}
@Override
public void onListItemCollapsed(int position) {
Recipe collapsedRecipe = recipes.get(position);
Toast.makeText(CreateMyTeamActivity.this,
"Collapse",
Toast.LENGTH_SHORT)
.show();
}
});
mRecyclerView.setAdapter(mAdapter);
}
-------------Right here is my prepareSelection Method
public void prepareSelection (View view, int position ){
if (((CheckBox)view).isChecked()){
selection_list.add(recipes.indexOf(position));
count = count+1;
Toast.makeText(CreateMyTeamActivity.this,
count,
Toast.LENGTH_SHORT)
.show();
}
}
}
Ingredient View holder
public class IngredientViewHolder extends ChildViewHolder implements View.OnClickListener{
private TextView mIngredientTextView;
private CheckBox button;
CreateMyTeamActivity createMyTeamActivity;
public IngredientViewHolder(View itemView) {
super(itemView);
mIngredientTextView = (TextView) itemView.findViewById(R.id.teamNameChild);
button = (CheckBox)itemView.findViewById(R.id.checkBoxChild);
button.setOnClickListener(this);
}
public void bind(Ingredient ingredient) {
mIngredientTextView.setText(ingredient.getName());
}
@Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(),mIngredientTextView.getText(),Toast.LENGTH_SHORT ).show();
createMyTeamActivity.prepareSelection(v,getAdapterPosition() );
}
}
Ingredient.java
public class Ingredient {
private String mName;
public Ingredient(String name) {
mName = name;
}
public String getName() {
return mName;
}
}
Aucun commentaire:
Enregistrer un commentaire