簡體   English   中英

如何在SeparatedListAdapter中設置文本顏色?

[英]How do I set the text color in a SeparatedListAdapter?

我正在使用傑夫·沙基(Jeff Sharkey)的SeparatedListAdapter,並且我想設置文本顏色,但是我不確定如何設置。

為了給您一些有關適配器的背景知識,他將BaseAdapter子類化為一個簡單的Android列表。 因此,我試圖像這樣在getView()方法中設置文本顏色(您可以在注釋部分之間看到我的嘗試):

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    int sectionnum = 0;
    for(Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;



        // check if position inside this section
        if(position == 0) return headers.getView(sectionnum, convertView, parent);
        if(position < size)
        {
            /***** I added this section to try to set the text color ***/

            TextView captionTV = (TextView)adapter.getView(position, convertView, parent).findViewById(R.id.list_complex_caption);
            captionTV.setTextColor(R.color.black;);

            TextView titleTV = (TextView)adapter.getView(position, convertView, parent).findViewById(R.id.list_complex_title);
            titleTV.setTextColor(R.color.black;);

            /***** end add *****/               

            return adapter.getView(position - 1, convertView, parent);
        }

        // otherwise jump into next section
        position -= size;
        sectionnum++;
    }
    return null;
}

但是發生的是,它為第一個單元格設置了文本顏色,但沒有為其余單元格設置文本顏色。

有任何想法嗎?

嗯。 漂亮的適配器。

這里要注意的第一件事-修改View的方式最好留給您在各個節適配器中使用的布局,例如,如果您要使用黑色文本,請使用具有黑色文本的項目布局。

無論如何要在代碼中執行此操作,您都不應在子節適配器上重復調用#getView(int, View, ViewGroup) ,而是這樣做:

/**
 * {@inheritDoc}
 */
public View getView(int position, View convertView, ViewGroup parent) {
    int sectionnum = 0;
    for(Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if(position == 0) return headers.getView(sectionnum, convertView, parent);
        if(position < size){
            View view = adapter.getView(position - 1, convertView, parent);
            TextView captionTV = (TextView) view.findViewById(R.id.list_complex_caption);
            captionTV.setTextColor(R.color.black);
            TextView titleTV = (TextView) view.findViewById(R.id.list_complex_title);
            titleTV.setTextColor(R.color.black);
            return view;
        }

        // otherwise jump into next section
        position -= size;
        sectionnum++;
    }
    return null;
}

嘗試在bindView中做到這一點,以覆蓋bindView函數,例如

@Override
public void bindView(View v, Context context, Cursor c) {

    String name = c.getString(nameCol);

    TextView captionTV = (TextView) v.findViewById(R.id.list_complex_caption);
    captionTV.setTextColor(R.color.black);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM