سؤال

لدي طريقة عرض قائمة ومحول يضع ألوان خلفية بالتناوب لعناصر القائمة (نمط قائمة "Zebra"):

public View getView(final int position, View convertView, ViewGroup parent) {
    int colorPos = position % colors.length;
    ...
    convertView.setBackgroundColor(colors[colorPos]);
    return convertView;
}

ولكن الآن ، عندما أقوم بتحديد عنصر باستخدام عجلة التمرير ، أو عندما أقوم بالنقر فوق عنصر ، فإن الألوان الأصلية لتحديد/النقر فوق لا تتجاوز خلفياتي المخصصة (يمكنني رؤية اللون الأصلي أسفل اللون الذي قمت بتعيينه).

كيف يمكنني ضبط الألوان الأصلية لهذه الحالات؟

هل كانت مفيدة؟

المحلول

أعتقد أن أسهل طريقة هي إنشاء اثنين من المحددات التي تستخدم كموارد الخلفية ، مع لون شفاف في الوضع state_sexted: (res/drawable/alterselector1.xml :)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:drawable="@drawable/transparent" />
    <item android:state_pressed="true"
        android:drawable="@drawable/transparent" />
    <item android:state_selected="false"
        android:drawable="@drawable/altercolor1"/>

</selector>

(RES/Drawable/AlterSelector2.xml :)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:drawable="@drawable/transparent" />
    <item android:state_pressed="true"
        android:drawable="@drawable/transparent" />
    <item android:state_selected="false"
        android:drawable="@drawable/altercolor2"/>
</selector>

(الدقة/القيم/الألوان. xml :)

<resources>
    <drawable name="transparent">#00ffffff</drawable>
    <drawable name="altercolor1">#ffffffff</drawable>
    <drawable name="altercolor2">#ff000000</drawable>
</resources>

ثم تقوم بتعيين الخلفيات في طريقة GetView للمحول باستخدام طريقة الانتكاس:

if (position % 2 == 0){
    reusableView.setBackgroundResource(R.drawable.alterselector1);
} else {
    reusableView.setBackgroundResource(R.drawable.alterselector2);
}

الآن عندما تختار صفًا ، لا تخفي خلفيتك المحدد الأصلي خلفه.

نصائح أخرى

تحتاج إلى تغيير لون تسليط الضوء على قائمتك إذا قمت بذلك عبر الأنماط

 <style name="Widget.AbsListView">
        <item name="android:listSelector">@drawable/my_selector</item>
 </style>

أو يمكنك تعيين نفس السمة في الكود my_selector قابلة للرسم - ابحث عن أمثلة في دليل SDK:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_window_focused="false"
        android:drawable="@color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@drawable/list_selector_background_disabled" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_transition" />

    <item android:state_focused="true"
        android:drawable="@drawable/list_selector_background_focus" />

</selector>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top