0

Hello everyone ım trying to create an RecyclerView which have two views. First is the item and the second is the header ı made the recyclerView and the headers in same page but ı couldn't categorized the items. Maybe ım making it totally wrong ı just want to seperate my objects which is collected in "lstPoster" (Poster is the parent class the all posters are the child of Poster class, each child will be categorized below the headers) Those are my codes

public class NewRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    Context mContext;
    Header header;
    List<Poster> listItems;

    public NewRecyclerViewAdapter(Header header, List<Poster> listItems, Context context) {
        mContext = context;
        this.header = header;
        this.listItems = listItems;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == TYPE_HEADER) {

            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false);
            return  new VHHeader(v);

        }else if(viewType == TYPE_ITEM) {

            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_item, parent, false);
            return new VHItem(v);

        }
        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    private Poster getItem(int position)
    {
        return listItems.get(position != 0 ? position-1 : position);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if(holder instanceof VHHeader)
        {
            VHHeader VHheader = (VHHeader)holder;
            VHheader.headerText.setText(header.getHeaderText());

        }
        else if(holder instanceof VHItem)
        {
            Poster currentItem = getItem(position);
            VHItem VHitem = (VHItem)holder;
            VHitem.textPosterTitle.setText(currentItem.getPosterTitle());
            VHitem.posterImageView.setBackgroundResource(currentItem.getPosterImage());
            VHitem.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Poster data = getItem(position);
                    if(data instanceof PosterProduct) {
                        Intent intent = new Intent(mContext, PosterActivity.class);
                        //passing data to the poster activity
                        intent.putExtra("Title",data.getPosterTitle());
                        intent.putExtra("Description", ((PosterProduct) data).getDescription());
                        intent.putExtra("Image",data.getPosterImage());
                        mContext.startActivity(intent);
                    }
                }
            });
        }
    }

    //    need to override this method
    @Override
    public int getItemViewType(int position) {
        if(isPositionHeader(position))
            return TYPE_HEADER;
        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position)
    {
        return position == 0;
    }

    //increasing getItemCount to 1. This will be the row of header.
    @Override
    public int getItemCount() {
        return listItems.size()+1;
    }


    class VHItem extends RecyclerView.ViewHolder{

        private TextView textPosterTitle;
        private ImageView posterImageView;
        private CardView cardView;


        public VHItem(@NonNull View itemView) {
            super(itemView);
            textPosterTitle = (TextView) itemView.findViewById(R.id.poster_title_id);
            posterImageView = (ImageView) itemView.findViewById(R.id.poster_image_id);
            cardView = (CardView) itemView.findViewById(R.id.cardview_id);
        }
    }

    class VHHeader extends RecyclerView.ViewHolder{

        private TextView headerText;
        private ImageView headerImage;

        public VHHeader(@NonNull View itemView) {
            super(itemView);
            headerText = (TextView) itemView.findViewById(R.id.headerText);
            headerImage = (ImageView) itemView.findViewById(R.id.headerImage);
        }
    }

}
public class ProductsFragment extends Fragment {


    private RecyclerView mRecyclerView;
    LinearLayoutManager mLinearLayoutManager;
    static public List<Poster> lstPoster;
    NewRecyclerViewAdapter adapter;

    public ProductsFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_products, container, false);

        mRecyclerView = view.findViewById(R.id.recyclerView);
        mLinearLayoutManager = new LinearLayoutManager(getContext());
        adapter = new NewRecyclerViewAdapter(getHeader(), getListItem(), getContext());
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(adapter);


        return view;
    }

    public Header getHeader() {
        Header header = new Header();
        header.setHeaderText("I'm header");
        return header;
    }



    public static List<Poster> getListItem() {
        lstPoster = new ArrayList<Poster>();

        lstPoster.add(new PosterVrMovies("Aerobatics", "link", R.drawable.aerobotics));
        lstPoster.add(new PosterFlatMovies("Astrocop", "link", R.drawable.astrocop));
        lstPoster.add(new PosterDomeMovies("Mission Mars", "link", R.drawable.missionmars));
        lstPoster.add(new PosterProduct("Capadochia", "link4", R.drawable.capadochia, "Description 1"));
        lstPoster.add(new PosterEnterteinment("Dino", "link5", R.drawable.dinoisland, "Description 2"));

        return lstPoster;
    }
}
4

0 回答 0