2012-06-12 71 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>