I have a listview
in ListFragment
which gets populated from an adapter, I want to highlight the clicked (or selected) item in the list as well as do some action based on the selection. I am able to handle the event but How do I set the color of selected item from the list.
5 に答える
を使用している場合はListFragment
、in XML を指定していない可能性があります。つまり、 in コードListView
を設定する必要があります。listSelector
. になってしまうので、アクティビティのライフサイクルの早い段階で設定しないように注意してくださいIllegalStateException
。
level_list_selector.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="@drawable/level_gradient_bg" />
<item android:state_pressed="true" android:drawable="@drawable/level_gradient_bg_hover" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/level_gradient_bg_hover" />
</selector>
断片:
public class LevelFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Create view
...
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setSelector(R.drawable.level_list_selector);
}
}
注: セル行にカスタム レイアウトを使用している場合は、その上にもリスト セレクターを設定する必要があります。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/level_list_selector"
android:padding="5dip" >
...
</RelativeLayout>
必要な状態を含むセレクター ファイルを作成するだけです。それはあなたに解決策を提供します。例えば -
セレクター.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/bg_list_item_highlighted" /> <!-- @drawable/tab_focus -->
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/bg_list_item_pressed" /> <!-- @drawable/tab_press -->
</selector>
ListView
このセレクターを listselector として設定します。元、
<ListView
android:id="@+id/listView1"
android:width="wrap_content"
android:height="wrap_content"
android:listSelector="@drawable/selector" />
これらの例を見てください -
ListFragment で標準的な ListView の動作が必要な場合は、リスト アイテムの行レイアウトを設定して、背景として ?android:attr/activatedBackgroundIndicator を使用できます。そのようです:
ListItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator" <!--This is the important bit-->
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight" />
<TextView
android:text=""
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>
ListFragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:tools="http://schemas.android.com/tools"
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="@drawable/divider"
android:dividerHeight="0.5dp" />
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No data" />
</LinearLayout>
ListFragment フラグメント コードでは、カスタム レイアウトを使用します (これは mono コードですが、絶望しないでください)。
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
base.OnCreateView(inflater, container, savedInstanceState);
View view = inflater.Inflate(Resource.Layout.ListFragment, container, false);
//... do stuff
}
アダプターを作成するコード内のどこかで、ListItem レイアウトを使用します。この場合、データを Icon および Text ビューにマッピングする SimpleAdapter です。ListItem.xml の背景 (?android:attr/activatedBackgroundIndicator) は、通常のリストで表示されるように項目を動作させます。
var list = new List<IDictionary<string, object>>(listOfStuff.Count);
foreach (AMap map in listOfStuff) {
var dictionary = new JavaDictionary<string, object> {
{"text", map.Text}, {"icon", Resource.Drawable.SomeIcon}
};
list.Add(dictionary);
}
SimpleAdapter _adapter = new SimpleAdapter(Activity, list,
Resource.Layout.ListItem,
new string[] { "text", "icon" },
new int[] { Resource.Id.text,
Resource.Id.icon });
このためには、リスト項目の背景としてドローアブルを作成する必要があります。lisView からアイテムを「選択」した後、ドローアブルを透明または #000 に設定します。アイテムがセレクターを選択すると、メイドが表示されます。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://meta.android.com/apk/res/android">
<item android:drawable="@color/transparent" />
<item android:state_pressed="false" android:drawable="@color/BackgroundColor" />
</selector>
get view method() のアダプタークラスでは、次のように実行できます..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.setTag(position);
if(ur condition){
view.setBackgroundColor(Color.TRANSPARENT);
}else {
view.setBackgroundColor(Color.GREEN);
}
return view;
}