2

I would like to highlight the first row (0) of my ListView when I create the listView. I tried different ways (like you can see in the commented code), but nothing worked. It's strange because the highlight in the OnItemClickListener WORKS FINE! (It works via an xml selector).

I have this method in my fragment that it's called by 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 is just a static variable)

the getView in the listAdapter:

    @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;
}
Accollativo
  • 1,537
  • 4
  • 32
  • 56
  • I guess if you try to do selection via code you have to do that in the getView method of the adapter which you are binding with your listView. – Ritaban Sep 07 '13 at 09:34
  • I already tried to do this, it's commented: // View listItem = bindingData.getView(0, null, list); // listItem.setSelected(true); – Accollativo Sep 07 '13 at 10:16
  • have u tried using listview.invalidate() after making the change. – Ritaban Sep 07 '13 at 10:21
  • Just now. Still don't work. – Accollativo Sep 07 '13 at 10:26
  • I tried to extend ListView and override the method onAttachedToWindow: @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); setSelection(0); setItemChecked(0, true); invalidate(); } But didn't work. – Accollativo Sep 08 '13 at 11:13
  • Did you try to override setChecked to see if it goes through it ? – An-droid Sep 09 '13 at 07:41
  • And if you are using a version pre honeycomb : http://stackoverflow.com/questions/17070905/setitemchecked-not-working-on-gingerbread – An-droid Sep 09 '13 at 07:47
  • I used list.isItemChecked(0) that return true, so the item is correctly checked. I'm using android 4.2 with the v4 support library. – Accollativo Sep 09 '13 at 12:04
  • I tried to call the click event too (just after the listener declaration), but doesn't work: View listItem = bindingData.getView(0, null, list); list.performItemClick(listItem, 0, 0); The strange thing is that except this case, the item click change the item's background correctly. – Accollativo Sep 09 '13 at 12:19

1 Answers1

0

In getView method of listview's adapter class,take id of text from Layout file and apply background color to it.

for example,

         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);
                            }
 }
Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
  • I edited the first post. I tried and It nearly worked. The first item is highlighted, but when I click on another item, the first stay highlighted and the other too. – Accollativo Nov 04 '13 at 16:09
  • You need to create selector.xml file in your drawable folder and set background of textview as this selector file... – Jigar Pandya Nov 13 '13 at 06:48
  • Like you can read in the first post, I already have a selector, but it don't work good. – Accollativo Nov 13 '13 at 22:00