2011-02-02 18 views
4

xmlには1つのTableLayoutを含むScrollViewがあります。 私はそれをクリックするたびにフォーカス可能な行を持つことが可能かどうかです。 ...事前にテーブル内のフォーカス可能な行

おかげ

<ScrollView 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <TableLayout 
      android:id="@+id/table2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 
      <TableRow> 
       <RelativeLayout 
        android:id="@+id/rowlayout" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:background="@drawable/rowbackground2" > 
         <ImageView 
          android:id="@+id/icon" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:src="@drawable/icon_code_contact" 
          android:padding="7dip" 
          android:layout_alignParentLeft="true" /> 

         <TextView 
          android:id="@+id/contacts" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textStyle="bold" 
          android:text="Contacts" 
          android:textColor="#000" 
          android:textSize="18dip" 
          android:layout_toRightOf="@id/icon" 
          android:paddingTop="10dip" /> 

       </RelativeLayout> 
      </TableRow> 


      <TableRow> 
       <Button 
        android:id="@+id/contacts_button" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@drawable/contact_button" /> 
      </TableRow> 

は、私はすでに "フォーカス可能=" "真" と "focusableInTouchMode =" true "を" しかし、何事もなかっ試してみました: はここに私のXMLコードであります

答えて

0

もちろんです。 Rowには、onclickメソッドの名前を指定できるonClickプロパティがあります。

他の方法は、この行にあなたのxmlのid - nameを与えることです。次に、コードから呼び出してonClickイベントを追加できます。

+0

私はすでにあなたが行IDについて言ったことを行っているが、それはうまくいきませんでした。.. 私の問題は、それがクリックされていますときに私は行がフォーカス可能にすることができないということです// – Nikitas

+0

、私はわかりませんよあなたはfucusableを意味しますか、それはそれが選択されていることを示すべきであることを意味しますか?行内に相対レイアウトがあるため、クリックすると他のビューにフォーカスが当てられることがあります。おそらく、他の内部ビューはクリック可能であるためにfalseになるはずです... – Lumis

21

リストビューのように振る舞うためにテーブルを作成する方法について尋ねている場合は、私は次のようでした:

私の活動のためのレイアウトを定義するXMLファイルでは、私はテーブルを定義した:

<ScrollView 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" > 
    <TableLayout 
     android:id="@+id/my_table" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:stretchColumns="1" 
     android:shrinkColumns="1"/> 
</ScrollView> 
私は私のテーブルに行を追加する手順を宣言した活動に

<?xml version="1.0" encoding="UTF-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:focusable="true" 
    android:focusableInTouchMode="false" 
    android:clickable="true" 
    android:background="@android:drawable/list_selector_background"> 

    <TextView 
     android:id="@+id/cell_1" 
     ... 
    /> 
    ... 
    <TextView 
     android:id="@+id/cell_N" 
     ... 
    /> 
</TableRow> 

はその後、私は、テーブル行のレイアウトで別のtable_row_item.xmlファイルを書いた

private void addTableRow() { 
    final TableLayout table = (TableLayout) findViewById(R.id.my_table); 
    final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item, null); 

    TextView tv; 
    // Fill out our cells 
    tv = (TextView) tr.findViewById(R.id.cell_1); 
    tv.setText(...); 
    ... 
    tv = (TextView) tr.findViewById(R.id.cell_N); 
    tv.setText(...); 
    table.addView(tr); 

    // Draw separator 
    tv = new TextView(this); 
    tv.setBackgroundColor(Color.parseColor("#80808080")); 
    tv.setHeight(/*height of separaor line. 2dip will be enough*/); 
    table.addView(tv); 

    // If you use context menu it should be registered for each table row 
    registerForContextMenu(tr); 
} 
+0

これは素晴らしいです。 :) –

+0

これは良いリソースです。まことにありがとうございます! –

0

このコードは、選択したドロウアブルの背景(背景を持たないためにnullを使用できると思います)とデフォルトのAndroidの「選択済み」バックグラウンドの間を移動します。

row.setClickable(true); 
    row.setOnTouchListener(new OnTouchListener() 
    { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      final int action = event.getAction(); 
      Log.d("Touched!", "" + action); 
      switch (action & MotionEvent.ACTION_MASK) 
      { 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_CANCEL: 
      case MotionEvent.ACTION_OUTSIDE: 
       // case MotionEvent.ACTION_MOVE: 
       v.setBackgroundResource(R.drawable.border_bottom); 
       v.invalidate(); 
       break; 
      default: 
       v 
         .setBackgroundResource(android.R.drawable.list_selector_background); 
       v.invalidate(); 

      } 
      return false; 
     } 
    }); 
+0

より良いコードで更新してください。 – tacone

0

テーブルにデータを表示するためのシンプルで効果的なソリューションをお探しの場合は、TableViewがその仕事を行います。

SortableTableView Example

関連する問題