setSelection() on ListViews that don't fill the viewport
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?
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.
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>
精彩评论