gpt4 book ai didi

Android RecyclerView 缺少项目的大差距

转载 作者:行者123 更新时间:2023-11-29 23:45:48 25 4
gpt4 key购买 nike

我有从 sqlite 数据库加载数据的回收器 View 。在打开 Activity 时,回收站 View 中没有加载任何内容,但在滚动时可以看到一半数据。再次转到顶部可以看到顶部项目,下面有很大的差距。缺口出现在缺失项目的位置。看这个video .

Device Screenshot Data in Database

如您所见,数据库中的单词 tupperware 不在 recyclerview 中。取而代之的是一个很大的差距。

来自适配器

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

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

数据库数据通过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;
}

最佳答案

唯一让我觉得奇怪的是,您以编程方式更改了回收 View 的布局参数。您能否尝试删除该部分并查看它是否有任何改变?

关于Android RecyclerView 缺少项目的大差距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51443280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com