0

I'm learning native android and have trouble trying to show my recyclerview in a fragment that is in a viewpager of a tablayout. I have gone through multiple tutorials and discussions trying to figure it out.

This is my code for my fragment:

public class AllEventsFragment extends Fragment {
    private ArrayList<Event> listEvents = new ArrayList<>();
    private AdapterEvents mAdapter;
    private RecyclerView mRecyclerEvents;

    public AllEventsFragment() {}

public static AllEventsFragment newInstance() {
    AllEventsFragment fragment = new AllEventsFragment();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

    View view = inflater.inflate(R.layout.fragment_allevents, container, false);
    FragmentActivity fragmentActivity = getActivity();view.findViewById(R.id.swipeEventHits);
    mRecyclerEvents = (RecyclerView) view.findViewById(R.id.listAllEvents);
    LinearLayoutManager layoutManager = new LinearLayoutManager(fragmentActivity);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    Event e1 = new Event("fee1","id1","Desc1","loc1","pic1","date1");
    Event e2 = new Event("fee2","id2","Desc2","loc2","pic2","date2");
    listEvents.add(e1);
    listEvents.add(e2);

    mAdapter = new AdapterEvents(fragmentActivity, listEvents);
    if (listEvents.size() > 0 || listEvents != null) {
        mRecyclerEvents.setAdapter(mAdapter);
    }

    mRecyclerEvents.setLayoutManager(layoutManager);

    return view;
}

As for my adapter,

public class AdapterEvents extends RecyclerView.Adapter<AdapterEvents.ViewHolderEvents> {
private ArrayList<Event> listEvents;
private LayoutInflater layoutInflater;

public  AdapterEvents(Context context) {
    layoutInflater = LayoutInflater.from(context);

}

public AdapterEvents(Context context, ArrayList<Event> list) {
    listEvents = list;
    layoutInflater = LayoutInflater.from(context);
}

public void setEventList(ArrayList<Event> listEvents) {
    this.listEvents = listEvents;
    notifyItemRangeChanged(0,listEvents.size());
}

public ArrayList<Event> getListEvents() {
    return listEvents;
}

@Override
public ViewHolderEvents onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.custom_event_list, parent, false);
    ViewHolderEvents vh = new ViewHolderEvents(view);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolderEvents holder, int position) {
    Event currentEvent = listEvents.get(position);
    holder.eventFee.setText(currentEvent.getFee());
    holder.eventID.setText(currentEvent.getId());
    holder.eventDesc.setText(currentEvent.getDesc());
    holder.eventLoc.setText(currentEvent.getLocation());
    holder.eventPic.setText(currentEvent.getEventPic());
    holder.eventDate.setText(currentEvent.getDate());
}



@Override
public int getItemCount() {
    return 0;
}


public static class ViewHolderEvents extends RecyclerView.ViewHolder {

    CardView cv;
    TextView eventFee;
    TextView eventID;
    TextView eventDesc;
    TextView eventLoc;
    TextView eventPic;
    TextView eventDate;

    public ViewHolderEvents(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cardView);
        eventFee = (TextView) itemView.findViewById(R.id.feeTV);
        eventID = (TextView) itemView.findViewById(R.id.idTV);
        eventDesc = (TextView) itemView.findViewById(R.id.descTV);
        eventLoc = (TextView) itemView.findViewById(R.id.locationTV);
        eventPic = (TextView) itemView.findViewById(R.id.picTV);
        eventDate = (TextView) itemView.findViewById(R.id.dateTV);
    }
}

}

And my fragment layout for AllEvents:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/listAllEvents"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />

</FrameLayout>

and custom_view_layout that my RecyclerView.adapter will be using:

<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/feeTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="18dp"
        android:text="hi"
        android:textColor="@color/colorTextPrimary"/>

    <TextView
        android:id="@+id/idTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="18dp"
        android:textColor="@color/colorTextPrimary"
        android:text="test"/>

    <TextView
        android:id="@+id/descTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18dp"
        android:textColor="@color/colorTextPrimary"/>
    <TextView
        android:id="@+id/locationTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18dp"
        android:textColor="@color/colorTextPrimary"/>
    <TextView
        android:id="@+id/picTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18dp"
        android:textColor="@color/colorTextPrimary"/>
    <TextView
        android:id="@+id/dateTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18dp"
        android:textColor="@color/colorTextPrimary"/>

</LinearLayout>

klutznic
  • 5
  • 3
  • `getItemCount` has to return the size of the dataset. – Blackbelt Feb 16 '16 at 10:58
  • 1
    @Blackbelt OMG! you are a life saver! I didn't realise such a small mistake cost me this much trouble. I kept looking for solutions into why my view isn't initalizing. really big thank you! and sorry for the duplicate question :\ – klutznic Feb 16 '16 at 11:05

0 Answers0