2

So i have a fragment, which have a viewpager with tablayout, which consist of two tabs-two fragments.

The thing is, recyclerview shows empty, and i have no idea why.,

Tab Fragment LAyout:

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

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

Tag Fragment:

List<OrderListItem> orderList = new ArrayList<>();
        orderList.add(new OrderListItem(333, "ABCDE", new Date(), new Date(), false, true));

        adapter = new OrderListAdapter(orderList, this.getActivity());
        layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
        myRecyclerView.setLayoutManager(layoutManager);
        myRecyclerView.setAdapter(adapter);

Adapter:

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

    private List<OrderListItem> orderList;
    @LayoutRes
    private final int layoutRes;

    private Context context;
    private final LayoutInflater inflater;

    public OrderListAdapter(List<OrderListItem> orderList, Context context){
        this.orderList = orderList;
        this.layoutRes = R.layout.order_list_item_layout;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(layoutRes, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final OrderListItem item = orderList.get(position);    
    }

    public void setItems(List<OrderListItem> orderList){
        this.orderList.clear();
        this.orderList.addAll(orderList);
        notifyDataSetChanged();
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

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

        }
    }
}

RecycleView Item some colorful layouts insider, so i know if the child layout is there or not, which it isnt. any idea why recyclerview is empty?

edit1: i know the recyclerview is there, because its in a lolipop phone, and if i make a movement at the recycler place it shows me the ripple top and bottom scroll border. but the child layouts are empty and blank, and should be colorful as i specificed in the child layout.

eit2. just used a listview with a simpleAdapter and it is showing. there must be something buggy with the rv

edit3: row layout (i should clearly see an empty textview with a color, besides not any value setted.)

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/black"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/material_deep_teal_200"
        android:text="New Text"
        android:id="@+id/textView" />
</LinearLayout>
johnny_crq
  • 4,291
  • 5
  • 39
  • 64

2 Answers2

4

Change:

android:layout_width="match_parent"
android:layout_height="match_parent"

to

android:layout_width="50dp"  // set yourself
android:layout_height="50dp" // set yourself
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
2

Where is the logic in which you set OrderListItem properties to ui? You have first to set ViewHolder components:

public static class ViewHolder extends RecyclerView.ViewHolder {
    TextView mTitleTv;
    public ViewHolder(View itemView) {
        super(itemView);
        mTitle = itemView.findViewById(R.id.textview_id_in_your_xml_file);
    }
}

And then set them in onBindViewHolder:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final OrderListItem item = orderList.get(position);   
    holder.mTitleTv.setText(item.getReplaceThisWithAStringProperty);
}

EDIT

If you have to use default LinearLayoutManager properties use this constructor:

layoutManager = new LinearLayoutManager(getActivity());

Instead of this:

layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);

Add also fixed size property for RecyclerView:

recyclerView.setHasFixedSize = true;
Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
  • i didn't yet, because i couldn't set the child items to show. My child item layout has some colors so i know if its displayed. At the moment is not setting anything, but that is not the point, if if it was, it wud still not show – johnny_crq Sep 03 '15 at 15:09
  • @user3806331 post your xml file for cell – Giorgio Antonioli Sep 03 '15 at 15:28
  • done. I know i didn't set any value, but the textview color is there so i can check if its showing or not – johnny_crq Sep 03 '15 at 15:31
  • the fragment containing the recyclerview is a viewpager fragment of some tab, which is hosted inside a fragment. i think the problem has something to do with that. iv used recyclerview multipletime and didn't have any problem. or im just missing something stupied. @Fondesa – johnny_crq Sep 03 '15 at 15:32
  • layoutmanager and fixedsize corrected but still showing blank. Also, after creating the adapter, initiating the list and setting it to the rv, iv printed adapter.getItemCount() and the items are there. just dont appear @Fondesa – johnny_crq Sep 03 '15 at 15:40
  • 2
    @user3806331 Did you got any solution for this issue ? Please comment here to let me know – Haresh Chaudhary Apr 06 '16 at 16:01
  • This worked for me. http://stackoverflow.com/questions/34579614/how-to-implement-recyclerview-in-a-fragment-with-tablayout – J.Krishna Dec 09 '16 at 17:34