Click here to Skip to main content
14,022,154 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


26 bookmarked
Posted 21 May 2012
Licenced CPOL

Create a Dynamic Shelfview in Android?

, 16 Sep 2012
Rate this:
Please Sign up or sign in to vote.
In this article, you can see a program that shows a shelf view. This shelf view has two features. First create a ListView that includes another list view. Second, Inner ListView where you can move items as horizontal.


As you know, Android doesn’t support ListView with horizontal view feature. There is a way to make it as a gallery or TableLayout (you can see my tip in here). Of course, I'll describe them.


Notice, in this application I use two classes:

  • HorizontalListView class that extends the AdapterView. It has been downloaded from GitHub.
  • Quaere library used almost the same as Linq2Object in .NET. You can download it here.

Other Approaches

In here, I want to describe other ways for showing a shelf view (I mean a listview that can show multi items in every row and every row can scroll horizontal).

First Way - Gallery

In "main.xml" file, we code only LinearLayout with an ID. Then, in Java class, try to create a multi gallery and insert in LinearLayout every gallery that plays the role of row:

public class MainActivity extends Activity {	
	//---the images to display---
    Integer[] imageIDs = {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        for(int i = 0; i < 5 ; i++){
		Gallery gallery = (Gallery) findViewById(;
		gallery.setAdapter(new ImageAdapter(this));
    public class ImageAdapter extends BaseAdapter 
        private Context context;
        private int itemBackground;
        public ImageAdapter(Context c) 
            context = c;
            //---setting the style---
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            itemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);
        //---returns the number of images---
        public int getCount() {
            return imageIDs.length;
        //---returns the ID of an item--- 
        public Object getItem(int position) {
            return position;
        //---returns the ID of an item---         
        public long getItemId(int position) {
            return position;
        //---returns an ImageView view---
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
            return imageView;

There is some problem in this way, one problem related to view position about items is that it appears in the center of display and I can't resolve it.

Second Way - TableLayout

It has already been fully described in this tip. Of course, it has some problems too such as it is unsupported for virtualization as AdapterView.

The Beginning

Now, we want to introduce a new way that consists of the following advantages vs. the above ways:

  • Support virtualization
  • Use Array Adapter instead of loop statements for creating a list of items
  • Every row can make separate other rows

Before doing anything else, please add Quaere library.

In our example, you will define your layout for the items, rows and a layout for base scheme and use it in your adapter or another place. We want to plan two list views - the first list view (we call it child ListView) acts horizontal and the second list view (we call it parent ListView) includes multi child ListView and scrolling as vertical.

Create the "item.xml" layout file in the "res/layout" folder of the project.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""



    android:orientation="vertical" >





        android:src="@drawable/book" />







        android:textSize="15px" />







        android:textSize="15px" />

"item.xml" layout file is used in child ListView. Now create the "row.xml" layout file in the "res/layout" folder of project. "row.xml" defines a new listview from HorizontalListView class instead of a common ListView.

 <LinearLayout xmlns:android=""



    android:orientation="vertical" >




        android:background="@drawable/shelf1" />

Notice, I've written "com.onazifi.shelf. ", it is related to my project folders and if you use it, change its address.

Ok, create "main.xml" file:

<LinearLayout xmlns:android=""







        android:layout_height="fill_parent" />

Create BookItem class for map components of item and Library class for making list of BookItem. In this class, there is a "groupbyArrayBookItem" method for grouping a list of items. This method uses the quaere library for grouping. This library acts as LINQ in .NET.

 public class Library {
	private ArrayList<BookItem> arrayBookItem;
	public static final int AUTHOR = 1;
	public static final int TITLE = 2;
	public static final int RATE = 3;
	public static final int DOWNLOAD_DATE = 4;
	public Library() {
		arrayBookItem = new ArrayList<BookItem>();
	public void setColectionBookItem(ArrayList<BookItem> _array) {
		this.arrayBookItem = _array;
	public void addBookItem(BookItem _bi) {
	public ArrayList<ArrayList<BookItem>> groupbyArrayBookItem(int type) {
		BookItem[] books = BookItem.ALL_BOOKS;
		ArrayList<ArrayList<BookItem>> groupList = 
				new ArrayList<ArrayList<BookItem>>();
		String getType = "";
		switch (type) {
		case AUTHOR:
			getType = "bookitem.getAuthor()";
		case TITLE:
			getType = "bookitem.getTitle()";
			getType = "bookitem.getDownloadDate()";
		case RATE:
			getType = "bookitem.getRate()";
			return groupList;
		 * books is a object of BookItem
		 * bookitem is item for point to list
		 * getType is a string value for set type of grouping
		 * groupbyArrayBookItem return back array of array of items
		Iterable<Group> groups = 
		for (Group group : groups) {
			ArrayList<BookItem> obj = new ArrayList<BookItem>();
			for (Object Item : group.getGroup()) {
				obj.add((BookItem) Item);
		return groupList;

And finally, survey ShelfViewActivity class. In this class, we call library class to get an array of array of items. Then set adapter parent array list to parent ListView and in adapter class, call another adapter class for creating a list of items in child ListView.

 public class ShelfViewActivity extends ListActivity {
	/** Called when the activity is first created. */
	private VerticalAdapter verListAdapter;
	public void onCreate(Bundle savedInstanceState) {
		 * Calling Library & BookItem classes for create list of groups
		 *  groupbyArrayBookItem return back array of array of items
		Library lb = new Library();
		for (BookItem item : BookItem.ALL_BOOKS) {
		ArrayList<ArrayList<BookItem>> groupList = 
				new ArrayList<ArrayList<BookItem>>();
		groupList = lb.groupbyArrayBookItem(Library.AUTHOR);
		verListAdapter = new VerticalAdapter(this, R.layout.row, groupList);
	 * This class add a list of ArrayList to ListView that it include multi
	 * items as bookItem.
	private class VerticalAdapter extends ArrayAdapter<ArrayList<BookItem>> {
		private int resource;
		public VerticalAdapter(Context _context, int _ResourceId,
				ArrayList<ArrayList<BookItem>> _items) {
			super(_context, _ResourceId, _items);
			this.resource = _ResourceId;
		public View getView(int position, View convertView, ViewGroup parent) {
			View rowView;
			if (convertView == null) {
				rowView = LayoutInflater.from(getContext()).inflate(resource,
			} else {
				rowView = convertView;
			HorizontalListView hListView = (HorizontalListView) rowView
			HorizontalAdapter horListAdapter = new HorizontalAdapter(
					getContext(), R.layout.item, getItem(position));
			return rowView;
	 * This class add some items to Horizontal ListView this ListView include
	 * several bookItem.
	private class HorizontalAdapter extends ArrayAdapter<BookItem> {
		private int resource;
		public HorizontalAdapter(Context _context, int _textViewResourceId,
				ArrayList<BookItem> _items) {
			super(_context, _textViewResourceId, _items);
			this.resource = _textViewResourceId;
		public View getView(int position, View convertView, ViewGroup parent) {
			View retval = LayoutInflater.from(getContext()).inflate(
					this.resource, null);
			TextView topText = (TextView) retval.findViewById(;
			TextView bottomText = (TextView) retval
			return retval;


If you pay attention to the source code, you'll learn how to create new classes that extend from Android base class.

Notice that there are images for showing in "res/drawable". Also, this project has been coded in Android 4.0 with android:minSdkVersion="14".

I have tried to describe my project completely and I hope the project will be useful for you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...


Comments and Discussions

Questionscroll Pin
Member 106302742-Jul-14 0:19
memberMember 106302742-Jul-14 0:19 
AnswerRe: scroll Pin
Member 1093399012-Jul-14 21:35
memberMember 1093399012-Jul-14 21:35 
Questionhiii Pin
Member 1066290425-Apr-14 7:58
memberMember 1066290425-Apr-14 7:58 
AnswerRe: hiii Pin
ajela_dam9-May-14 23:48
memberajela_dam9-May-14 23:48 
Bugi also got a error in this app Pin
Member 1066290425-Apr-14 7:37
memberMember 1066290425-Apr-14 7:37 
QuestionGetting wrong data when I am clicking on any item of the list. And onClickListener is not working on particular data Pin
Md Faiz Anwar8-Apr-14 20:08
memberMd Faiz Anwar8-Apr-14 20:08 
Questionjava.lang.noclassdeffounderror: org.quaere.dsl Pin
amarnath baitha12-Feb-14 7:44
memberamarnath baitha12-Feb-14 7:44 
AnswerRe: java.lang.noclassdeffounderror: org.quaere.dsl Pin
Member 1031619912-Apr-14 9:40
memberMember 1031619912-Apr-14 9:40 
QuestionProblem noclassdef Pin
Tealcoune28-Dec-13 11:35
memberTealcoune28-Dec-13 11:35 
AnswerRe: Problem noclassdef Pin
omid.nazifi12-Jan-14 18:47
memberomid.nazifi12-Jan-14 18:47 
QuestionMyLibrary has stopped error Pin
droidcoder19-Dec-13 1:05
memberdroidcoder19-Dec-13 1:05 
QuestionsetOnItemClickListener() not be called Pin
Member 21272220-Oct-13 16:31
memberMember 21272220-Oct-13 16:31 
AnswerRe: setOnItemClickListener() not be called Pin
omid.nazifi22-Oct-13 19:04
memberomid.nazifi22-Oct-13 19:04 
GeneralRe: setOnItemClickListener() not be called Pin
Member 21272223-Oct-13 21:05
memberMember 21272223-Oct-13 21:05 
GeneralRe: setOnItemClickListener() not be called Pin
omid.nazifi26-Oct-13 2:56
memberomid.nazifi26-Oct-13 2:56 
GeneralRe: setOnItemClickListener() not be called Pin
Member 1075317615-Apr-16 0:49
memberMember 1075317615-Apr-16 0:49 
QuestionHandling clicks? Pin
Member 1017992530-Jul-13 7:41
memberMember 1017992530-Jul-13 7:41 
AnswerRe: Handling clicks? Pin
omid.nazifi30-Jul-13 23:33
memberomid.nazifi30-Jul-13 23:33 
QuestionAddilng image icons Pin
Zubair KK28-Feb-13 18:55
memberZubair KK28-Feb-13 18:55 
AnswerRe: Addilng image icons Pin
omid.nazifi16-Mar-13 19:27
memberomid.nazifi16-Mar-13 19:27 
AnswerRe: Addilng image icons Pin
Member 123715706-Mar-16 0:33
memberMember 123715706-Mar-16 0:33 
QuestionIncreasing Space in TableRow Pin
Sultan Ahmed Sagor9-Feb-13 12:17
memberSultan Ahmed Sagor9-Feb-13 12:17 
AnswerRe: Increasing Space in TableRow Pin
omid.nazifi10-Feb-13 22:43
memberomid.nazifi10-Feb-13 22:43 
QuestionThanks..... Pin
sowcom26-Dec-12 15:53
membersowcom26-Dec-12 15:53 
QuestionProblem fix for “NoClassDefFoundError” with ADT 17 Pin
Anilkutsa26-Nov-12 23:15
memberAnilkutsa26-Nov-12 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190417.4 | Last Updated 17 Sep 2012
Article Copyright 2012 by omid.nazifi
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid