如何在RecyclerView中为特定项目添加项目分隔符

时间:2018-10-12 13:33:05

标签: java android android-recyclerview android-adapter

是否可以在RecyclerView中仅为特定项目添加项目分隔符?当前,在每个项目之间应用项目分隔符。我想在以下位置添加一个分隔线:

  • 项目B C节
  • 之间
  • 项目C3 D部分
  • 之间
  • 项目D2 E节
  • 之间
  • 项目E3 项目F1
  • 之间
  • 项目F1 项目G1
  • 之间

当前结果

Current result

片段

public class FragmentRV extends android.support.v4.app.Fragment {
    RecyclerView mRecyclerView;

    public FragmentRV() {}

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_rv, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        mRecyclerView = v.findViewById(R.id.my_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getContext()), LinearLayout.VERTICAL));

        super.onActivityCreated(savedInstanceState);

        initRVAdapter();
    }

    private void initRVAdapter(){
        List<Object> itemsList = new ArrayList<>();

        RVItemsAapter itemsListAdapter = new RVItemsAapter(getContext());
        mRecyclerView.setAdapter(itemsListAdapter);

        itemsList.add(new SMSmessage("Item A","Item A description"));
        itemsList.add(new Phonecall("Item B","Item B description"));
        itemsList.add(new SectionHeader("Section C"));
        itemsList.add(new SMSmessage("Item C1","Item C1 description"));
        itemsList.add(new SMSmessage("Item C2","Item C2 description"));
        itemsList.add(new SMSmessage("Item C3","Item C3 description"));
        itemsList.add(new SectionHeader("Section D"));
        itemsList.add(new SMSmessage("Item D1","Item D1 description"));
        itemsList.add(new SMSmessage("Item D2","Item D2 description"));
        itemsList.add(new SectionHeader("Section E"));
        itemsList.add(new SMSmessage("Item E1","Item E1 description"));
        itemsList.add(new SMSmessage("Item E2","Item E2 description"));
        itemsList.add(new SMSmessage("Item E3","Item E3 description")); 
        itemsList.add(new SMSmessage("Item F1","Item F1 description"));             
        itemsList.add(new Phonecall("Item G1","Item G1 description"));

        itemsListAdapter.setCallSMSFeed(itemsList);
        itemsListAdapter.notifyDataSetChanged();
    }
}

4 个答案:

答案 0 :(得分:1)

如果我需要在部分之间使用分隔符,则尝试在全局的recyclerview适配器类中为部分标题定义一种类型:

ProxyPass        /socket.io http://localhost:6680/socket.io
ProxyPassReverse /socket.io http://localhost:6680/socket.io

然后,我将返回一个布局,其中包含TextView及其下方的分隔线。

private static final int TYPE_SMS_MESSAGE = 0;
private static final int TYPE_PHONE_CALL = 1;
private static final int TYPE_SECTION_HEADER = 2;

要能够检测并返回此布局,您需要检测到它是SectionHeader的一个实例。

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case TYPE_SMS_MESSAGE:
            // return a view as you did before
        case TYPE_PHONE_CALL:
            // return a view as you did before
        case TYPE_SECTION_HEADER:
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.section_header_layout, null);
            return new YourRecyclerViewHolder(v);
        default:
            return null;
    }
}

希望这将帮助您有一个想法并在此方面取得进展! 快乐编码:)

巴基

答案 1 :(得分:0)

是的,您可以在row.xml布局中创建自己的分隔线,并根据onBindViewholder()方法中的条件显示/隐藏分隔线。

答案 2 :(得分:0)

一种解决方案是像这样删除所有分隔线:

首先定义您的RecyclerView:

RecyclerView recycle = (RecyclerView) findViewById(R.id.recyclerView);

在您的活动中使用此方法:

recycle.addItemDecoration(new DividerItemDecoration(context, 0));

然后在您的自定义适配器中,您可以在所需的位置添加分隔线:

itemsList.add(new SMSmessage("Item A","Item A description"));
itemsList.add(new Phonecall("Item B","Item B description"));
itemsList.add(new YourCustomDivider());
itemsList.add(new SectionHeader("Section C"));

答案 3 :(得分:0)

一种解决方案是编写自定义RecyclerView.ItemDecoration并仅在需要的地方绘制分隔线。您应该重写发生魔术的onDraw()方法。 您可以使用以下代码在RecyclerView中设置自定义ItemDecoration:

recyclerView.addItemDecoration(new CustomItemDecoration());