[英]OnClickListener in RecyclerView in Fragment
我创建了主从流程,在Master Fragment中,我有一个RecyclerView。
我在RecyclerView中设置ClickListener时遇到问题。
我不确定是否遗漏了一些琐碎的问题或存在更大的问题,但是几乎相同的代码在标准Activity(超出Fragment)中可以完美地工作。
这是我的适配器:
//imports
public class StepsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final StepsAdapterOnClickHandler mClickHandler;
private Recipe recipe;
private List<Step> steps;
//init with Click Handler
public StepsAdapter(StepsAdapterOnClickHandler clickHandler) {
this.mClickHandler = clickHandler;
}
//set recipe and steps
public void setRecipe(Recipe recipe) {
this.recipe = recipe;
this.steps = recipe.steps;
notifyDataSetChanged();
}
//get position as ViewType for distinguishing between first View and others
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
if (steps == null) {
return 0;
}
return steps.size() + 1;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//setup different layout for first view
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view0 = inflater.inflate(R.layout.ingredients_tile, parent, false);
View view = inflater.inflate(R.layout.step_selection_tile, parent, false);
switch (viewType) {
case 0:
return new IngredientsViewHolder(view0);
default:
return new ViewHolderX(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 0:
IngredientsViewHolder viewHolder0 = (IngredientsViewHolder) holder;
//set text
viewHolder0.ingredientsLabelTV.setText("Ingredients");
break;
default:
ViewHolderX viewHolderX = (ViewHolderX) holder;
viewHolderX.shortDescriptionTV.setText(steps.get(position - 1).shortDescription);
break;
}
}
//click interface
public interface StepsAdapterOnClickHandler {
void onClick(int stepId);
}
//viewHolder for Ingredients
public class IngredientsViewHolder extends RecyclerView.ViewHolder {
TextView ingredientsLabelTV;
public IngredientsViewHolder(View itemView) {
super(itemView);
ingredientsLabelTV = itemView.findViewById(R.id.ingredients_label_tv);
}
}
//stepsViewHolder
class ViewHolderX extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView shortDescriptionTV;
public ViewHolderX(View itemView) {
super(itemView);
shortDescriptionTV = itemView.findViewById(R.id.short_description_tv);
}
@Override
public void onClick(View v) {
mClickHandler.onClick(33);
}
}
这是我的片段代码:
//imports
public class StepSelectionFragment extends Fragment implements StepsAdapter.StepsAdapterOnClickHandler {
OnStepClickListener mCallback;
RecyclerView stepsListRV;
StepsAdapter mAdapter;
RecipeDatabase mDb;
//mandatory empty builder
public StepSelectionFragment() {
}
//onClic - sends callback to Activity
@Override
public void onClick(int stepId) {
mCallback.onStepClick(stepId);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
//attach callback to fragment
try {
mCallback = (OnStepClickListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "must implement Listener");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_step_selection, container, false);
//init db
mDb = RecipeDatabase.getInstance(getActivity());
//init recyclerView
stepsListRV = rootView.findViewById(R.id.step_selection_rv);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
stepsListRV.setLayoutManager(layoutManager);
//setup adapter with clickHandler
mAdapter = new StepsAdapter(this);
stepsListRV.setAdapter(mAdapter);
//get recipes from Db
AppExecutors.getsInstance().getDiskIO().execute(new Runnable() {
@Override
public void run() {
Recipe recipe = mDb.recipeDAO().loadRecipe(RecipeViewActivity.getRecipeId());
mAdapter.setRecipe(recipe);
}
});
return rootView;
}
public interface OnStepClickListener {
void onStepClick(int stepId);
}
我想我尝试了一切。
整个代码可以在https://github.com/ASheler/BakingApp/tree/AddMasetDetailFlow/app/src/main/java/com/glaserproject/bakingapp中找到
添加itemView.setOnClickListener(this);
在适配器ViewHolder中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.