Android的 - 是否有可能在一个列表视图禁用卷轴? [英] Android - Is it possible to disable scroll on a listview?

查看:227
本文介绍了Android的 - 是否有可能在一个列表视图禁用卷轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为主体,是有可能禁用滚动一个的ListView

我动态地添加项目到我的的ListView ,并希望使所有项目始终可见无需滚动。

编辑:
我加入我的布局code更清楚地解释我的情况:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@绘制/ recipe_inside
机器人:方向=垂直><滚动型
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>    <的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>        < ImageView的
            机器人:ID =@ + ID / imageView1
            机器人:layout_width =match_parent
            机器人:layout_height =16DP
            机器人:SRC =@绘制/ head_cella/>        <的TextView
            机器人:ID =@ + ID / textView1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =41dp
            机器人:背景=@绘制/ body_cella
            机器人:重力=CENTER_HORIZONTAL
            机器人:paddingTop =5DP
            机器人:文字=记住离开这个页面或插入/修改的数据将会丢失前保存。
            机器人:文字颜色=#000000
            机器人:TEXTSIZE =13dp/>        < ImageView的
            机器人:ID =@ + ID / ImageView01
            机器人:layout_width =match_parent
            机器人:layout_height =16DP
            机器人:layout_marginTop =30dp
            机器人:SRC =@绘制/ head_cella/>        <的TextView
            机器人:ID =@ + ID / TextView01
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =35dp
            机器人:背景=@绘制/ body_cella
            机器人:重力=CENTER_HORIZONTAL
            机器人:paddingTop =10dp
            机器人:文字=类别
            机器人:文字颜色=#000000
            机器人:TEXTSIZE =15dp/>        <微调
            机器人:ID =@ + ID / category_spinner
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT/>        < ImageView的
            机器人:ID =@ + ID / ImageView02
            机器人:layout_width =match_parent
            机器人:layout_height =16DP
            机器人:layout_marginTop =30dp
            机器人:SRC =@绘制/ head_cella/>        <的TextView
            机器人:ID =@ + ID / TextView02
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =35dp
            机器人:背景=@绘制/ body_cella
            机器人:重力=CENTER_HORIZONTAL
            机器人:paddingTop =10dp
            机器人:文字=姓名
            机器人:文字颜色=#000000
            机器人:TEXTSIZE =15dp/>        <的EditText
            机器人:ID =@ + ID / recipe_title
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:EMS =10
            机器人:提示=配方标题机器人:行=1/>        < ImageView的
            机器人:ID =@ + ID / ImageView03
            机器人:layout_width =match_parent
            机器人:layout_height =16DP
            机器人:layout_marginTop =30dp
            机器人:SRC =@绘制/ head_cella/>        <的TextView
            机器人:ID =@ + ID / TextView03
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =35dp
            机器人:背景=@绘制/ body_cella
            机器人:重力=CENTER_HORIZONTAL
            机器人:paddingTop =10dp
            机器人:文字=配料
            机器人:文字颜色=#000000
            机器人:TEXTSIZE =15dp/>
         < ListView控件
            机器人:ID =@机器人:ID /列表
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1机器人:cacheColorHint =#00000000机器人:分=#00000000
            机器人:isScrollContainer =假
            >
       < /&的ListView GT;         <按钮
             机器人:ID =@ + ID / button_ing
             机器人:layout_width =WRAP_CONTENT
             机器人:layout_height =WRAP_CONTENT
             机器人:文字=机器人:背景=@绘制/ add_recipe_button机器人:的onClick =为addItems/>    < / LinearLayout中>< /滚动型>< / LinearLayout中>

