在android列表视图中设置焦点行单元格的背景

时间:2014-03-05 09:04:02

标签: android android-listview

我开发了一个应用程序,其中包含列表视图和从数据库获取的数据,在数据库中我有计时详细信息,所以我想要实现,比如当我开始列表活动(主要活动)时它自动关注列表视图项目上的当前时间详细信息和那个单元格用不同的背景或颜色突出显示我在这方面付出了太多的努力但仍然无法正常工作,我使用ListView.setselection方法但得到空指针免除错误

我的代码在这里..

 boolean isSelected = false;
            for (int i = 0; i < ListViewAdapter.List.size(); i++) {
                try {
                    placeTime = (Date) format.parse(((PlaceRouteDetail) ListViewAdapter.List.get(i)).getTime());
                    hourPlaceTime = placeTime.getHours();
                    minutePlaceTime = placeTime.getMinutes();
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                if (hour <= hourPlaceTime & minute <= minutePlaceTime) {
                    ListView.setSelection(i);
                    ListView.getChildAt(i).setBackground(Color.CYAN);
                    isSelected = true;
                    break;
                }

2 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class MyCustomAdapter extends BaseAdapter {
private ArrayList mListItems;
private LayoutInflater mLayoutInflater;
private Context mContext;

public MyCustomAdapter(Context context, ArrayList arrayList){

    mListItems = arrayList;
    mContext = context;

    //get the layout inflater
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    //getCount() represents how many items are in the list
    return mListItems.size();
}

@Override
//get the data of an item from a specific position
//i represents the position of the item in the list
public Object getItem(int i) {
    return null;
}

@Override
//get the position id of the item from the list
public long getItemId(int i) {
    return 0;
}

@Override

public View getView(int position, View view, ViewGroup viewGroup) {

    // create a ViewHolder reference
    ViewHolder holder;

    //check to see if the reused view is null or not, if is not null then reuse it
    if (view == null) {
        holder = new ViewHolder();

        view = mLayoutInflater.inflate(R.layout.list_item, null);
        holder.itemName = (TextView) view.findViewById(R.id.list_item_text_view);

        // the setTag is used to store the data within this view
        view.setTag(holder);
    } else {
        // the getTag returns the viewHolder object set as a tag to the view
        holder = (ViewHolder)view.getTag();
    }

    //get the string item from the position "position" from array list to put it on the TextView
    String stringItem = (String) mListItems.get(position);
    if (stringItem != null) {
        if (holder.itemName != null) {
            //set the item name on the TextView
            holder.itemName.setText(stringItem);
        }
    }

    if (position == 3) { // hardcoded position
        // highlighted row
        view.setBackgroundColor(mContext.getResources().getColor(R.color.white));
    } else {
        // normal row
        view.setBackgroundColor(mContext.getResources().getColor(R.color.black));
    }

    //this method must return the view corresponding to the data at the specified position.
    return view;

}

/**
 * Static class used to avoid the calling of "findViewById" every time the getView() method is called,
 * because this can impact to your application performance when your list is too big. The class is static so it
 * cache all the things inside once it's created.
 */
private static class ViewHolder {

    protected TextView itemName;

 }
}

答案 1 :(得分:0)

您可以尝试使用适配器中的布局。您应该做的是为要突出显示的位置设置变量。

int highlightPosition = 3; // define your position somehow

然后你应该在列表视图适配器中检查这个位置。

class ReactionListAdapter extends BaseAdapter {

    ...

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

        ...

        if(position == highlightPosition) {
            // highlight the row 
        }
    }
}

更改位置后,只需调用适配器的notifyDataSetChanged。

我也必须突出我的行一次,然后我使用了TransitionDrawable。在行的背景中使用转换drawable并开始转换,而不是立即更改背景颜色。它看起来更好。

为行背景创建一个drawable。

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
    <item android:drawable="@drawable/bg_row_normal" />
    <item android:drawable="@drawable/bg_row_highlight" />
</transition>

在行布局中设置此项。

android:background="@drawable/bg_row"

所以从视图中获取背景:

background = (TransitionDrawable) convertView.getBackground();

并开始过渡。

background.startTransition(duration);

最后你的适配器应如下所示:

class ReactionListAdapter extends BaseAdapter {

    ...

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

        ...

        if(position == highlightPosition) {
            // highlight the row 
            TransitionDrawable background = (TransitionDrawable) convertView.getBackground();
            background.startTransition(duration);
        }
    }
}