使用RecyclerView.Adapter项目上的内容动态加载片段

时间:2019-01-07 10:16:38

标签: android android-fragments android-recyclerview

Android和Java的新功能。 单击RecyclerAdapter项时,尝试通过Activity / TabLayout上的内容动态加载片段。这是我的bitbucket repo。在下面嵌入recyclerview代码。

public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.PlacesViewHolder> {

    private List<Place> mPlaces;

    public PlacesAdapter(List<Place> mPlaces) {
        this.mPlaces = mPlaces;
    }

    @NonNull
    @Override
    public PlacesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.place_list_item, viewGroup, false);
        return new PlacesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PlacesViewHolder viewHolder, int i) {

        Place place = mPlaces.get(i);

        viewHolder.itemHeading.setText(place.getPlaceTitle());
        viewHolder.itemExcerpt.setText(place.getPlaceExcerpt());
        viewHolder.itemRatings.setText(place.getPlaceRatings());
        viewHolder.itemRatingBar.setRating(Float.parseFloat(place.getPlaceRatings()));
        viewHolder.itemImage.setImageResource(place.getPlaceImage());

//        viewHolder.itemCardVIew.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(view.getContext(), "Item Clicked " + getItemCount(), Toast.LENGTH_SHORT).show();
//            }
//        });

    }

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

    public class PlacesViewHolder extends RecyclerView.ViewHolder {

        public TextView  itemHeading;
        public TextView  itemExcerpt;
        public ImageView itemImage;
        public TextView  itemRatings;
        public RatingBar itemRatingBar;
        public CardView  itemCardVIew;

        public PlacesViewHolder(@NonNull View itemView) {
            super(itemView);

            itemHeading = itemView.findViewById(R.id.place_item_heading);
            itemExcerpt = itemView.findViewById(R.id.place_item_excerpt);
            itemRatings = itemView.findViewById(R.id.place_item_rating_text);
            itemRatingBar = itemView.findViewById(R.id.place_item_rating_bar);
            itemImage = itemView.findViewById(R.id.place_item_image);
            itemCardVIew = itemView.findViewById(R.id.place_card_view);

            itemCardVIew.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(view.getContext(), "Item Clicked! " + itemHeading.getText(), Toast.LENGTH_SHORT).show();

                    // load fragment
                    AppCompatActivity activity   = (AppCompatActivity) view.getContext();
                    Fragment          myFragment = new Fragment();
                    activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();
                }
            });

        }
    }
}

1 个答案:

答案 0 :(得分:0)

viewHolder.itemCardVIew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             Toast.makeText(view.getContext(), "Item Clicked " + getItemCount(), Toast.LENGTH_SHORT).show();
             FragmentManager manager = ((Activity) context).getFragmentManager();
             FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
             fragmentTransaction.replace(R.id.fragment_container, new YourFragment());
             fragmentTransaction.addToBackStack(null); //depend's on your usecase
             fragmentTransaction.commit();
        }
    });

这应该是您动态启动片段的代码。只需用它替换注释的代码即可