为什么我的listview不会滚动?

时间:2013-06-26 21:01:23

标签: java android listview scroll

好的,所以我看过其他有相同问题的帖子,但仍然无法弄清楚我做错了什么。这是我的代码:

ListView list = (ListView) getView().findViewById(R.id.listView);
    String[] stringArray = new String[] { "Bright Mode", "Normal Mode","Bright Mode",
            "hey Mode","Bright Mode", "Normal Mode","Bright Mode", "Normal Mode","Bright Mode", "Normal Mode","Bright Mode",
            "hey Mode","Bright Mode", "Normal Mode","Bright Mode", "Normal Mode","Bright Mode", "Normal Mode","Bright Mode",
            "hey Mode","Bright Mode", "Normal Mode","Bright Mode", "Normal Mode","Bright Mode", "Normal Mode","Bright Mode",
            "hey Mode","Bright Mode", "Normal Mode","Bright Mode", "Normal Mode","end", "Bright Mode", "Normal Mode","Bright Mode",
            "hey Mode","Bright Mode", "Normal Mode","Bright Mode", "Normal Mode", "end2" };
    ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(getView().getContext(), R.layout.document_list_view, stringArray);
    list.setAdapter(modeAdapter);
    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {

            Toast.makeText(getView().getContext(), "Clicked", Toast.LENGTH_LONG).show();

        }
    }); 

点击工作正常,但内容离开屏幕,它不会让我滚动。这是我的XML。它不在scrollview或任何内容中。

document_list_view:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
    android:id="@+id/listText"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_height="wrap_content"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:textColor="@color/black">


</TextView>

页面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="1000dp"
    android:layout_height="600dp">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>



</LinearLayout>

3 个答案:

答案 0 :(得分:2)

发现这个问题,是因为我正在扩展一个将所有内容放在scrollview中的类。不要将listviews放在scrollviews

答案 1 :(得分:0)

将您的页面布局更改为与其父级大小相匹配的列表视图,在这种情况下是窗口装饰。

<ListView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

答案 2 :(得分:0)

如果您为每个列表项使用自定义视图,则需要使用自定义适配器作为列表视图。你需要这样的东西

public class CustomAdapter extends ArrayAdapter<String>{
List<String> items;
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.document_list_view, null);
    }

    String item = (String) items.get(position);

    if (item != null) {
        TextView text= (TextView) v.findViewById(R.id.listText);
        if (text!= null) {
            text.setText(item);
        }
    }
    return v;
}

并将此对象设置为列表的适配器。