动态添加标题到recylcerview

时间:2016-12-21 08:58:13

标签: android android-recyclerview

我处理和设置以字符串形式发送的内容,其中包含 http链接http://.。)和(可包括)普通文字(info , 镜子, ...)。 我想在我的recylcerview中将普通文本设置为标题

这一切都需要发生动态,因为如果String包含普通文本,并且包括普通文本,那么预先知道,有多少普通文本项。< / p>

所以recylcerview (可能)应该是这样的:

http://www.link1.com
MIRROR
https://www.link2.com
https://www.link3.com
INFO
http://www.link4.com
http://www.link5.com

现在看起来像这样:

MIRROR
http://www.link1.com
http://www.link2.com
http://www.link3.com

根据日志看起来没问题:

/testpackage.com E/ITEM: https://link1.com
/testpackage.com E/ITEM: MIRROR
/testpackage.com E/ITEM: https://link2.com
/testpackage.com E/ITEM: https://link3.com

我无法在recyclelerview中将标题放在正确的位置。 我已经查找了一些解决方案,我确实读过了几个可能的部分,比如SectionedRecyclerViewAdapter,但我不知道如何根据我的需要实现它。 我想问题的棘手部分是它需要动态设置。

我的代码到现在为止:

SectionViewHolder类:

public class SectionViewHolder extends RecyclerView.ViewHolder {

    public TextView name;
    public SectionViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.sectionHeader);
    }
}

我设置的活动:

@EActivity(R.layout.downloads_activity)
public class DownloadsActivity extends BaseActivity {

    String outPut;
    RecyclerView recyclerView;
    AdapterSectionRecycler adapterRecycler;
    List<SectionHeader> sectionHeaders;
...
@AfterViews
    public void init() {
        int i = 0;
        List<Child> childList = new ArrayList<>();
        sectionHeaders = new ArrayList<>();

        //initialize RecyclerView
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        //setLayout Manager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
     ...
        // GET RID OF BAD HTML and SPLIT AS OF <br/>
        String html = outPut.replaceAll("<br/></a>", "</a>");
        String[] lines = html.split("<br/>");
     ...
        // loop and process URL links and normal text
        for (String str : Arrays.asList(lines)) {
            Jsoup.parse(str).text();
            str.split(",");
            // function to parse the URL links and normal text from String
            String item = unescapeJavaString(String.valueOf(Html.fromHtml(str)));

            if (!str.contains("Use following links")) {

                    // Add URL links to list
                    if (item.startsWith("http")) {
                        childList.add(new Child(item));
                    }

                // Add section headers
                if (!item.startsWith("http")) {
                    sectionHeaders.add(new SectionHeader(childList, item, i));
                }
            }
            Log.i("ITEM", item);
        }
        adapterRecycler = new AdapterSectionRecycler(this, sectionHeaders);
        recyclerView.setAdapter(adapterRecycler);
    }

SectionHeader类:

public class SectionHeader implements Section<Child>, Comparable<SectionHeader> {

    List<Child> childList;
    public String sectionText;
    int index;

    public SectionHeader(List<Child> childList, String sectionText, int index) {
        this.childList = childList;
        this.sectionText = sectionText;
        this.index = index;
    }

    @Override
    public List<Child> getChildItems() {
        return childList;
    }

    public String getSectionText() {
        return sectionText;
    }

    @Override
    public int compareTo(SectionHeader another) {
        if (this.index > another.index) {
            return -1;
        } else {
            return 1;
        }
    }
}

ChildViewHolder:

public class ChildViewHolder extends RecyclerView.ViewHolder {

    public TextView name;

    public ChildViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.child);
    }
}

儿童班:

public class Child {

    String name;

    public Child(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

我的AdapterSectionRecycler:

public class AdapterSectionRecycler extends SectionRecyclerViewAdapter<SectionHeader, Child, SectionViewHolder, ChildViewHolder> {

    Context context;

    public AdapterSectionRecycler(Context context, List<SectionHeader> sectionHeaderItemList) {
        super(context, sectionHeaderItemList);
        this.context = context;
    }

    @Override
    public SectionViewHolder onCreateSectionViewHolder(ViewGroup sectionViewGroup, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.section_item, sectionViewGroup, false);
        return new SectionViewHolder(view);
    }

    @Override
    public ChildViewHolder onCreateChildViewHolder(ViewGroup childViewGroup, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.child, childViewGroup, false);
        return new ChildViewHolder(view);
    }

    @Override
    public void onBindSectionViewHolder(SectionViewHolder sectionViewHolder, int sectionPosition, SectionHeader sectionHeader) {
        sectionViewHolder.name.setText(sectionHeader.sectionText);
    }

    @Override
    public void onBindChildViewHolder(ChildViewHolder childViewHolder, int sectionPosition, int childPosition, Child child) {
        childViewHolder.name.setText(child.getName());
    }
}

0 个答案:

没有答案