2012-06-12 73 views
0

我有一個搜索Activity,其佈局文件如下。ListView上的焦點問題

<?xml version="1.0" encoding="utf-8"?> 
       <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" > 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@color/white2" > 

        <EditText 
         android:id="@+id/et_search" 
         android:layout_width="280dp" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         android:layout_marginBottom="5dp" 
         android:layout_marginLeft="20dp" 
         android:layout_marginRight="20dp" 
         android:layout_marginTop="5dp" 
         android:drawableRight="@drawable/search_20" 
         android:ems="10" 
         android:hint="Search by name" /> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@color/grey05" 
        android:orientation="horizontal" > 

        <TextView 
         android:id="@+id/tv_name" 
         android:layout_width="1dip" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="20dp" 
         android:layout_weight="0.25" 
         android:text="Name" 
         android:textAppearance="?android:attr/textAppearanceMedium" > 
        </TextView> 

        <TextView 
         android:id="@+id/tv_sno" 
         android:layout_width="1dip" 
         android:layout_height="wrap_content" 
         android:layout_weight="0.25" 
         android:text="Serial No." 
         android:textAppearance="?android:attr/textAppearanceMedium" > 
        </TextView> 

        <TextView 
         android:id="@+id/tv_type" 
         android:layout_width="1dip" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="20dp" 
         android:layout_weight="0.25" 
         android:text="Type" 
         android:textAppearance="?android:attr/textAppearanceMedium" > 
        </TextView> 
       </LinearLayout> 

       <ListView 
        android:id="@+id/listView1" 
        android:layout_width="match_parent" 
        android:layout_height="0dip" 
        android:layout_weight="1" 
        android:transcriptMode="normal" > 
       </ListView> 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="2dp" 
        android:layout_marginRight="2dp" 
        android:orientation="horizontal" > 

        <Button 
         android:id="@+id/btn_chkin" 
         android:layout_width="0dip" 
         android:layout_height="50dip" 
         android:layout_marginBottom="5dip" 
         android:layout_marginRight="2dip" 
         android:layout_marginTop="20dip" 
         android:layout_weight="1" 
         android:background="@drawable/custombutton" 
         android:text="Submit" /> 

        <Button 
         android:id="@+id/btn_chkout" 
         android:layout_width="0dip" 
         android:layout_height="50dip" 
         android:layout_marginBottom="5dip" 
         android:layout_marginLeft="2dip" 
         android:layout_marginTop="20dip" 
         android:layout_weight="1" 
         android:background="@drawable/custombutton" 
         android:text="Cancel" /> 
       </LinearLayout> 

      </LinearLayout> 

A搜索EditText在上面,在該中心,並在屏幕的底部的兩個按鈕的列表視圖。

我無法將一個項目集中在列表視圖中。 在爲列表視圖適配器佈局文件,我已經設置android:clickable="false" android:focusable="false" 不過,當我選擇一個項目,所選項目沒有得到突出.. 我也試圖把下面的代碼onItemClick

// @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
      long id) { 

     if (lview3 != null) { 
      lview3.getChildAt(prevPos).setBackgroundColor(Color.BLACK);   
      lview3.getChildAt(position).setBackgroundColor(Color.BLUE); 
     } 
     prevPos = position; 
    } 
// @Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
     long id) { 
     lview3.setSelection(position); 
} 

#1工程模擬器上精美絕倫,但在我的三星Galaxy Y,它說* 「APPLI陽離子已意外停止。請再試一次「*

對於#2,只要我觸摸屏幕上的項目,只要我移開手指,高光顏色消失,項目就會高亮顯示。

我listview_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linLayout_search" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/tv_name" 
     android:layout_width="1dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.25"   
     android:text="Medium Text" 
     android:textSize="15sp" /> 

    <TextView 
     android:id="@+id/tv_sno" 
     android:layout_width="1dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.25"  
     android:text="Medium Text" 
     android:textSize="15sp" /> 

    <TextView 
     android:id="@+id/tv_type" 
     android:layout_width="1dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.30"   
     android:text="Medium Text" 
     android:textSize="15sp" /> 

</LinearLayout> 
+0

android:clickable =「false」android:focusable =「false」設置這些屬性爲true。 – Akram

+0

發佈你的錯誤日誌 –

+0

不要在模擬器中檢查你的應用程序,如果你用任何Android手機檢查你可以捕獲所有的錯誤。 – anoop

回答

0

Yipeee ... https://stackoverflow.com/a/10277370/840520 這解決了這個問題。下面是我added..in的getView()ListAdapter

// set selected item 
    LinearLayout activeItem = (LinearLayout) convertView; 

    if (position == SearchAssetActivity.selectedItem){ 
     activeItem.setBackgroundColor(Color.GRAY); 
     // for focus on it 
     int top = (activeItem == null) ? 0 : activeItem.getTop(); 
     ((ListView) parent).setSelectionFromTop(position, top); 
    } 
    else{ 
     activeItem.setBackgroundColor(Color.BLACK); 
    } 
2

你必須選擇模式在列表視圖。這是在列表視圖進行持續性的選擇標準代碼

// inside onCreate method 
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // Make the newly clicked item the currently selected one. 
    getListView().setItemChecked(position, true); 
} 

在這之前,你需要設置列表視圖項目的佈局背景的選擇

android:background="@drawable/activated_background" 

activated_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="400"> 

    <item android:drawable="#ff0099cc" android:state_selected="true"/> 
    <item android:drawable="#ff0099cc" android:state_checked="true"/> 
    <item android:drawable="#ff0099cc" android:state_activated="true"/> 
    <item android:drawable="@android:color/transparent"/> 

</selector>