gpt4 book ai didi

android - RecyclerView 的垂直列表中的项目分隔符未显示

转载 作者:行者123 更新时间:2023-11-30 00:04:26 25 4
gpt4 key购买 nike

我正在为我的应用程序创建 RecyclerList,并且需要在列表项之间设置最简单的分隔符。为此,我决定使用 android.support.v7.widget 包中的 DividerItemDecoration 类。下面是代码 fragment :

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);

我的问题是应用屏幕上没有出现分隔线(我正在使用 API 18 的手机上进行测试)。所以,我需要帮助来确定哪里出了问题以及如何解决我的问题。下面我发布了带有 RecyclerView 描述的完整 Activity 代码:

公共(public)类 GroupsActivity 扩展 AppCompatActivity {

private GroupsVcAdapter adapter;
private GroupsViewModel mGroupsViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_groups);
//Creating of toolbar with title Add Group
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_groups);
setSupportActionBar(myToolbar);
//Enable Up Button
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
//RecyclerView containing the list of groups with sound icons
adapter = new GroupsVcAdapter(this);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
//Using ViewModel to observe GroupVc data
mGroupsViewModel = ViewModelProviders.of(this).get(GroupsViewModel.class);
mGroupsViewModel.getAllGroups().observe(this, new Observer<List<GroupVc>>() {

@Override
public void onChanged(@Nullable List<GroupVc> groupVcs) {
adapter.setGroupsVc(groupVcs);
}
});
}

最佳答案

我找到了决定。此代码以我喜欢的方式工作:

adapter = new GroupsVcAdapter(this);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
recyclerView.setAdapter(adapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setItemAnimator(new DefaultItemAnimator());

关于android - RecyclerView 的垂直列表中的项目分隔符未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49263761/

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