So we have a listview and want to make it so that some of the items are "above" what's visible (i.e. scroll upwards to see them.) setSelection() works but only when the ListView has enough items to scroll off the screen already.. anyone have any idea how to implement this when the list only has a few items in it?
Asked
Active
Viewed 919 times
0
-
To clarify, must the items above the selection be off screen? If so, then a solution may be to just fill the space below with enough unselectable empty items. – Programmer Bruce May 15 '11 at 20:02
-
how would you go about this? each item has buttons, etc. associated with it and if we just add empty items it'll look pretty messy/unprofessional. it's a dynamic list. – Chris Bolton May 15 '11 at 20:12
2 Answers
0
setSelection() will work, but if the item is already on screen you should not see any difference. Remember that in touch mode, the list items are not displayed as selected.

Romain Guy
- 97,993
- 18
- 219
- 200
-
yeah.. that was kind of my problem. We're trying to anchor a specific list item to the top of the screen regardless of how few items are on the screen. – Chris Bolton May 15 '11 at 20:08
-
so no way to do this? :/. perhaps a blank spacer like the commenter above suggested? – Chris Bolton May 15 '11 at 20:30
0
This combines one of Thane's ListView examples, and information from Android ListView child View setEnabled() and setClickable() do nothing
package com.stackoverflow.q6010765;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<Datum> data = new ArrayList<Datum>();
data.add(new Datum("FIRST", "One line of text."));
data.add(new Datum("SECOND", "Two lines of text. Two lines of text. Two lines of text."));
data.add(new Datum("THIRD", "Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text. Three or more lines of text."));
data.add(new Datum("FOURTH", "One line of text, again."));
ListView myList = (ListView) findViewById(R.id.my_list);
myList.setAdapter(new MyAdapter(this, R.layout.row, data));
myList.setSelection(2);
}
}
class Datum
{
String left, right;
Datum(String left, String right)
{
this.left = left;
this.right = right;
}
}
class MyAdapter extends ArrayAdapter<Datum>
{
private final List<Datum> data;
private final int originalDataLength;
private int textViewResourceId;
MyAdapter(Context context, int textViewResourceId, List<Datum> data)
{
super(context, textViewResourceId, data);
this.data = data;
this.originalDataLength = data.size();
for (int i = 0; i < 20; i++)
{
data.add(new Datum("", ""));
}
this.textViewResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(textViewResourceId, null);
}
Datum datum = data.get(position);
if (datum != null)
{
TextView leftView = (TextView) convertView.findViewById(R.id.left_text);
TextView rightView = (TextView) convertView.findViewById(R.id.right_text);
if (leftView != null)
{
leftView.setText(datum.left);
}
if (rightView != null)
{
rightView.setText(datum.right);
}
}
if (datum.left.length() == 0)
{
convertView.setFocusable(false);
convertView.setClickable(false);
}
return convertView;
}
@Override
public boolean areAllItemsEnabled()
{
return false;
}
@Override
public boolean isEnabled(int position)
{
return position < originalDataLength;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/left_text"
android:layout_width="75dip"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/right_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="2"
android:ellipsize="end" />
</LinearLayout>

Community
- 1
- 1

Programmer Bruce
- 64,977
- 7
- 99
- 97
-
Replicating what I posted is easy. Just copy and paste. I'd take a look at what you've got if you were to describe the problem you're having and post the complete minimal code necessary to replicate the problem. – Programmer Bruce May 16 '11 at 02:59
-
well on first glance the for loop doesn't seem to be adding any additional items onto the list. – Chris Bolton May 16 '11 at 03:21
-
If you're referring to the code I posted, then you are wrong. If you want to see the items more clearly, then remove the areAllItemsEnabled and isEnabled overrides (or just change them to always return true). If you are referring to a for loop in your code, then again, I'd be glad to take a look, if you were to post the complete minimal code necessary to replicate the problem. I don't want to guess what the rest of the code looks like. – Programmer Bruce May 16 '11 at 03:38