另一个编辑:
旧的OnCreate()函数code:

 保护无效的onCreate(捆绑savedInstanceState)
{    super.onCreate(savedInstanceState);    的setContentView(R.layout.add_recipe_form);    微调=(微调)myhead.findViewById(R.id.category_spinner);    光标光标= mydb.getCategoriesList();
    ArrayList的<类别及GT;名单=新的ArrayList<类别及GT;();
    list.add(新类别(0,选择一个类别));    而(cursor.moveToNext()){
        list.add(新类别(cursor.getInt(0),cursor.getString(1)));
    }
    //第2步:创建和一帮国家的对象填写的ArrayAdapter
    ArrayAdapter spinnerArrayAdapter =新ArrayAdapter(这一点,android.R.layout.simple_spinner_item,清​​单);    //第3步:告诉我们的适配器微调
    spinner.setAdapter(spinnerArrayAdapter);
    spinner.setOnItemSelectedListener(本);
    setListAdapter(适配器);
    ingredients.add((查看)findViewById(R.layout.row_add_recipe));
    adapter.notifyDataSetChanged();
}

如果我移动相关,以填补我的微调与数据库中的数据code,它总是返回我,这不是我的布局中发现一个NullPointerException异常。所以,我需要保持它在这里和液中加入addHeaderView()在我的ListView似乎是确定的,但我有一个关于我的头布局另一个NullPointerException异常:

 保护无效的onCreate(捆绑savedInstanceState)
{    super.onCreate(savedInstanceState);    的setContentView(R.layout.add_recipe_form);
    ListView控件mylistview =(ListView控件)findViewById(android.R.id.list);
    的LinearLayout myhead =(的LinearLayout)findViewById(R.id.linear_form);
    mylistview.addHeaderView(myhead);
    微调=(微调)myhead.findViewById(R.id.category_spinner);    光标光标= mydb.getCategoriesList();
    ArrayList的<类别及GT;名单=新的ArrayList<类别及GT;();
    list.add(新类别(0,选择一个类别));    而(cursor.moveToNext()){
        list.add(新类别(cursor.getInt(0),cursor.getString(1)));
    }
    //第2步:创建和一帮国家的对象填写的ArrayAdapter
    ArrayAdapter spinnerArrayAdapter =新ArrayAdapter(这一点,android.R.layout.simple_spinner_item,清​​单);    //第3步:告诉我们的适配器微调
    spinner.setAdapter(spinnerArrayAdapter);
    spinner.setOnItemSelectedListener(本);
    setListAdapter(适配器);
    ingredients.add((查看)findViewById(R.layout.row_add_recipe));
    adapter.notifyDataSetChanged();
}


解决方案

如果我理解你很好,你只需要一些更多的看法至极与你一起的ListView滚动。实现它的最好方法是调用<一个href=\"http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29\"相对=nofollow> addHeaderView() 和<一个href=\"http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29\"相对=nofollow> addFooterView()的ListView()

编辑:

应该是这样的,我在想:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);
    的setContentView(R.layout.your_main_layout);
    LayoutInflater吹气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看标题= inflater.inflate(R.layout.your_header_layout); //R.layout,不R.id!    (ListView控件)列表= findViewById(android.R.list);
    list.addHeaderView(头);    list.setAdapter(适配器); //根据文档应该叫addHeaderView后setAdapter()()    //得到微调
    (微调)微调=(微调)header.findViewById(R.id.spinner);}

我是从内存中写,并没有测试它,但我认为它应该工作。试试吧,当你得到了片刻。

As subject, is it possible to disable scroll on a ListView?

I dynamically add items to my ListView and want to make all the items visible always without scrolling.

