尝试发送数据并在单击recyclerview时将片段替换为片段

时间:2018-08-04 22:59:04

标签: android android-fragments android-recyclerview

我想将我在RecyclerView中选择的商店的StoreID从Homefragment发送到StoreFragment并显示StoreFragment,这样我就出错了

No view found for id 0x7f080080 (one.test.com.test2:id/frame_fragStore) for fragment StoreFragment{dcda377 #1 id=0x7f080080}

HomeFragment frame的ID为:frame_fragHome,StoreFragment frame的ID为:frame_fragStore

RecyclerViewAdapter:

public class TopRateAdapter extends RecyclerView.Adapter<TopRateAdapter.TopRateViewHolder> {
private List<Store> storeList;
private int AdapterMod;
private Context mContext;

public TopRateAdapter(List<Store> storeList,Context context) {
    this.storeList = storeList;
    mContext = context;
}
public void updateAdapterData(List<Store> storeList){
    this.storeList = storeList;
}
public void setAdapterMod(int AdapterMod){
    this.AdapterMod = AdapterMod;
}

@Override
public TopRateAdapter.TopRateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_store_rate,parent,false);
    return new TopRateViewHolder(v);
}

@Override
public void onBindViewHolder(final TopRateAdapter.TopRateViewHolder holder, int position) {
    final Store currentModel = storeList.get(position);
    if(AdapterMod==1) {
        holder.StoreName.setText(currentModel.getStoreName());
        holder.StoreRate.setRating(currentModel.getStoreRate());
        holder.StoreVoters.setText(Integer.toString(currentModel.getStoreVoters()));
        holder.StoreViewers.setVisibility(View.INVISIBLE);
        holder.StoreViewers.setHeight(0);
    }
    if(AdapterMod==2){
        holder.StoreName.setText(currentModel.getStoreName());
        holder.StoreViewers.setText(Integer.toString(currentModel.getStoreViewers()));
        holder.StoreVoters.setVisibility(View.INVISIBLE);
        holder.StoreRate.setVisibility(View.INVISIBLE);
    }
}

@Override
public int getItemCount() {
    return  storeList.size();
}

public class TopRateViewHolder extends RecyclerView.ViewHolder{
    protected ImageView storeImage;
    protected TextView StoreName;
    protected RatingBar StoreRate;
    protected TextView StoreVoters;
    protected TextView StoreViewers;
    protected LinearLayout StoreLayout;

    public TopRateViewHolder(final View itemView) {
        super(itemView);
        storeImage = (ImageView)itemView.findViewById(R.id.toprate_image);
        StoreName = (TextView)itemView.findViewById(R.id.toprate_name);
        StoreRate = (RatingBar)itemView.findViewById(R.id.toprate_rate);
        StoreVoters = (TextView)itemView.findViewById(R.id.toprate_voters);
        StoreViewers = (TextView)itemView.findViewById(R.id.toprate_viewer);
        StoreLayout = (LinearLayout)itemView.findViewById(R.id.toprate_linear);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StoreFragment storeFragment = new StoreFragment();
                Bundle args = new Bundle();
                Store store = storeList.get(getAdapterPosition());
                args.putString("StoreID",store.getStoreID());
                storeFragment.setArguments(args);
                AppCompatActivity activity = (AppCompatActivity)itemView.getContext();
                FragmentManager fragmentManager = activity.getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.frame_fragStore,storeFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
    }
}

然后将recyclerView放在HomeFragment中。 如果您有更好的方法与我联系,我不知道这是如何工作的。

1 个答案:

答案 0 :(得分:1)

您的list_item_store_rate.xml没有frag_store组件。您需要将数据传递回活动,并从活动中进行片段事务。这将解决问题