2011-09-24 179 views
3

私はBluetooth経由で他のデバイスとペアリングする必要があるアプリケーションを開発しています。私はペアリングされたデバイスのリストを正しい方法で表示するのに問題がありました。私はアンドロイドのapi(Bluetooth Chat)の例も見ていましたが、私は同じ問題を抱えていました。リストビューの行数を制限する

ペアリングデバイスのリストが大きいにあり、その後、リストの一番下にある検索ボタンを非表示になります。

私のXMLコードは、例の非常に同じです:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView android:id="@+id/listpaired_devices" 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/title_paired_devices" 
    android:visibility="gone" 
    android:background="#666" 
    android:textColor="#fff" 
    android:paddingLeft="5dp" 
    /> 
    <ListView android:id="@+id/paired_devices" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stackFromBottom="true" 
    android:layout_weight="1" 
    /> 
    <TextView android:id="@+id/list_new_devices" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/title_other_devices" 
    android:visibility="gone" 
    /> 
    <ListView android:id="@+id/new_devices" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stackFromBottom="true" 
    android:layout_weight="2" 
    /> 
    <Button android:id="@+id/btscan" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/btscan" 
    /> 
</LinearLayout> 

しかし、私は一番下にある検索ボタンを表示することはできません。 ここで私の画面: My Screen

ダイアログウィンドウの下部に少しのボタンが表示されます。

これは、リストビューに表示行数を制限することが可能ですか?誰もがあなたからの情報を隠しているので、あなたは、それは本当にあなたが達成したいものの中にあなたを助けるかどうかわからリストビューに表示行数を制限することはできませんが、私はこの問題を解決する方法を

答えて

5

まずいくつかのポイントを、あなたのコードについて:。

  • layout_weightだけ意味があるオブジェクトが特定の次元には大きさを持っていない場合、つまり、あなたはlayout_heightまたは0からlayout_width設定されているので、これはあなたのコードには影響しません。
  • ListViewの高さをwrap_contentに設定するのは無意味ですが、うまく機能していても悪いことです。 'fill_parent'または明確な高さのいずれかを使用します。
  • 上記の点ごとに、あなたが作成したリストビューには事前に定義された大きさを持っていない、ので、ボタンが隠されているので、画面オフボタンを押して、彼らができる限り多くのスペースを取ります。

ここに本当にあるものを考えてみましょう。それは一番下にボタンが付いた単一のリストです(ヘッダーや複数のデータソースがあるかもしれませんが、それに気づくでしょう)。

次のレイアウトは、あなたの一番上にListViewコントロールと下部のボタンを与えます。 ListViewは、Buttonによって使用されていないスペースを占有します。ボタンがの前にのレイアウトで定義されていることに注目してください。これにより、リストビューを検討する前にボタンがどれくらいの高さになっているか計算されます。その後、利用可能な高さの残りの部分をListViewに渡します。

<RelativeLayout 
    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/btscan" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:text="@string/btscan" 
    /> 
    <ListView android:id="@+id/all_devices" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@id/btscan" 
    android:stackFromBottom="true" 
    /> 
</RelativeLayout> 

これはレイアウトです。次に、ヘッダーと、ペアになったデバイスのリストと、別のヘッダーと、次に新しいデバイスのリストが続くリストの実際の内容を検討してみましょう。

これは単一アダプタを使用して作成できます - CommonswareはMergeAdapterという非常に優れた実装を提供しますが、独自のコードを作成することもできます。 MergeAdapterはビューを直接(たとえばヘッダーのために)表示させませんが、ありがたいことにCommonswareはSackOfViewsAdapterも提供しています。このビューにはビューを追加してから、SackOfViewsAdapterをMergeAdapterに追加できます。

ここでは、上に概説した内容を達成する必要がある擬似コードを示します。

// This is the adapter we will use for our list with ListView.setAdapter 
MergeAdapter listAdapter = new MergeAdapter(); 

// The first header - you'll need to inflate the actual View (myHeaderView1) from some resource 
// using LayoutInflater 
List<View> firstHeaderViews = new List<View(); 
firstHeaderViews.add(myHeaderView1); 
SackOfViewsAdapter firstHeaderAdapter = new SackOfViewsAdapter(firstHeaderViews) 

// Second header 
List<View> secondHeaderViews = new List<View(); 
secondHeaderViews.add(myHeaderView2); 
SackOfViewsAdapter secondHeaderAdapter = new SackOfViewsAdapter(secondHeaderViews); 

// Now to add everything to the MergeAdapter 

// First goes the first header 
listAdapter.addAdapter(firstHeaderAdapter); 

// Now your first list 
listAdapter.addAdapter(firstListAdapter); 

// Now your second header 
listAdapter.addAdapter(secondHeaderAdapter); 

// Now your second list 
listAdapter.addAdapter(secondListAdapter); 

// Now set the adapter to the list 
myList.setAdapter(listAdapter) 

レイアウトはlike thisのように表示されます。注記リストを拡張して、画面よりも長いリストでの動作を示しました。ボタンはまだ表示されたままです。赤い枠が画面の境界を示します。

+0

"表示できる最大行数"を指定することはできません。 – usman

2

を教えてもらえます彼にとって重要なユーザーかもしれません。リストビューのアダプタに渡す項目の数を制限することで行数を制限することができます。または、リストビューが特定の位置に到達したときにgetViewメソッドで可視性を ''に設定できます( 'position' getView()のパラメータ)。

しかし、私はリストアイテムのビューに '新規/他のデバイス'タイトルのセパレータを追加することをお勧めしますが、デフォルトで非表示にしてから、suri 、(スキャンボタンを配置する)リストビューのためにヘッダーとフッターを使用

関連する問題