1

SearchActivityがあり、そのレイアウトファイルは次のとおりです。

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

上部の検索EditText、中央のリストビュー、画面の下部にある2つのボタン。

リストビューで1つのアイテムにフォーカスできません。リストビューアダプタのレイアウトファイルで、android:clickable="false" android:focusable="false" [まだ]を設定しました。アイテムを選択すると、選択したアイテムが強調表示されません。..次のコードも入力しようとしました。onItemClick

1

  // @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;
    }

2

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

#1はエミュレーターではまったく問題なく動作しますが、私のSamsung Galaxy Yでは、* 「アプリケーションが予期せず停止しました。もう一度やり直してください」と表示されます*

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

2 に答える 2

2

listview に選択モードがあります。これは、リスト ビューで永続的な選択を行うための標準コードです。

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

activate_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>
于 2012-06-13T16:14:07.600 に答える
1

Yipeee... https: //stackoverflow.com/a/10277370/840520 これで問題は解決しました。これが私が追加したもの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);
    }
于 2012-06-12T07:12:48.420 に答える