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

时间:2018-03-13 18:50:01

标签: android android-recyclerview material

我正在为我的应用程序创建RecyclerList,需要在list-items之间设置最简单的分隔符。为此,我决定使用android.support.v7.widget包中的DividerItemDecoration类。 这里是代码片段:

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

我的问题是app屏幕上没有显示分隔符(我正在使用API​​ 18在手机上测试它)。所以,我需要帮助来定义我出错的地方以及如何解决我的问题。下面我发布了描述RecyclerView的完整活动代码:

公共类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);
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

我已经找到了决定。这段代码按我喜欢的方式工作:

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