Android RecyclerView缺少项的较大差距

时间:2018-07-20 13:09:18

标签: android android-recyclerview

我有回收站视图,该视图从sqlite数据库加载数据。 在打开活动时,没有任何内容加载到“回收者”视图中,但滚动时可见一半数据。再次转到顶部,可见顶部下方有大间隙的项目。差距出现在缺少项目的位置。参见此video

Device Screenshot Data in Database

如您所见,recyclerview中没有来自数据库的tupperware单词。代替的是,可以看到很大的差距。

来自适配器

@NonNull
@Override
public HistoryRecycleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.history_recycler_item, parent, false);

    return new HistoryRecycleHolder(view);
}

@Override
public void onBindViewHolder(@NonNull HistoryRecycleHolder holder, int position) {

    final String txt = wordList.get(position);

    holder.historyWord.setText(txt);

    holder.wordConstrain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, WordActivity.class);
            intent.putExtra(IntentKeys.HISTORY_KEY, txt);
            context.startActivity(intent);
        }
    });

}

RecycleView.ViewHolder

public class HistoryRecycleHolder extends RecyclerView.ViewHolder {

    public TextView historyWord;
    public ConstraintLayout wordConstrain;

    public HistoryRecycleHolder(View itemView) {
        super(itemView);
        historyWord = itemView.findViewById(R.id.history_word);
        wordConstrain = itemView.findViewById(R.id.history_recycler_constrain);
    }
}

history_recycler_item.xml

<?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:id="@+id/history_recycler_constrain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/history_word"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/cardview_light_background"
        android:paddingBottom="5dp"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        android:paddingTop="5dp"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="@string/word" />
</android.support.constraint.ConstraintLayout>

来自活动

        historyList = new ArrayList<>();
        historyAdapter = new HistoryAdapter(historyList, this);

        RecyclerView.LayoutManager layoutManager =
                new LinearLayoutManager(getApplicationContext());

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(this,
                LinearLayoutManager.VERTICAL));
        recyclerView.setAdapter(historyAdapter);
        recyclerView.setHasFixedSize(true);

        ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
        params.height = (MaxHeight / 2);
        recyclerView.setLayoutParams(params);

通过asyncLoader加载数据库数据

@Nullable
@Override
public ArrayList<String> loadInBackground() {

        return database.getWordsInSearchHistory();
}

数据库方法

public ArrayList<String> getWordsInSearchHistory() {
        ArrayList<String> list = new ArrayList<>();

        SQLiteDatabase database = this.getReadableDatabase();

        String query =
                "SELECT * FROM " + SEARCH_HISTORY_TABLE
                        + " ORDER BY " + SEARCH_HISTORY_TABLE_COLUMN_ID + " DESC";

        Cursor cursor = database.rawQuery(query, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            list.add(cursor.getString(cursor.getColumnIndex(SEARCH_HISTORY_TABLE_COLUMN_WORD)));
            cursor.moveToNext();
        }
        cursor.close();
        return list;
    }

1 个答案:

答案 0 :(得分:1)

对我来说唯一奇怪的是,您以编程方式更改了recylcerview的布局参数。您可以尝试删除该部分,看看它是否有任何改变?