在recyclerView(Java)中的相应部分填充数据

时间:2016-03-10 07:15:19

标签: java android android-recyclerview

我有一个recyclerView,其中我使用此library

添加了一些部分

我的RecyclerView现在看起来像image of my RecyclerView

你可以看到我的数据尚未组织,现在我的数据就在这个表格中:

Option Explicit

Sub LoopOverListOfSheets2()
Dim shtsColl As Collection
Dim sht As Worksheet

Set shtsColl = GetSheets(ThisWorkbook.Worksheets("SheetWithNames"))

For Each sht In shtsColl
'    sht.Calculate
    '...
Next sht

End Sub


Function GetSheets(namesSht As Worksheet) As Collection
Dim myColl As New Collection
Dim shtNamesRng As Range, cell As Range
Dim sht As Worksheet

With namesSht
    Set shtNamesRng = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants, xlTextValues)

    For Each cell In shtNamesRng
        Set sht = SetSheet(namesSht.Parent, cell.Value)
        If Not sht Is Nothing Then myColl.Add sht
    Next cell
End With

Set GetSheets = myColl

End Function


Function SetSheet(wb As Workbook, shtName As String) As Worksheet

On Error Resume Next
Set SetSheet = wb.Worksheets(shtName)
On Error GoTo 0

End Function

我希望它像:

  Section 2015
2016-05-03
2016-04-03
2015-12-03
  Section 2016
2016-05-03
2016-04-03
2015-12-03

你可以看到我想根据它们的值和部分的值来管理数据

这是我的MainAdapter类:

  Section 2015
2015-12-03
  Section 2016
2016-05-03
2016-04-03

这是我的MainActivity:

public class MainAdapter extends SectionedRecyclerViewAdapter<MainAdapter.MainVH> {

Context context;
LayoutInflater inflater;

List<Data> dataArray = Collections.emptyList();
ArrayList<String> data;
ArrayList<String > sectionData;

public MainAdapter(Context context ,ArrayList<String> data  , ArrayList<String> sectionData   ){

    inflater =  LayoutInflater.from(context);


    this.context = context;
    this.data = data;
    this.sectionData = sectionData;

}

@Override
public int getSectionCount() {

    return sectionData.size(); // number of sections.
}

@Override
public int getItemCount(int section) {



    return data.size(); // odd get 8
//        return 8; // number of items in section (section index is parameter).
}

@Override
public void onBindHeaderViewHolder(MainVH holder, int section) {
    // Setup header view.

    String current = sectionData.get(section);


    holder.title.setText(current);

}

@Override
public void onBindViewHolder(MainVH holder, int section, int relativePosition, int absolutePosition) {
    // Setup non-header view.
    // 'section' is section index.
    // 'relativePosition' is index in this section.
    // 'absolutePosition' is index out of all non-header items.
    // See sample project for a visual of how these indices work.

    if (data.get(relativePosition).contains("2015")){


        Toast.makeText(context , "it contains 2015",Toast.LENGTH_SHORT).show();

    }

    String currentRow = data.get(relativePosition);
    holder.title.setText(currentRow);



  //        holder.title.setText(String.format("S:%d, P:%d, A:%d", section, relativePosition, absolutePosition));


}

@Override
public int getItemViewType(int section, int relativePosition, int absolutePosition) {
    if (section == 1)
        return 0; // VIEW_TYPE_HEADER is -2, VIEW_TYPE_ITEM is -1. You can return 0 or greater.
    return super.getItemViewType(section, relativePosition, absolutePosition);
}


@Override
public MainVH onCreateViewHolder(ViewGroup parent, int viewType) {
    // Change inflated layout based on 'header'.

    int layout;
    switch (viewType) {
        case VIEW_TYPE_HEADER:
            layout = R.layout.section;
            break;
        case VIEW_TYPE_ITEM:
            layout = R.layout.row;
            break;
        default:
            Toast.makeText(context,"Default",Toast.LENGTH_SHORT).show();
            break;
    }
    View v = LayoutInflater.from(parent.getContext())
            .inflate(viewType == VIEW_TYPE_HEADER ? R.layout.section : R.layout.row, parent, false);
    return new MainVH(v);
}

public static class MainVH extends RecyclerView.ViewHolder {

    final TextView title;
    public MainVH(View itemView) {
        super(itemView);
        // Setup view holder.
        // You'd want some views to be optional, e.g. for header vs. normal.

        title = (TextView) itemView.findViewById(R.id.TV);
    }
}
}

我甚至不知道从哪里开始如此,如果任何身体可以给我任何暗示或指导,而不是它将如此有用并且非常感谢我,谢谢

如果我的问题不可理解,那么请让我知道我会解决它

1 个答案:

答案 0 :(得分:0)

您可以使用库SectionedRecyclerViewAdapter以更简单的方式实现这一目标。

首先创建一个Section类来对项目进行分组:

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

然后使用您的章节设置RecyclerView:

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

// Create your sections with the list of data for each year
MySection section2015 = new MySection("2015", dataList2015);
MySection section2016 = new MySection("2016", dataList2016);

// Add your Sections to the adapter
sectionAdapter.addSection(section2015);
sectionAdapter.addSection(section2016);

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