sticky or pinned sectioned list view in android with dynamic data

341 Views Asked by At

I want to achieve something like this. I am getting "survey titles"(type: string) and "different number of questions"(type:string) under every survey titles. I want to use survey titles as headers and questions of that survey should be display under that survey title header.

I have already tried https://github.com/emilsjolander/StickyListHeaders; and few other libraries. It is using first char of list data as header. Which in my case not possible.

This will be the look one header and its questions. When there are multiple survey with their questions, Sticky or pinned Header behaviour I want to achieve in my android app.

1

There are 1 best solutions below

3
Mr00Anderson On

You can use the API for StickyListHeaders how you want and not use a char. I took this sample and changed a few items StickyListHeaders#getting-started indicated by my comments "//*** NOTE:" Just have to learn that you can change implementations however you want in Java if the API is provided. Just takes experimenting. Nothing in this API forces you to use their 'char' method or method at all. This is just the build in API I assume.

 @Override 
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;
        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = inflater.inflate(R.layout.header, parent, false);
            holder.text = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }
        //*** NOTE: You can use the name here
        //set header text as first char in name
        String headerText = "" + countries[position].subSequence(0, 1).charAt(0);
        holder.text.setText(headerText);
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        //*** NOTE: You could use the hashcode of the word here or your own implementation
        //return the first character of the country as ID because this is what headers are based upon
        return countries[position].subSequence(0, 1).charAt(0);
    }