1

私のAndroidアプリケーションでは、リスト項目ごとにリストビューと詳細ビューがあります。タブレットの場合、アイテムのリスト ビューと選択したアイテムの詳細ビューを次のように表示しました。

ここに画像の説明を入力

だから私の問題は、ユーザーがリスト項目をクリックした後に選択した項目をどのように強調表示できるかです。

リスト ビューを読み込むために BaseAdapter を使用しています。

編集:

はい、chintan khetiyaが述べたように、次のxmlファイルをリストアイテムの背景として使用しましたが、選択したアイテムを喜ばせません. 私は何を逃したのですか?

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:drawable="@color/abs__background_holo_light" />
<item android:state_pressed="true" 
    android:drawable="@color/action_bar_blue" />
<item android:state_selected="true"
 android:state_pressed="false" 
    android:drawable="@color/action_bar_blue" />
</selector>
4

3 に答える 3

3

あなたのクエリ:

私の問題は、ユーザーがリスト項目をクリックした後に、選択した項目を強調表示する方法です。

セレクターについて質問していると思います。リスト行がフォーカス状態にある場合、他のすべての行とは異なって見えるはずです。行を押したりタッチしたりしても同じです。

そのためには、Selector.xmlファイルを Drawable フォルダーに作成し、そのセレクター ファイルをリスト行に配置する必要があります。

そのファイルには、次のような異なるタグが必要でありFocus-Click-Press、状態ごとに Drawable を変更する必要があります。

アップデート :

アイコンを置き換えて、Drawable フォルダーに保存するだけです。

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

    <!-- Pressed -->
    <item android:drawable="@drawable/p_paly_press" android:state_pressed="true"/>

    <!-- selected -->
    <item android:drawable="@drawable/p_play" android:state_selected="true"/>

    <!-- focused -->
    <item android:drawable="@drawable/p_paly_press" android:state_focused="true"/>

    <!-- default -->
    <item android:drawable="@drawable/p_play"/>

</selector>
于 2013-02-01T11:32:47.023 に答える
2

以下のコードに似たコードを使用して、選択した項目を強調表示することができます (他の方法では必要ない場合)。

class Adapter extends ArrayAdapter<String> {

    private int selectedPos = -1;
    Drawable selectedBackground;

    public MainSelectAdapter(Context context, int textViewResourceId,
            List<String> objects) {
        super(context, textViewResourceId, objects);
        selectedBackground =
              context.getResources().getDrawable(R.color.selecteditembackground);
    }
    public void setSelectedPosition(int pos){
        selectedPos = pos;
        notifyDataSetChanged();
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if (selectedPos == position) {
            v.setBackgroundDrawable(selectedBackground);
        } else {
            v.setBackgroundDrawable(null);
        }
        return v;
    }
}

そして、主な活動では

  Adapter adapter = new Adapter(this, android.R.layout.simple_list_item_1,
    myItemsToShow);

  list = (ListView) findViewById(R.id.flows);
  list.setItemsCanFocus(true);
  list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> adapter, View view,
                int pos, long id) {
                adapter.setSelectedPosition(pos);
    }
  });

このアプローチでは、選択したアイテムの強調表示を独自に制御し、選択イベントをキャプチャするリスナーを用意し、必要な Drawable を自分で設定します。

于 2013-02-01T12:02:47.707 に答える
1

あなたのstyles.xmlで定義することができます

    <style name="Theme.Base" parent="...">
        <item name="activatableItemBackground">@drawable/activatable_item_background</item>
    </style>

   <style name="ListItemContainerBase">
        <item name="android:background">?activatableItemBackground</item>
    </style>

res/drawable で activatable_item_background.xml を定義します

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/item_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/item_focused" android:state_focused="true" />
    <item android:drawable="@drawable/item_focused" android:state_selected="true" />
    <item android:drawable="@drawable/item_activated" android:state_activated="true" />
    <item android:drawable="@drawable/item_checked" android:state_checked="true" />
    <item android:drawable="@android:color/transparent" />
</selector>

item_pressed、item_focused..... は res/drawable-xxx の画像です

ビュー内の各アイテムに対して、次のようなレイアウトを定義します。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ListItemContainerBase">
于 2013-02-01T11:53:45.577 に答える