EDIT: I'm adding my layout code to explain more clearly my situation:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/recipe_inside"
android:orientation="vertical" >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

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

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="16dp"
            android:src="@drawable/head_cella" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="41dp"
            android:background="@drawable/body_cella"
            android:gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="Remember to save before leaving this page or inserted/modified data will be lost."
            android:textColor="#000000"
            android:textSize="13dp" />

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="match_parent"
            android:layout_height="16dp"
            android:layout_marginTop="30dp"
            android:src="@drawable/head_cella" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:background="@drawable/body_cella"
            android:gravity="center_horizontal"
            android:paddingTop="10dp"
            android:text="Category"
            android:textColor="#000000"
            android:textSize="15dp" />

        <Spinner
            android:id="@+id/category_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/ImageView02"
            android:layout_width="match_parent"
            android:layout_height="16dp"
            android:layout_marginTop="30dp"
            android:src="@drawable/head_cella" />

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:background="@drawable/body_cella"
            android:gravity="center_horizontal"
            android:paddingTop="10dp"
            android:text="Name"
            android:textColor="#000000"
            android:textSize="15dp" />

        <EditText
            android:id="@+id/recipe_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Recipe title" android:lines="1"/>

        <ImageView
            android:id="@+id/ImageView03"
            android:layout_width="match_parent"
            android:layout_height="16dp"
            android:layout_marginTop="30dp"
            android:src="@drawable/head_cella" />

        <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:background="@drawable/body_cella"
            android:gravity="center_horizontal"
            android:paddingTop="10dp"
            android:text="Ingredients"
            android:textColor="#000000"
            android:textSize="15dp" />
         <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" android:cacheColorHint="#00000000" android:divider="#00000000"
            android:isScrollContainer="false"
            >
       </ListView>

         <Button
             android:id="@+id/button_ing"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="" android:background="@drawable/add_recipe_button" android:onClick="addItems"/>

    </LinearLayout>

</ScrollView></LinearLayout>

ANOTHER EDIT: old OnCreate() function code:

protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.add_recipe_form);

    spinner = (Spinner)myhead.findViewById(R.id.category_spinner);

    Cursor cursor = mydb.getCategoriesList();
    ArrayList<Category> list = new ArrayList<Category>();
    list.add(new Category(0,"Choose a category"));

    while (cursor.moveToNext()) {
        list.add(new Category(cursor.getInt(0),cursor.getString(1))); 
    }
    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list);

    // Step 3: Tell the spinner about our adapter
    spinner.setAdapter(spinnerArrayAdapter);   
    spinner.setOnItemSelectedListener(this);


    setListAdapter(adapter);
    ingredients.add((View)findViewById(R.layout.row_add_recipe));
    adapter.notifyDataSetChanged();
}

If I move code related to fill my spinner with database data, it always returns me a NullPointerException as it's not found in my layout. So I need to keep it here and the solution adding addHeaderView() to my ListView seems to be ok, but I have another NullPointerException regarding my head layout:

protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);

    setContentView(R.layout.add_recipe_form);
    ListView mylistview = (ListView) findViewById(android.R.id.list);
    LinearLayout myhead = (LinearLayout)findViewById(R.id.linear_form);
    mylistview.addHeaderView(myhead);
    spinner = (Spinner)myhead.findViewById(R.id.category_spinner);

    Cursor cursor = mydb.getCategoriesList();
    ArrayList<Category> list = new ArrayList<Category>();
    list.add(new Category(0,"Choose a category"));

    while (cursor.moveToNext()) {
        list.add(new Category(cursor.getInt(0),cursor.getString(1))); 
    }
    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list);

    // Step 3: Tell the spinner about our adapter
    spinner.setAdapter(spinnerArrayAdapter);   
    spinner.setOnItemSelectedListener(this);


    setListAdapter(adapter);
    ingredients.add((View)findViewById(R.layout.row_add_recipe));
    adapter.notifyDataSetChanged();
}

解决方案

If I understand you well, you just need some more views wich are scrollable along with your listview. The best way to implement it is to call addHeaderView() and addFooterView() on your ListView().

EDIT:

It should go like this, I think:

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_main_layout);
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View header = inflater.inflate(R.layout.your_header_layout); //R.layout, not R.id!

    (ListView) list = findViewById(android.R.list);
    list.addHeaderView(header);

    list.setAdapter(adapter); //according to docs you should call setAdapter() after addHeaderView()

    //to get the spinner
    (Spinner) spinner = (Spinner) header.findViewById(R.id.spinner);

}

I'm writing from memory and didn't test it, but I think it should work. Try it out when you get a moment.

这篇关于Android的 - 是否有可能在一个列表视图禁用卷轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