【问题标题】:The ListView does not scroll. How do I fix it?ListView 不滚动。我如何解决它?
【发布时间】:2012-11-26 12:45:46
【问题描述】:

我在 Linearlayout 中有一个 ListView。 列表视图有一个自定义光标适配器。 一切正常,除了 ListView 不滚动。

欢迎提出任何建议!!谢谢。毛里齐奥

这是 MainActivity 的 XML

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

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Aggiungi" />

    <ListView
        android:id="@+id/list"  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

这是相关的java代码。

public class MainActivity extends Activity  {
    private DatabaseHelper db=null;
    private Cursor tabellaCursor=null;
    private ListAdapter adapter; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int[] to = new int[] {R.id.name_entry};
        db = new DatabaseHelper(this);
        tabellaCursor=db.getWritableDatabase().rawQuery("SELECT _id, colonna1, colo      nna2, colonna3 FROM tabella ORDER BY _id", null);
        ListAdapter adapter=new MyAdapter(this, R.layout.list_example_entry, tabe     llaCursor, new String[]{"colonna1"},to);
        ListView lt = (ListView)findViewById(R.id.list); 
        lt.setAdapter(adapter); 
        Button addbtn=(Button)findViewById(R.id.buttonAdd);
        addbtn.setOnClickListener(new OnClickListener()
        {public void onClick(View v){add(); }
        });
}

【问题讨论】:

    标签: android listview scroll


    【解决方案1】:

    您的 ListViews 布局参数是“wrap_content”。当您向 litview 添加新项目时,它会扩展。因此它不会滚动。如果您将其设置为“match_parent”,它肯定会开始滚动。不要忘记视图只有在其内容时才会滚动(子视图) 尺寸比它大。

    【讨论】:

    • 不相关,但让我走上了正轨,我以为我有很多东西。在列表中,但只有 2 个,是的,它没有滚动……在添加了 10 个以上的项目后。我可以看到它正确滚动。
    【解决方案2】:

    添加更多超出高度的列表项。然后你就可以滚动了。

    【讨论】:

      【解决方案3】:

      请使用以下代码,它将解决您的问题。

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >
      
          <Button
              android:id="@+id/buttonAdd"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal"
              android:text="Aggiungi" />
      
          <ListView
              android:id="@+id/list"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" 
              android:layout_below="@+id/buttonAdd"/>
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-09
        • 2020-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多