搜索了几个小时后,我无法找到解决问题的方法。我有以下课程:
主要活动:
RecyclerView rvIdeas = (RecyclerView) findViewById(R.id.recView1);
// Initialize ideas
DaoSession daosession = ((App) getApplication()).getDaoSession();
IdeaDao ideadao = daosession.getIdeaDao();
ideas = (ArrayList<Idea>) ideadao.loadAll();
// Set layout manager to position the items
rvIdeas.setLayoutManager(new LinearLayoutManager(this));
// Create adapter passing in the list of ideas
IdeaAdapter adapter = new IdeaAdapter(this, ideas);
// Attach the adapter to the recyclerview to populate items
rvIdeas.setAdapter(adapter);
然后是适配器:
public class IdeaAdapter extends
RecyclerView.Adapter<IdeaAdapter.ViewHolder> {
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView ideaname;
public TextView docCount;
public TextView last_mod;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
ideaname = (TextView) itemView.findViewById(R.id.main_idea_name);
docCount = (TextView) itemView.findViewById(R.id.idea_documents);
last_mod = (TextView) itemView.findViewById(R.id.idea_last_edited);
}
}
// Store a member variable for the ideas
private List<Idea> ideas;
// Store the context for easy access
private Context mContext;
// Pass in the idea array into the constructor
public IdeaAdapter(Context context, List<Idea> ideaList) {
ideas = ideaList;
mContext = context;
}
// Easy access to the context object in the recyclerview
private Context getContext() {
return mContext;
}
// Usually involves inflating a layout from XML and returning the holder
@Override
public IdeaAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View ideaView = inflater.inflate(R.layout.item_idea, parent, false);
// Return a new holder instance
return new ViewHolder(ideaView);
}
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(IdeaAdapter.ViewHolder viewHolder, int position) {
// Get the data model based on position
Idea idea = ideas.get(position);
// Set item views based on your views and data model
viewHolder.ideaname.setText(idea.getName());
String docs = R.string.media + idea.getName();
viewHolder.docCount.setText(docs);
viewHolder.last_mod.setText(idea.getLast_modified().toString());
}
// Returns the total count of items in the list
@Override
public int getItemCount() {
return ideas.size();
}
}
fragment_main的.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app1="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context=".view.fragments.MainFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvIdea"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:visibility="visible" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_insert"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="325dp"
android:layout_marginStart="325dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="465dp"
android:adjustViewBounds="false"
android:baselineAlignBottom="false"
android:clickable="true"
android:nestedScrollingEnabled="true"
android:onClick="addFABclick"
app1:pressedTranslationZ="24dp"
app:layout_behavior=".view.animation.ScrollAwareFABBehavior"
app:srcCompat="@drawable/material_add_white" />
</android.support.design.widget.CoordinatorLayout>
最后但并非最不重要的是recyclerview的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/main_idea_name"
android:layout_width="210dp"
android:layout_height="20dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:textAppearance="@android:style/TextAppearance.Medium"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/idea_documents"
android:layout_width="210dp"
android:layout_height="20dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="0dp"
android:textColor="@color/black"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_idea_name" />
<ImageView
android:id="@+id/idea_share"
android:layout_width="28dp"
android:layout_height="31dp"
app:srcCompat="?attr/actionModeShareDrawable"
app:layout_constraintRight_toLeftOf="@+id/idea_go_inside"
android:layout_marginRight="22dp"
android:layout_marginEnd="22dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
android:contentDescription="@string/share"/>
<ImageView
android:id="@+id/idea_go_inside"
android:layout_width="29dp"
android:layout_height="30dp"
app:srcCompat="@drawable/material_arrow_right"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
android:contentDescription="@string/go_next"/>
<ImageView
android:id="@+id/idea_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_menu_delete"
app:layout_constraintRight_toLeftOf="@+id/idea_share"
android:layout_marginRight="14dp"
android:layout_marginEnd="14dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toRightOf="@+id/idea_documents"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
app:layout_constraintHorizontal_bias="1.0"
android:contentDescription="@string/trash"/>
<TextView
android:id="@+id/idea_last_edited"
android:layout_width="210dp"
android:layout_height="20dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="0dp"
android:textColor="@color/colorSecondaryText"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/idea_documents" />
</android.support.constraint.ConstraintLayout>
问题是永远不会调用onBindViewHolder
和onCreateViewHolder
。 getItemCount
返回一些&gt; 0并且没有空指针或嵌套滚动。 recyclerview什么都不显示。我究竟做错了什么?
感谢您的帮助。