如何使用RecycleView显示分层数据结构?

时间:2016-08-30 13:04:11

标签: android ios uitableview android-recyclerview

在iOS中,如果您的数据结构具有两级,“部分”,“行”,则UITableView适用于呈现数据。它有两个委托方法:

numberOfSectionsInTableView(tableView: UITableView) -> Int    
tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

第一种方法询问部分数量,第二种方法存在某一部分中有多少行。

这种方法在Android中是否存在?

enter image description here

3 个答案:

答案 0 :(得分:1)

您可以为recycleview创建自己的适配器,处理2种(或更多)类型的列表项,将它们添加到列表中(按照您想要的方式),然后将其传递给适配器。

答案 1 :(得分:1)

RecyclerView.Adapter支持多种开箱即用的视图类型。由您决定要在任何特定位置显示的项目类型。您可以轻松地使用它来创建节,例如,如果您定义两个项类型TYPE_HEADER和TYPE_ITEM:

@Override
public int getItemViewType(int position) {
    if (isHeader(position)) {
        return TYPE_HEADER;
    } else {
        return TYPE_ITEM;
    }
}

然后,您可以使用onCreateViewHolder来扩展正确的布局,并使用onBindViewHolder来使用它。

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_HEADER) {
        View v = ... ; // inflate here
        return new HeaderViewHolder(v);
    } else {
        View v = ... ; // inflate here
        return new ItemViewHolder(v);
    }    
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    if (holder instanceof HeaderViewHolder) {
        // handle header
    } else if (holder instanceof ItemViewHolder {
        // handle item
    }
}

您可以使用您选择的数据结构。我更喜欢使用我自己实现的树状结构,或者只使用普通的ArrayList和instanceof来检测item是标题还是内容。

答案 2 :(得分:0)

You can use the library SectionedRecyclerViewAdapter to group your data into sections.

First create a Section class:

class MySection extends StatelessSection {

    String title;
    List<String> list;

    public MySection(String title, List<String> list) {
        // call constructor with layout resources for this Section header, footer and items 
        super(R.layout.section_header, R.layout.section_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(list.get(position));
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new SimpleHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

        // bind your header view here
        headerHolder.tvItem.setText(title);
    }
}

Then you set up the RecyclerView with your Sections:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data for each year
MySection section1 = new MySection("A", firstDataList);
MySection section2 = new MySection("B", secondDataList);

// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);