我有一个片段,我想要包含recyclerview。但遗憾的是它没有显示任何内容,似乎查看器方法没有开始。 (对不起,我的英语不好)。 这是片段的代码:
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>
我做了一些调试,似乎适配器中的视图符号没有被调用..如果你可以帮助我会很棒...非常感谢
答案 0 :(得分:9)
您的适配器需要提供此方法:
@Override
public int getItemCount() {
return itemsData.length;
}