ListView上的setSelection()没有填充视口

时间:2011-05-15 19:44:40

标签: android

所以我们有一个列表视图,并且希望这样做,以便某些项目在“可见”之上“(即向上滚动以查看它们。)setSelection()有效,但只有当ListView有足够的项目滚动时屏幕已经..当列表中只有几个项目时,任何人都知道如何实现这个?

2 个答案:

答案 0 :(得分:0)

setSelection()将起作用,但如果该项目已经在屏幕上,则不应该看到任何差异。请记住,在触摸模式下,列表项不会显示为已选中。

答案 1 :(得分:0)

这结合了Thane的ListView示例和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>