【问题标题】:ListView without Scrollbar to use in a ScrollView没有滚动条的 ListView 在 ScrollView 中使用
【发布时间】:2013-12-05 15:39:11
【问题描述】:

我想在ScrollView 中使用一种ListView。该列表具有可变长度,始终可以添加/删除项目。有没有一种简单的方法可以修改默认的AndroidListView,或者我是否必须创建一个自定义的ListView?

【问题讨论】:

  • 在滚动视图中添加列表视图是不好的做法。你想实现什么?
  • 使用 forloop 在滚动视图之间创建视图
  • ListView 可以添加/删除项目,并且可以自行滚动。那么为什么你想要一个做同样事情的新类呢?
  • 就像其他人指出的那样,这是非常糟糕的做法。查看 listview 小部件的创建者的答案以及 cmets 中的讨论以获取更多信息:stackoverflow.com/a/3496042/362298
  • 我有一个相当大的垂直布局,我想在里面有一个TextViews 的列表。当然我可以在布局中放很多单TextView,但对我来说这似乎也不是很实用。

标签: android listview scroll


【解决方案1】:

xml 文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="19dp"
        android:text="@string/hello_world" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher" />

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

</ScrollView>

内部活动:

public class MainActivity extends Activity {
   private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
                 "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
   ListView myList;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          myList = (ListView) findViewById(R.id.listView);
          myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
          Helper.getListViewSize(myList);

   }
}

助手类:

public class Helper {
    public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
      //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}

【讨论】:

  • 不错,但有点慢。我认为您还添加了填充: params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1)) + listview.getPaddingBottom() + listview.getPaddingTop();
【解决方案2】:

当 ListView 包含在 ScrollView 中时,它应该扩展以填充尽可能多的垂直区域,因此您应该没有问题。

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 2015-12-08
    • 2015-05-22
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2021-10-28
    • 2013-10-01
    • 2015-07-29
    相关资源
    最近更新 更多