【问题标题】:How to scroll LinearLayout united with ListView in android如何在android中滚动与ListView结合的LinearLayout
【发布时间】:2014-11-30 18:08:27
【问题描述】:

我正在开发一个安卓应用程序。

我将 LinearLayout 放在 ListView 上。我想滚动与 ListView 结合的 LinearLayout。

我的 xml 在下面。

你能告诉我上面的实现方法吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/header1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="3dp"
        android:choiceMode="singleChoice"
        android:orientation="vertical" >

    ・・・

    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:choiceMode="singleChoice" />

</LinearLayout>

【问题讨论】:

    标签: android listview scroll android-linearlayout


    【解决方案1】:

    您不应该将 ListView 放在 ScrollView 中,因为它非常昂贵并且违背了 ListView 的目的。请改用 LinearLayout/RelativeLayout。

    如果您想使用 LinearLayout 填充 ListView 项目,您需要创建一个适配器来获取数据并填充 listView。

    下面是一些接受字符串值并将它们显示在列表中的代码。

    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
      private final Context context;
      private final String[] values;
    
      public MySimpleArrayAdapter(Context context, String[] values) {
        super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = values;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(values[position]);
        // change the icon for Windows and iPhone
        String s = values[position];
        if (s.startsWith("iPhone")) {
          imageView.setImageResource(R.drawable.no);
        } else {
          imageView.setImageResource(R.drawable.ok);
        }
    
        return rowView;
      }
    } 
    

    您在 getView 中扩展行布局的位置 - 这将是您的 LinearLayout。

    http://www.vogella.com/tutorials/AndroidListView/article.html#androidlists_adapterintro - 了解更多信息。

    【讨论】:

      【解决方案2】:

      使用 ScrollView(示例):

      <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"
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
      
              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical" >
      
                  <LinearLayout
                      android:id="@+id/header1"
                      android:layout_width="match_parent"
                      android:layout_height="100dp"
                      android:layout_margin="3dp"
                      android:choiceMode="singleChoice"
                      android:orientation="vertical" >
      
      ・・・
      
              </LinearLayout>
      
              <ListView
                  android:id="@android:id/list"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:choiceMode="singleChoice" />
      
              </LinearLayout>
          </RelativeLayout>
      </ScrollView>
      

      记住 ScrollView 只能有一个子属性,所以你可以 f.e.使用RelativeLayout封装LinearLayout和ListView

      【讨论】:

      • 你可以用LinearLayout代替Relative来封装里面的东西:)
      【解决方案3】:

      ListView 有自己的 ScrollView,如果你想滚动 LinearLayout 就按照这个代码来。

      • 将 Scroll-view 声明为父级,并在其中仅声明一个子级 Linearlayout(Scrollview 仅包含一个子级)
      • 在 Linearlayout 中声明您想要的内容。

        <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"> 
        <LinearLayout
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="vertical" >
        
        <LinearLayout
            android:id="@+id/header1"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_margin="3dp"
            android:choiceMode="singleChoice"
            android:orientation="vertical" >
            .
            .
            .
        
        </LinearLayout>
        
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:choiceMode="singleChoice" />
        </LinearLayout>
        </ScrollView>
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 2013-04-26
        相关资源
        最近更新 更多