Pushkar
Pushkar

Reputation: 770

Android RecyclerView Large gaps with missing item

I have recycler view that loads data from a sqlite database. On opening the activity nothing loads in the recycler view but on scrolling half data is visible. On again going to top top item are visible with large gaps below. The gaps appear in place of missing items. See this video.

Device Screenshot Data in Database

As you can see the word tupperware from database is not in the recyclerview. In place of that a big gap is visible.

From The Adapter

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

From The activity

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

The database data is loaded through a asyncLoader

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

        return database.getWordsInSearchHistory();
}

Database Method

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

Upvotes: 1

Views: 248

Answers (1)

Ridcully
Ridcully

Reputation: 23655

The only thing that looks strange to me, is your programmatically changing the layout params of the recylcerview. Can you try to remove that part and see if it changes anything?

Upvotes: 1

Related Questions