android - fragment 中的 Recyclerview 不显示任何内容

标签 android android-fragments android-scrollview android-recyclerview android-viewholder

我有一个 fragment ,我想在其中包含一个回收 View 。但不幸的是它没有显示任何东西,似乎 viewholder 方法没有启动。 (对不起,我的英语不好)。 这是我的 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;
    }
}

这是适配器:

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;
    }
}

fragment_scrollview_quran 的 xml 是这样的:

<?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>

我已经进行了一些调试,看起来适配器中的 viewholder 没有被调用。如果你能帮助我就太好了......非常感谢

最佳答案

您的适配器需要提供此方法:

@Override
public int getItemCount() {
    return itemsData.length;
}

关于android - fragment 中的 Recyclerview 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092157/

相关文章:

android - 如何在一个查询室中的多对多关系中获取连接表中的额外列android

java - 在使用 Kotlin 的 Android Studio 3.6.1 中,System.out.println(CustomArrayList) 不会在调试时打印值

Android关于下载数据和 fragment 列表

android - 当我设置 minDate 和 maxDate 时 DatePickerDialog 出错

java - fragment 和文本转语音以及幻灯片菜单

android - 当键盘打开时,包裹在 ScrollView 中的 ConstraintLayout 仍然会调整大小

android - 修复 Mediacontroller 在 Scrollview 中的位置

c# - 如何在 android xamarin 中动态地将按钮添加到 View ?

android - 如何替换 fragment 存储第一个历史记录并删除其他事务?

android - 了解如何使用 OverScroller 类