2013-04-21 98 views
0

我想突出顯示點擊自定義列表視圖的項目。我嘗試了不同的選擇,但不是他們正在工作。有人能告訴我什麼是錯的嗎?Android CustomListView高亮點擊項目

我的自定義列表視圖com.foo.ViewEditListView extends ListView implements android.widget.AdapterView.OnItemLongClickListener

list_layout.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" 
    android:layout_alignParentBottom="true" 
    android:orientation="vertical" 
    > 

    <TextView 
     android:id="@+id/textView_list_info" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical|center_horizontal" 
     > 
    </TextView> 

    <EditText 
     android:id="@+id/searchQueryText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:drawableRight="@drawable/search" 
     android:hint="Search Expenses by Amount"   
     android:textColorHint="#5e8e19" 
     android:inputType="numberSigned|numberDecimal" 
     style="@android:style/TextAppearance.Small"   
      > 

     </EditText> 

    <com.foo.ViewEditListView 
     android:id="@+id/viewExpenseListView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@/drawable/list_selector_bg" //doesn't work 
     /> 

</LinearLayout> 

list_row.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@/drawable/list_selector_bg" //doesn't work 
    > 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    > 

    <TextView 
     android:id="@+id/v_row_field1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:textColor="#C11B17" 
     android:layout_alignParentLeft="true" 
     /> 


    <TextView 
     android:id="@+id/v_row_field2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:textStyle="bold" 
     android:layout_marginRight="2dp" /> 

    </RelativeLayout> 

</LinearLayout> 



list_selector_bg.xml 

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

<item android:state_pressed="true" 
    android:drawable="@android:drawable/list_selector_background" />  


</selector> 

我也試圖在setSelector活動,但他們都不工作。現在,我無能爲力。有人可以幫我嗎?

如果我把android:background =「@/drawable/list_selector_bg」放到RelativeLayout中,它只會突出顯示那部分。但我希望整個佈局突出顯示。

感謝 CP

回答

1

具有自定義繪製設置爲背景的佈局。根據您的需要修改以下內容。按下後,您將擁有不同的背景,並且發佈將恢復爲默認狀態。

在你list_row.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" 
android:layout_alignParentBottom="true" 
android:background="@drawable/listbkg" // set backcground 
android:orientation="vertical" 
> 
.... 
</LinearLayout> 

listbkg.xml在繪製文件夾

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
    android:drawable="@drawable/pressed" /> //pressed state 
<item android:state_focused="false" 
    android:drawable="@drawable/normal" /> // normal state 
</selector> 

pressed.xml在繪製fodler

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FF1A47"/>  //background color 
<stroke android:width="3dp"   // border color 
     android:color="#0FECFF"/> 
<padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp" //for rounded corners  
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

normal.xml在繪製文件夾

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/> // change olor 
<stroke android:width="3dp" // for border color 
     android:color="#0FECFF" /> 

<padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp"// for rounded corners 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

編輯:

您還可以包含漸變,因爲您可以在快照中看到相同的漸變。

在getView()定製適配器的

 convertView =mInflater.inflate(R.layout.list_row, parent,false); 
    // inflating custom layout 

然後在list_row.xml設置爲根佈局的背景。

得到的快照

當按下它突出的佈局和發佈將回到正常狀態。

enter image description here

+0

嗨Raghunandan,我想你的解決方案。但它不起作用。還是一樣的行爲。你認爲VieEditListView導致一些問題嗎? – Chintan 2013-04-22 00:52:12

+0

@Chintan如果你做得對,這應該起作用。我用自定義drawable作爲背景的列表視圖行也一樣。 – Raghunandan 2013-04-22 04:21:27

+0

謝謝拉古南丹。它正在工作! – Chintan 2013-04-22 23:59:01

0

1)如果你想突顯選定的項目使用您的CustomAdapter你可以處理它。但是,這不是很好的方式:)

2)我認爲,最好的辦法是用普通的Android執行勾選的接口,爲您的觀點(爲例):

public class CheckableFrameLayout extends FrameLayout implements Checkable { 
    private boolean mChecked; 

    public CheckableFrameLayout(Context context) { 
     super(context); 
    } 

    public CheckableFrameLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public void setChecked(boolean checked) { 
     mChecked = checked; 
     setBackgroundDrawable(checked ? new ColorDrawable(0xff0000a0) : null); 
    } 

    public boolean isChecked() { 
     return mChecked; 
    } 

    public void toggle() { 
     setChecked(!mChecked); 
    } 
} 

OR

3)使用CheckedTextView

您可以在Android Samples中找到示例,名稱爲ApiDemos(ListView with multichoice)。

UPDATE: 不要忘記,你的ListView必須ChoiceMode = ListView.CHOICE_MODE_NONE

CheckableFrameLayout到你的包和重寫你的list_row.xml這樣的:

<com.foo.CheckableFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/list_selector_bg" 
    > 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <TextView 
     android:id="@+id/v_row_field1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:textColor="#C11B17" 
     android:layout_alignParentLeft="true" 
     /> 


    <TextView 
     android:id="@+id/v_row_field2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:textStyle="bold" 
     android:layout_marginRight="2dp" /> 

    </RelativeLayout> 
</com.foo.CheckableFrameLayout> 

如果你想獲得美國爲你的狀態列表CheckableFrameLayout(將其設置爲背景),那麼你需要覆蓋你的list_selector_bg.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_checked="true" android:drawable="@android:drawable/list_selector_background" /> 
</selector> 

並重寫CheckableFrameLayout與它一起工作。我沒有在匆忙中正確地做到這一點(我不知道爲什麼它只是在第二次點擊時正確地改變亮點)。但是你可以利用這個代碼,並提高它(也許使用的CheckedTextView代碼可以幫助你):

public class CheckableFrameLayout extends FrameLayout implements Checkable { 
    private static final int[] CHECKED_STATE_SET = { 
     android.R.attr.state_checked 
    }; 

    private boolean mChecked; 

    public CheckableFrameLayout(Context context) { 
     super(context); 
    } 

    public CheckableFrameLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected int[] onCreateDrawableState(int extraSpace) { 
     final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); 
     if (isChecked()) { 
      mergeDrawableStates(drawableState, CHECKED_STATE_SET); 
     } 
     return drawableState; 
    } 

    @Override 
    public void setChecked(boolean checked) { 
     mChecked = checked; 
     // setBackgroundDrawable(checked ? new ColorDrawable(0xff0000a0) : null); 
    } 

    @Override 
    public boolean isChecked() { 
     return mChecked; 
    } 

    @Override 
    public void toggle() { 
     setChecked(!mChecked); 
    } 
} 
+0

我不希望複選框被顯示。我如何將CheckableFrameLayout附加到我自定義的listview類(即ViewEditListView)? – Chintan 2013-04-22 01:00:49

+0

我也有customAdapter,我如何實現突出選定的項目? – Chintan 2013-04-22 01:18:42

+0

@Chintan我正在更新我的答案。使用適配器它不是很好的主意,但很多人使用它:( http://stackoverflow.com/questions/13699818/highlight-listview-item http://stackoverflow.com/questions/13137465/highlight- listview-items-android – nfirex 2013-04-22 07:21:26