How to add a recycler view to fragment
03:53 27 Jan 2017

I have tried many ways to add recyclerview inside a fragment. I'm new for android. My android have 5 fragments one of these fragment I need to add a recyclerview. here is my code

notification_item.xml




    

   


notification_Fragment.xml







NotificationItem.java

public class NotificationItem {
    private String title;
    private int imageResId;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getImageResId() {
        return imageResId;
    }

    public void setImageResId(int imageResId) {
        this.imageResId = imageResId;
    }
}

NotificationData.java

public class NotificationData {
    private static final String[] textItem = {"pathum", "sai", "charu"};
    private static final int[] imgItem = {android.R.drawable.ic_popup_reminder, android.R.drawable.ic_menu_add, android.R.drawable.ic_menu_delete};

    public static List getListData() {
        List data = new ArrayList<>();

        for (int x = 0; x < 4; x++) {
            for (int i = 0; i < textItem.length && i < imgItem.length; i++) {
                NotificationItem item = new NotificationItem();
                item.setImageResId(imgItem[i]);
                item.setTitle(textItem[i]);
                data.add(item);
            }
        }

        return data;
    }

}

NotificationAdapter.java

public class NotificationAdapter extends RecyclerView.Adapter {

    private List listData;
    private LayoutInflater inflater;

    public NotificationAdapter(List listData, Context c) {

        this.inflater = LayoutInflater.from(c);
        this.listData = listData;
    }

    @Override
    public NotificationHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.notification_item,parent,false);
        return new NotificationHolder(view);
    }

    @Override
    public void onBindViewHolder(NotificationHolder holder, int position) {
        NotificationItem item = listData.get(position);
        holder.title.setText(item.getTitle());
        holder.icon.setImageResource(item.getImageResId());
    }

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

    class NotificationHolder extends RecyclerView.ViewHolder {

        private TextView title;
        private CircleImageView icon;
        private View container;

        public NotificationHolder(View itemView) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.notification_item_text);
            icon = (CircleImageView) itemView.findViewById(R.id.notification_item_img);
            container = itemView.findViewById(R.id.notification_item_root);
        }
    }
}

NotificationFragment.java

public class NotificationFragment extends Fragment {

    RecyclerView recyclerView;
    NotificationAdapter notificationAdapter;

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

        View rootView = inflater.inflate(R.layout.notification_fragment, container, false);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.notification_list);

    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    notificationAdapter = new NotificationAdapter(NotificationData.getListData(),this);
    recyclerView.setAdapter(notificationAdapter);


    return rootView;
}

}

I was unable to make it right in NotificationFragment.java and NotificationAdapter.java please help me guys.

java android android-fragments