Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recyclerview in a fragment doesnt show anything

i have a fragment, where i want to include a recyclerview. But unfortunately it doesnt show anything, it seems that the viewholder methods doesnt get started. (sorry for my bad english). Here is my code for the fragment:

public class Quran_Fragment extends Fragment {

    public static final String ARG_SCROLL_Y = "ARG_SCROLL_Y";

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


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

      // Ab hier neu: Basem @ BMW

      RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);

      // Die Klasse ItemData_quran kann ja gleich bleiben für alle Fragments !! nur itemsData heisst dann anders....
      // Die Adapter, welche die verbindung zwischen Daten und Darstellung sind, werden fuer alle Fragments neu erzeugt: Quran_Fragment ->QuranAdapter, Translation_Fragment ->TranslationAdapter etc.

    // hier vielleicht die Methode holen, die den Inhalt aus der xml liest, und dann der Item_Data_quran übergibt...z.B. new ItemData_quran(inhalt)... string inhalt...wird aus der Methode gefüllt
     ItemData_quran itemsData[] = { new ItemData_quran("DAILY HADITH")};

      recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 3. create an adapter
      QuranAdapter qAdapter = new QuranAdapter(itemsData);
        // 4. set adapter
      recyclerView.setAdapter(qAdapter);
        // 5. set item animator to DefaultAnimator
      recyclerView.setItemAnimator(new DefaultItemAnimator());

     //   TextView quran = (TextView) view.findViewById(R.id.content_quran);
     //   quran.setText("Quran");

        final ObservableScrollView scrollView = (ObservableScrollView) view.findViewById(R.id.scroll);
        Activity parentActivity = getActivity();
        if (parentActivity instanceof ObservableScrollViewCallbacks) {
            // Scroll to the specified offset after layout
            Bundle args = getArguments();
            if (args != null && args.containsKey(ARG_SCROLL_Y)) {
                final int scrollY = args.getInt(ARG_SCROLL_Y, 0);

                ScrollUtils.addOnGlobalLayoutListener(scrollView, new Runnable() {
                    @Override
                    public void run() {
                        scrollView.scrollTo(0, scrollY);

                    }
                });
            }
            scrollView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);
        }

        return view;
    }
}

and here is the adapter:

public class QuranAdapter extends RecyclerView.Adapter<QuranAdapter.ViewHolder> {
    private ItemData_quran[] itemsData;

    public QuranAdapter(ItemData_quran[] itemsData) {
        this.itemsData = itemsData;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public QuranAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.quran_item_layout, parent,false);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

@Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        // - get data from your itemsData at this position
        // - replace the contents of the view with that itemsData

        viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
        viewHolder.getPosition();



    }


 public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtViewTitle;
        public ImageView imgViewIcon;
        public TextView txtViewDescription;
        //public ClipData.Item currentItem;

        public ViewHolder(final View itemLayoutView) {
            super(itemLayoutView);
            txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
     }
    }


    // Return the size of your itemsData (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return itemsData.length;
    }
}

and the xml for fragment_scrollview_quran is this:

<?xml version="1.0" encoding="utf-8"?>

<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:overScrollMode="never">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/padding" />

    <com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView>

</LinearLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>

I have made some debugging, and it seems, that viewholder in the adapter doesnt get invoked.. Would be great if you could help me...Thanks a lot

like image 240
Basem Abdelfattah Avatar asked Jan 22 '15 15:01

Basem Abdelfattah


1 Answers

Your adapter needs to provide this method:

@Override
public int getItemCount() {
    return itemsData.length;
}
like image 134
Larry Schiefer Avatar answered Oct 25 '22 20:10

Larry Schiefer