我在片段1中创建了一个RecyclerView,并创建了RecyclerViewAdapter将日期集绑定到片段1中在RecyclerView中显示的视图。我已经在主要活动中加载了片段一。现在我有一个问题,我需要在main活动中创建一个接口,并通过该接口将数据从适配器传递到main活动,以便可以将数据包装到bundle中并加载两个片段。但是我不知道该怎么做。有人可以帮我吗?
这是我的主要活动。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentOne fragmentOne = new FragmentOne();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.fragment_container,fragmentOne).commit();
}}
这是我的片段一。
public class FragmentOne extends Fragment {
public RecyclerView recyclerView;
public ArrayList<Promotion> promotionArrayList;
public MyAdapter myAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
DividerItemDecoration divider = new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(divider);
promotionArrayList = Promotion.createPromotionList();
myAdapter = new MyAdapter(promotionArrayList);
recyclerView.setAdapter(myAdapter);
return view;
}}
这是我的适配器。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public ArrayList<Promotion> promotionArrayList;
public MyAdapter(ArrayList<Promotion>promotionArrayList) {
this.promotionArrayList = promotionArrayList;
}
@NonNull
@Override
public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Promotion promotion = promotionArrayList.get(i);
viewHolder.imageView.setImageResource(promotion.getImgId());
viewHolder.promotion_period.setText(promotion.getPromotion_period());
viewHolder.promotion_deals.setText(promotion.getPromotion_deals());
viewHolder.shop_name.setText(promotion.getShop_name());
}
@Override
public int getItemCount() {
return promotionArrayList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView shop_name,promotion_deals, promotion_period;
public ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
shop_name = (TextView)itemView.findViewById(R.id.shop_name);
promotion_deals = (TextView)itemView.findViewById(R.id.promotion_deals);
promotion_period = (TextView)itemView.findViewById(R.id.promotion_period);
imageView = (ImageView)itemView.findViewById(R.id.imageView);
}
}}
答案 0 :(得分:0)
当您在上面讲话时: 在MyAdapter中,您可以添加
app/MyLib_stub.h
在onBindViewHolder中,您可以通过点击设置所需的特定视图的侦听器:
public interface onItemClickListener {
public void onItemCLickListener(int position);
}
在MainActivity中实现OnItemClickListener 然后将数据打包成束并将其发送给片段 您可以在这里关注: Click listener for RecyclerView adapter
答案 1 :(得分:0)
第1步。创建界面
public interface RecyclerViewItemInterface {
void onItemClick(int position, String path);
}
第2步。在适配器中编写set方法
private RecyclerViewItemInterface viewItemInterface;
public void setViewItemInterface(RecyclerViewItemInterface viewItemInterface) {
this.viewItemInterface = viewItemInterface;
}
第3步。像这样传递数据和调用方法
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewItemInterface != null) {
viewItemInterface.onItemClick(holder.getAdapterPosition(),"");
}
}
});
第4步。像这样实现接口
bgImageAdapterNEW.setViewItemInterface(new RecyclerViewItemInterface() {
@Override
public void onItemClick(int position, String path) {
}
});