user3783005
user3783005

Reputation: 540

listview with sections with highlighted title

I am working on an app where i have to implement in it a listview with different sections, the sections should have a highlighted title. I have no idea how to do it and i am new at android programming and i found nothing on the internet .. can anyone help me please ? I have tried several tutorials but nothing worked for me.

Thanks.

The listview should look like that:

title 1

A

B

C

title 2

A

D

G

Upvotes: 0

Views: 308

Answers (3)

Beena
Beena

Reputation: 2354

1) You can refer Listview with sections

2) You can also use Expandablelistview instead of using list view. You can refer ExpandableListDemo. For your sections You can set different layout file.

For setting your listview always open you can use following code.

myListview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            // Doing nothing
            return true;
        }
    });

Upvotes: 1

Dharmendra Pratap Singh
Dharmendra Pratap Singh

Reputation: 1342

For creating a List-Activtiy or ListView with section wise, you need following thing--

Header XML
Normal ListRow XML
Main Activity with ListView
Adapter class

Create XML name item1.xml inside res/layout folder.

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent">
       <TextView
          android:id="@+id/text"
          android:layout_height="50dp"
          android:gravity="center_vertical"
          android:text="text"
          android:visibility="visible"
          android:layout_width="fill_parent"
          android:textColor="#FF000000"
          android:background="#FFFFFFFF" />
   </LinearLayout>

Create XML name item2.xml inside res/layout folder

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content"
   android:layout_width="fill_parent">
         <TextView
         android:id="@+id/textSeparator"
         android:layout_height="wrap_content"
         android:gravity="center"
         android:text="text"
         android:visibility="visible"
         android:layout_width="fill_parent"
         android:textColor="#FFFFFFFF"
         android:background="#000" />
   </LinearLayout>

ListAdapter Class

//Adapter Class private class MyCustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

    private ArrayList<String> mData = new ArrayList<String>();
    private LayoutInflater mInflater;

    private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

    public MyCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSeparatorItem(final String item) {
        mData.add(item);
        // save separator position
        mSeparatorsSet.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    public int getCount() {
        return mData.size();
    }

    public String getItem(int position) {
        return mData.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int type = getItemViewType(position);
        System.out.println("getView " + position + " " + convertView + " type = " + type);
        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.item1, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.item2, null);
                    holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.textView.setText(mData.get(position));
        return convertView;
    }

}

public static class ViewHolder {
    public TextView textView;
}

MultipleItemsList where you can set the adapter

public class MultipleItemsList extends ListActivity{

private MyCustomAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAdapter = new MyCustomAdapter();
    for (int i = 1; i < 50; i++) {
        mAdapter.addItem("Sameer Blog " + i);
        if (i % 4 == 0) {
            mAdapter.addSeparatorItem("Ahmad " + i);
        }
    }
    setListAdapter(mAdapter);
}
}

This would look like this

enter image description here

For more info http://androidtrainningcenter.blogspot.in/2012/03/android-listview-with-section-header.html

Upvotes: 0

happy
happy

Reputation: 25

You can refer the below link which was explained clearly with an example

http://javatechig.com/android/listview-with-section-header-in-android

Upvotes: 0

Related Questions