Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create a listview in your main xml file like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/masterLayout" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;ListView android:id="@+id/list" android:cacheColorHint="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>Then create another xml file called child_layout:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"&gt; &lt;ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Then in your activity class initialize your listview:</p> <pre><code>ListView listView1 = (ListView)findViewById(R.id.list); </code></pre> <p>Create a class that extends baseadapter and modify all the necessary methods the way you need to (create the constructor that takes a list of drawables as an argument and create a global variable that is set to the provided list). Then do the following in your activity class:<br/></p> <pre><code>ArrayList&lt;Drawable&gt; images = new ArrayList&lt;Drawable&gt;(); // add to the list here CustomListAdapter adapter = new CustomListAdapter(images); listView1.setAdapter(adapter); </code></pre> <p>Do this in your getView() function in your customlistadapter class:</p> <pre><code>public View getView(int position, View convertView, ViewGroup parent) { Drawable image = images.get(position); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.child_layout, null); } ImageView imageView = (ImageView)convertView.findViewById(R.id.image); imageView.setBackgroundDrawable(image); return convertView; } </code></pre> <p><em>ListView item click listener:</em></p> <pre><code>listView1.setOnItemClickListener(new ListView.OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; listView, View itemView, int position, long itemId) { String message = "example text: " + position; Toast.makeText(MyActivity.this, message, Toast.LENGTH_SHORT).show(); } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload