我的问题是,RecyclerView列表会出现,但会从列表中间的某个位置开始,一次只显示一行,而不是多行。我可以向上滚动到列表中的第一项,但我无法向下滚动。关于在Android 4.3中使用RecyclerView的stackoverflow,我发现了一些“问题”,但似乎没有人对我有类似的问题。使用早期版本的RecyclerView,它运行良好。使用三星Galaxy SIII。
我在我的应用中使用了recyclerview,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:name="na.com.company.myapp.List"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
tools:context="na.com.company.myapp.List"
tools:listitem="@layout/a_row"/>
列表中的每一行都由以下内容组成:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/common_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingTop="10dp"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView android:id="@+id/latin_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/common_name"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingBottom="10dp"
android:textStyle="italic"/>
<CheckBox android:id="@+id/select_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"/>
</RelativeLayout>
AppCompatActivity派生类中的代码如下(在onCreate函数中):
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.list);
LinearLayoutManager linear_layout = new LinearLayoutManager(this);
linear_layout.setOrientation(LinearLayoutManager.VERTICAL);
linear_layout.setAutoMeasureEnabled(true);
recyclerView.setLayoutManager(linear_layout);
Drawable dividerDrawable = ContextCompat.getDrawable(this, R.drawable.list_divider);
RecyclerView.ItemDecoration divider_decoration = new ListDivider(dividerDrawable);
recyclerView.addItemDecoration(divider_decoration);
app build.gradle文件包含:
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
ViewHolder代码:
public class TheViewHolder extends RecyclerView.ViewHolder implements OnClickListener
{
private View the_view = null;
private CheckBox selected_item = null;
public TheViewHolder(final View individual_view)
{
super(individual_view);
the_view = individual_view;
the_view.setOnClickListener(this);
selected_item = (CheckBox)the_view.findViewById(R.id.select_box);
}
public void AttachCursor(final Cursor data_cursor, String[] columns, int[] views, boolean is_selected)
{
TextView a_view = (TextView) the_view.findViewById(views[0]);
a_view.setText(data_cursor.getString(data_cursor.getColumnIndex(columns[0])));
a_view = (TextView) the_view.findViewById(views[1]);
a_view.setText(data_cursor.getString(data_cursor.getColumnIndex(columns[1])) + " " + data_cursor.getString(data_cursor.getColumnIndex(columns[2])));
selected_item.setChecked(is_selected);
}
@Override
public void onClick(View clicked_view)
{
selected_item.setChecked(true);
}
}
并且 getItemCount 函数中的适配器( query_cursor.getCount()当前返回值691):
public class SimpleCursorRecyclerViewAdapter extends RecyclerView.Adapter<TheViewHolder>
{
private Cursor query_cursor = null;
private int layout_resource = 0;
private String[] table_columns;
private int[] layout_views;
private View layout_view = null;
private boolean checkbox_list[] = null;
public SimpleCursorRecyclerViewAdapter(Context context, int layout_id, Cursor data_cursor, String[] columns, int[] views)
{
layout_resource = layout_id;
query_cursor = data_cursor;
table_columns = columns;
layout_views = views;
}
public Cursor get_item(final int position)
{
if (query_cursor != null && !query_cursor.isClosed())
{
query_cursor.moveToPosition(position);
}
return query_cursor;
}
@Override
public int getItemCount()
{
int size = 0;
if (query_cursor != null)
{
size = query_cursor.getCount();
checkbox_list = new boolean[size];
}
return size;
}
@Override
public void onBindViewHolder(TheViewHolder view_holder, int position)
{
Cursor current_cursor = get_item(position);
view_holder.AttachCursor(current_cursor, table_columns, layout_views, checkbox_list[position]);
}
@Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
if (layout_view == null)
{
layout_view = LayoutInflater.from(parent.getContext()).inflate(layout_resource, parent, false);
}
return new TheViewHolder(layout_view);
}
}
答案 0 :(得分:0)
问题在于我的代码,特别是这种方法:
@Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
if (layout_view == null)
{
layout_view = LayoutInflater.from(parent.getContext()).inflate(layout_resource, parent, false);
}
return new TheViewHolder(layout_view);
}
现在出现的列表应该是当我将onCreateViewHolder重写为:
@Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
layout_view = LayoutInflater.from(parent.getContext()).inflate(layout_resource, parent, false);
return new TheViewHolder(layout_view);
}