2012-06-05 34 views
8

私はListViewにアイテムを動的に追加し、スクロールせずにすべてのアイテムを常に見えるようにします。listViewでスクロールを無効にすることはできますか?

レイアウトコード:

<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> 

歳のOnCreate()関数のコード:

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(); 
} 

私は、データベースのデータと私のスピナーを埋めるために、コード関連を移動した場合、それはないとして、それは常に私にNullPointerExceptionが返されます私のレイアウトで見つかりました。だから私はここでそれを維持する必要があると私のListViewにaddHeaderViewを()を追加するソリューションは大丈夫のようですが、私は私の頭のレイアウトに関する別のNullPointerExceptionがあります

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(); 
} 
+0

'すべてのアイテムをスクロールせずに表示する 'スクリーンサイズを超えるアイテムをお持ちの場合はどうなりますか? –

+0

あなたはそれをより詳しく説明できますか?画面にアイテムを追加しすぎて表示すると、アイテムをどのように表示する必要がありますか(100個追加したなど) – Sprigg

+0

私のListViewは、他の要素が内部にあるLinearlayoutの内部にあり、ScrollViewにあります。 – dany84

答えて

4

を達成することができ、あなただけのいくつかのより多くを必要とすると言いますあなたのリストビューと共にスクロール可能なビュー。実装する最善の方法は、ListView()addHeaderView()addFooterView()と電話することです。

EDIT:

それはこのように行くべきだと思います:

@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); 

} 

私はメモリから書いていて、それをテストし、私はそれが動作するはずだと思いませんでした。あなたが瞬時にそれを試してみてください。

+0

これは興味深いです。私は頭のために新しいXMLレイアウトファイルを作成しましたが、このコードを使用すると、次のようになります。LinearLayout myhead =(LinearLayout)findViewById(R.id.linear_form);それは私に "ヌル"を返します。何か案が? – dany84

+0

どこに電話しますか?あなたの活動の 'onCreate()'では? –

+0

はい、私はそれをOnCreate()で呼び出します。便利な場合は、ここで私のOnCreate()関数の一部です:setContentView(R.layout.add_recipe_form); \t \tリストビューmylistview =(ListView)findViewById(android.R.id.list); \t \t LinearLayout myhead =(LinearLayout)findViewById(R.id.linear_form); \t \t mylistview.addHeaderView(myhead); – dany84

2

一つの解決策は、同じ大きリストビューの高さを行うことですコンテンツ。代わりに、リストビューの次のクラスを使用してこの効果を実現することができます

public class ExpandedListView extends ListView { 

    private int old_count = 0; 

    public ExpandedListView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
    if (getCount() != old_count) { 
     old_count = getCount(); 
     int totalHeight = 0; 
     int desiredWidth = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST); 
     for (int i = 0; i < getCount(); i++) { 
      View listItem = getAdapter().getView(i, null, this); 
      listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 

     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = totalHeight + (getDividerHeight() * (getCount() - 1)); 
     setLayoutParams(params); 
     requestLayout(); 
    } 

    super.onDraw(canvas); 
    } 

} 

とにかく、それはScrollViewの中にリストビューを持つことが悪い考えですので、私は、ヘッダー/フッターのビューとしてレイアウト内の他の要素を追加しますMichałKが指摘したように、addHeaderView()とaddFooterView()メソッドを使用します。

https://github.com/commonsguy/cw-advandroid/tree/master/ListView/HeaderFooter

+0

リストビューで特定の項目を取得する必要はありません。 'addHeaderView()'と 'addFooterView()'は簡単です。 –

+0

これらのヘッダー/フッターは固定され、常に表示されますか?彼はすべてがスクロール可能であることを望んでいると思う。 –

+0

これらはスクロール可能です。一部のカスタムビューはリストに行として追加されました。プラス、あなたはそれらのいくつかを追加することができますまたは他のビューと 'LinearLayout'を置くだけで、それはいい、カスタマイズ可能な解決策です –

1

があなたのコンテンツサイズよりも大きい、あなたのListView高さを行います

ここでは、これらのメソッドを使用する方法についての例があります。

は、私はあなたによく理解していればあなたは100以上 にこの方法を5リスト項目20SP高セットリストビューに高さを有するを持っている場合、あなたはそれを

+0

このようにして、スクロールできない 'ListView'を達成することができますが、' ListView'をいくつかのビューにしてスクロールさせるという最終的な目標を達成するより良い方法があります。すでに言及したように、 'ScrollView'の中に' ListView'を置くことは悪い考えです。 –

+1

これは、ExpandedListViewで自動的に達成できますが、私は答えにコピーしましたが、それは最適ではありません。 –

+0

新しい項目を追加すると、ClassCastExceptionが発生し、リストビューのレイアウトパラメータを変更しようとしました。 – dany84