通过代码在创建时突出显示ListView项

时间:2013-09-07 09:22:16

标签: android listview android-listview

我想在创建listView时突出显示ListView的第一行(0)。我尝试了不同的方式(就像你在注释代码中看到的那样),但没有任何效果。 这很奇怪,因为OnItemClickListener中的亮点很精细! (它通过xml选择器工作)。

我的片段中有这个方法,它由onCreateView调用:

    private void createListViewAll(View rootView, int listID, String[][] textList) 
    {
        MainListAdapter bindingData = new MainListAdapter(getActivity(), textList, true);
        ListView list = (ListView) rootView.findViewById(listID);
        list.setAdapter(bindingData);
        HelperListView.getListViewSize(list);

//      list.setItemsCanFocus(true);
//        list.setItemChecked(0, true);
//        list.setSelection(0);
//        list.setSelected(true);
//        list.requestFocus();
//
//      list.setSelection(0);
//      list.setSelected(true);
//      list.getChildAt( list.getHeaderViewsCount() - list.getFirstVisiblePosition() ).setSelected(true);
//      list.getChildCount();

//      bindingData.getCount();
//      View listItem = bindingData.getView(0, null, list);
//      listItem.setActivated(true);
//      listItem.setSelected(true);
//      list.invalidate();
//      list.isItemChecked(0);

        currentView = bindingData.getView(0, null, list);


        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 
            {
                mItemSelected.onItemSelected(position);
                highlightView(view);
            }
        });

    }

    private void highlightView(View view) 
    {
        if (view != currentView)
        {
            if (currentView!=null)
            {
                currentView.isSelected();
                currentView.setBackgroundResource( R.drawable.gradient_bg );
                currentView.setSelected(false);
                //currentView.invalidate();
            }
            view.setSelected(true);
            currentView = view;
        }
    }

(currentView只是一个静态变量)

listAdapter中的getView:

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

    View vi=convertView;
    if(convertView==null)
    {
        vi = inflater.inflate(R.layout.list_row, null);
        holder = new ViewHolder();

        holder.titleRow = (TextView)vi.findViewById(R.id.titleRow); 
        holder.subtitleRow = (TextView)vi.findViewById(R.id.subtitleRow);

        vi.setTag(holder);
    }
    else{

        holder = (ViewHolder)vi.getTag();
    }

    if (position==0 && highlightFirstElement)
    {
        vi.setSelected(true);
        vi.setBackgroundResource( R.drawable.gradient_bg_hover );
    }

    if (textList!=null)
    {
        holder.titleRow.setText(textList[0][position]);
        holder.subtitleRow.setText(textList[1][position]);
    }
    else
    {
        holder.titleRow.setText( arguments[idSubject.get(position)] );
        holder.subtitleRow.setText(subTitle.get(position));
    }
    return vi;
}

1 个答案:

答案 0 :(得分:0)

在listview的适配器类的getView方法中,从布局文件中获取文本的id并应用背景颜色。

例如,

         public View getView(int position, View convertView, ViewGroup parent) {
         View v = null;

         String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater li = (LayoutInflater) my_ctx
                    .getSystemService(inflater);
            v = li.inflate(R.layout.layoutfile, parent, false);
                          if(position==0)
                           {

              TextView text = (TextView) findViewById(R.id.textView1);
              text.setBackgroundColor(Color.ANY_COLOR);
                            }
 }