2

我想在创建 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;
}
4

1 回答 1

0

在 listview 的适配器类的 getView 方法中,从 Layout 文件中获取文本的 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);
                            }
 }
于 2013-11-02T05:51:23.180 回答