gpt4 book ai didi

android - 寻呼图书馆和房间 : Content not loading when Recyclerview scroll

转载 作者:行者123 更新时间:2023-11-30 05:09:19 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中使用分页库通过 recyclerview 和 Room 实现无限滚动 根据这篇文章:https://developer.android.com/topic/libraries/architecture/paging/#database-examples

部分数据在初始化时加载,但当用户滚动时,没有任何反应,其余数据未获取

我的道

@Dao
public interface MessageDao {
.
.
.
@Query("SELECT * FROM messages ORDER BY created DESC")
DataSource.Factory<Integer, Message> getPagedMessage();
}

View 模型

public class SmsViewModel extends AndroidViewModel {

private LiveData<PagedList<Message>> liveData;

public SmsViewModel(@NonNull Application application) {
super(application);

DataSource.Factory<Integer, Message> factory = AppDatabase.getInstance(getApplication()).messageDao().getPagedMessage();

PagedList.Config config = (new PagedList.Config.Builder())
.setPageSize(10)
.setInitialLoadSizeHint(20)
.setPrefetchDistance(5)
.setEnablePlaceholders(false)
.build();
liveData = new LivePagedListBuilder<>(factory, config).build();
}

public LiveData<PagedList<Message>> getLiveData() {
return liveData;
}
}

适配器类

public abstract class BasePagedAdapter<T> extends PagedListAdapter<T, BasePagedAdapter.MyViewHolder> {

private OnClickListener onClickListener;
private OnLongClickListener onLongClickListener;

public BasePagedAdapter(DiffUtil.ItemCallback<T> callback) {
super(callback);
}

public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}

public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
this.onLongClickListener = onLongClickListener;
}

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

LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, getLayoutIdForType(viewType), parent, false);
return new MyViewHolder(binding);
}

@Override
public void onBindViewHolder(@NonNull BasePagedAdapter.MyViewHolder holder, int position) {
T t = getDataAtPosition(position);
holder.bind(t);
}

public abstract T getDataAtPosition(int position);

abstract @LayoutRes
public int getLayoutIdForType(int viewType);

class MyViewHolder extends RecyclerView.ViewHolder {
private final ViewDataBinding binding;

MyViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;

itemView.setOnClickListener(v -> {
if (onClickListener != null) {
onClickListener.onClick(getAdapterPosition());
}
});

itemView.setOnLongClickListener(view -> {
if(onLongClickListener != null) {
onLongClickListener.onLongClick(getAdapterPosition());
return true;
}
return false;
});
}

void bind(T obj) {
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
}
}

public interface OnClickListener {
void onClick(int position);
}

public interface OnLongClickListener {
void onLongClick(int position);
}
}

Activity

public class SmsActivity extends AppCompatActivity {

@BindView(R.id.lyt_empty)
View lytEmpty;

@BindView(R.id.progressBar)
ProgressBar progressBar;

@BindView(R.id.recycler_view)
RecyclerView recyclerView;

PagedList<Message> messages;

BasePagedAdapter<Message> adapter = new BasePagedAdapter<Message>(DIFF_CALLBACK) {
@Override
public Message getDataAtPosition(int position) {
if (messages != null) {
return messages.get(position);
}
return null;
}

@Override
public int getLayoutIdForType(int viewType) {
return R.layout.item_sms_list;
}
};


SmsViewModel viewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

ButterKnife.bind(this);

viewModel = ViewModelProviders.of(this).get(SmsViewModel.class);

adapter.setOnClickListener(position -> {
Intent intent = new Intent(this, SmsDetailActivity.class);
intent.putExtra(SmsDetailActivity.EXTRA_SMS, messages.get(position));
startActivity(intent);
});
recyclerView.setAdapter(adapter);
recyclerView.setNestedScrollingEnabled(false);
loadData();
}

private void loadData() {
viewModel.getLiveData().observe(this, messages -> {
progressBar.setVisibility(View.GONE);
this.messages = messages;
if (messages != null && messages.size() > 0) {
adapter.submitList(messages);
recyclerView.setVisibility(View.VISIBLE);
lytEmpty.setVisibility(View.GONE);
} else {
recyclerView.setVisibility(View.GONE);
lytEmpty.setVisibility(View.VISIBLE);
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_sms_list, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return true;
}


private static DiffUtil.ItemCallback<Message> DIFF_CALLBACK =
new DiffUtil.ItemCallback<Message>() {
@Override
public boolean areItemsTheSame(Message oldItem, Message newItem) {
return oldItem.uid == newItem.uid;
}

@Override
public boolean areContentsTheSame(Message oldItem, @NonNull Message newItem) {
return oldItem.equals(newItem);
}
};
}

消息表中目前有25个元素,但初始化时只显示20个。当我滚动 Recyclerview 时没有任何反应,其他 5 个元素没有显示

最佳答案

我认为你需要这样做。

recyclerView.setNestedScrollingEnabled(true);

信用

看这里:

https://github.com/googlesamples/android-architecture-components/issues/215#issuecomment-394849133

关于android - 寻呼图书馆和房间 : Content not loading when Recyclerview scroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53987502/

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