highlight listview item

若如初见. 提交于 2019-12-06 03:38:57

remove the selector and try the following:

EDIT

public class UsersAdapter extends BaseAdapter implements OnClickListener {

        LoginActivity context;
        private List<String> listOfUsers;
        boolean[] arrBgcolor;
        private int blue = Color.BLUE;

        public UsersAdapter(LoginActivity _context, List<String> listOfUsers) {
            context = _context;
            this.listOfUsers = listOfUsers;

            arrBgcolor = new boolean[listOfUsers.size()];
            resetArrbg();

        }

        private void resetArrbg() {
            for (int i = 0; i < arrBgcolor.length; i++) {
                arrBgcolor[i] = false;                  
            }
        }

        ......

        public View getView(final int position, View convertView, ViewGroup parent) {
            String entry = listOfUsers.get(position);
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.users_row, null);
            }


            TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
            UserName.setText(entry);

            LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
            LinLayout.setFocusableInTouchMode(false);
            LinLayout.setFocusable(false);
            LinLayout.setOnClickListener(this);

            //add the following, the position is accessible from here
            if (arrBgcolor[position]) {
                convertView.setBackgroundColor(blue);
            }
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    resetArrbg();
                    arrBgcolor[position] = true;
                    notifyDataSetChanged();
                }
            }); 
            return convertView;
        }

////@drawable/list_selector

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:drawable="@drawable/list_item_bg_normal"
  android:state_pressed="false" android:state_selected="false" android:state_activated="false"/>

  <item android:drawable="@drawable/list_item_touch"
  android:state_pressed="true"/>

 <item android:drawable="@drawable/list_item_touch"
 android:state_pressed="false" android:state_selected="true"/>

 <item android:drawable="@drawable/list_item_bg_pressed"
  android:state_activated="true"/>

</selector>

////////////////////and on ListView

        <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"
        android:divider="#060606"/>

///////////////////////listview Item

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/list_selector">

      <ImageView
      android:id="@+id/icon"
      android:layout_width="35dp"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginLeft="12dp"
      android:layout_marginRight="12dp"
      android:contentDescription="@string/desc_list_item_icon"
      android:src="@drawable/ic_home"
      android:layout_centerVertical="true" />

      </RelativeLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!