lAndroid ListView OnListItemClick API 8 Highlight

2019-09-05 06:58发布

问题:

As I am working on a project where it needs to work with API 8 I found a lot of examples on how to highlight a row would not work as they need minimum API 10 upwards.

So I have looked around and got the highlighter to work via OnListItemClick however my problem now is , when there is more than one item in the list, it allows for multiple items to be selected which is not what I want

I used the following to highlight and un highlight the row:

if (selectedrow == 0) {
   ((TextView)v).setBackgroundColor(Color.rgb(0, 153, 204)); 
   selectedrow = 1;
   oki.setEnabled(true);
   currpos = position;
   File fileselected = new File(pathi.get(currpos));

 } else {
  ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
  selectedrow = 0;
  oki.setEnabled(false);
   currpos = -1;

}

When I noticed it was highlighting more than one row I then tried the following before the above code:

if (currpos != -1 ) {
   ((ListView)l).smoothScrollToPosition(currpos);
   ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
   ((ListView)l).smoothScrollToPosition(position);
   selectedrow = 0;
 }

I have also tried the above as follows:

if (currpos != -1 ) {
  ((ListView)l).setSelection(currpos);
  ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
  ((ListView)l).setSelection(position);
  selectedrow = 0;
 }

EDIT*

The custom adapter is now working and is as follows but the highlighter is still an issue I hope posting the code will shed some light on this:

public class FileListAdapter extends ArrayAdapter<String>  {

     private List<String> itemsll; 

        public FileListAdapter(Context context, int row,
                List<String> iteml) {
            super(context,row,iteml);
            // TODO Auto-generated constructor stub
            this.itemsll = iteml;
        }

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

            View viewnull = convertView;
            if (viewnull == null) {
                LayoutInflater vrow;
                vrow = LayoutInflater.from(getContext());

                viewnull = vrow.inflate(R.layout.row, null);
            }
            String currow = itemsll.get(position);
            TextView rowtext = (TextView)viewnull.findViewById(R.id.rowtext);
            rowtext.setText(currow);

            if (currpos == position) {
                rowtext.setBackgroundColor(Color.BLUE);
            }

            return viewnull;
        }


        public void setSelectedPosition( int pos )
        {
            currpos = pos; // selectedPos is global variable to handle clicked position
            // inform the view of this change
            notifyDataSetChanged();
        }
     }

and then I added the onItemClick as follows:

public void onItemClick( AdapterView<?> arg0,View arg1, int arg2, Long arg3) {

         int pos = arg2;
         fl.setSelectedPosition(pos);
     }

Solved.
Soon registered that I could not use onListItemClick and onItemClick and also as the class is a ListActivity I had to use Listview lw = getListView() to then use the onItemClick method

回答1:

Your approach is wrong there is some thing called selector which works on all the api version almost and I have did it on API level 8 as well there is the example try this

Here's Adapter Layout

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:background="@drawable/rowselector" >

   . . . . . . 

</RelativeLayout>

you can see I am setting background which actually is a selector

My rowselector (keeping this in drawable)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="true" 
     android:state_pressed="true" android:drawable="@drawable/left_navbg_hover" />
    <item android:state_enabled="true"
     android:state_focused="true" android:drawable="@drawable/left_navbg_hover" />
    <item android:state_enabled="true"
     android:state_selected="true" android:drawable="@drawable/left_navbg_hover" />
    <item
     android:drawable="@drawable/left_navbg" />
</selector>

where left_navbg_hover and left_navbg is an Image I have used .

Try that , all the best !



回答2:

if you are using custom list adaptor than you can do one thing:

create one public method in your adaptor class like :

public void setSelectedPosition( int pos )
    {
        selectedPos = pos; // selectedPos is global variable to handle clicked position
        // inform the view of this change
        notifyDataSetChanged();
    }

In getView method change your backgroundcolor:

 if ( selectedPos == position )
    {
            ((TextView)v).setBackgroundColor(Color.rgb(0, 153, 204)); 
    }

onItemClick method call adaptor method:

@Override
    public void onItemClick( AdapterView<?> arg0, View arg1, int arg2, long arg3 )
    {
        int position = arg2;

        youradapter.setSelectedPosition( position );

    }