我正在尝试通过从回收商视图中的卡片视图中选择图像来打开新片段。最好同时使用适配器和片段类。我该怎么办?
这是我的适配器类:
public class CorporateAdapter extends RecyclerView.Adapter {
private int[] images = {
R.drawable.sample1,
R.drawable.sample2
};
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.corporate_list_row, viewGroup, false);
final MyViewHolder vHold = new MyViewHolder(view);
return vHold;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
((MyViewHolder) viewHolder).bindView(i);
}
@Override
public int getItemCount() {
return images.length;
}
private class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ImageView mImageView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.showImage);
mImageView.setOnClickListener(this);
}
public void bindView(int position) {
mImageView.setImageResource(images[position]);
}
@Override
public void onClick(View v) {
}
}
这是我的Fragment类:
public class CorporateFragment extends Fragment {
private final int LAYOUT_LISTCORPORATE_FRAGMENT = R.layout.corporate_membership;
private View v;
private Date date;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
public CorporateFragment() {
}
public static CorporateFragment newInstance(Date date) {
CorporateFragment fragment = new CorporateFragment();
Bundle args = new Bundle();
//args.putSerializable(ARG_DATE,date);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(LAYOUT_LISTCORPORATE_FRAGMENT, container, false);
ButterKnife.bind(this, v);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
final CorporateAdapter listAdapter = new CorporateAdapter();
recyclerView.setAdapter(listAdapter);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
}
我已经看到了一些方法,我们可以直接从适配器中打开新片段,但是更正确的方法是在片段类和适配器类中都包含Onclicklistener。
有什么办法可以做到吗?谢谢
答案 0 :(得分:0)
以下是您直接粘贴的示例。 构造一个在适配器中将接口作为参数的构造函数,并在Fragment中覆盖它。
您的片段代码如下所示。
public class CorporateFragment extends Fragment {
private final int LAYOUT_LISTCORPORATE_FRAGMENT = R.layout.corporate_membership;
private View v;
private Date date;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
public CorporateFragment() {
}
private int[] images = {
R.drawable.sample1,
R.drawable.sample2
};
public static CorporateFragment newInstance(Date date) {
CorporateFragment fragment = new CorporateFragment();
Bundle args = new Bundle();
//args.putSerializable(ARG_DATE,date);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(LAYOUT_LISTCORPORATE_FRAGMENT, container, false);
ButterKnife.bind(this, v);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
final CorporateAdapter listAdapter = new CorporateAdapter(getActivity(), images, new CorporateAdapter.onItemClickListener() {
@Override
public void onItemClick(View view, int position) {
/////////////////// do your on Click Stuff here...
/////////// for ex .. go to next fragment
}
});
recyclerView.setAdapter(listAdapter);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
}
}
并且您的适配器已设置为
public class CorporateAdapter extends RecyclerView.Adapter<CorporateAdapter.MyViewHolder> {
Context context;
private int[] images;
onItemClickListener onItemClickListener;
public CorporateAdapter(Context context, int[] images, CorporateAdapter.onItemClickListener onItemClickListener) {
this.context = context;
this.images = images;
this.onItemClickListener = onItemClickListener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.corporate_list_row, viewGroup, false);
final MyViewHolder vHold = new MyViewHolder(view);
return vHold;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
((MyViewHolder) holder).bindView(position);
holder.mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListener.onItemClick(view,position);
}
});
}
@Override
public int getItemCount() {
return images.length;
}
private class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView mImageView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.showImage);
}
public void bindView(int position) {
mImageView.setImageResource(images[position]);
}
}
public interface onItemClickListener{
void onItemClick(View view , int position);
}
}
希望它会有所帮助。祝你好运