亮点列表视图项 [英] highlight listview item

查看:135
本文介绍了亮点列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要突出一个列表视图项,当我触摸它,留突出。我什么都试过,我发现,但毫无效果。这里是我的code:

这是列表视图:

 < ListView控件
    机器人:ID =@ + ID / lvUsers
    机器人:layout_width =300dp
    机器人:layout_height =FILL_PARENT
    机器人:layout_alignParentLeft =真
    机器人:背景=@绘制/ border2
    机器人:choiceMode =singleChoice
    机器人:填充=5DP>
< /&的ListView GT;

@绘制/ border2 就在列表视图的边框。

这是ListView项布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / layoutUsersRow
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@绘制/ list_selector_background
机器人:方向=垂直><的TextView
    机器人:ID =@ + ID / tvAllUsersName
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=10dp
    机器人:文字=用户名
    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?

这是list_selector_background:

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:state_focused =真正的机器人:可绘制=@色/蓝色/> <! - 集中 - >
    <项目的android:state_focused =真正的机器人:STATE_ pressed =真正的机器人:可绘制=@色/蓝色/> <! - 专注和pressed - >
    <项目的android:STATE_ pressed =真正的机器人:可绘制=@色/蓝色/> <! - pressed - >
    <项目机器人:可绘制=@色/透明/> <! - 默认 - >
< /选择>

这是从列表视图适配器code

 公共类UsersAdapter扩展BaseAdapter实现OnClickListener {LoginActivity背景;
私人列表<串GT; listOfUsers;公共UsersAdapter(LoginActivity _context,列表与LT;弦乐> listOfUsers){
    上下文= _context;
    this.listOfUsers = listOfUsers;
}公众诠释的getCount(){
    返回listOfUsers.size();
}公共对象的getItem(INT位置){
    返回listOfUsers.get(位置);
}众长getItemId(INT位置){
    返回的位置;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    字符串项= listOfUsers.get(位置);
    如果(convertView == NULL){
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.users_row,NULL);
    }    TextView的用户名=(的TextView)convertView.findViewById(R.id.tvAllUsersName);
    UserName.setText(输入);    的LinearLayout LinLayout =(的LinearLayout)convertView.findViewById(R.id.layoutUsersRow);
    LinLayout.setFocusableInTouchMode(假);
    LinLayout.setFocusable(假);
    LinLayout.setOnClickListener(本);    返回convertView;
}公共无效的onClick(视图v){
    //获取该行点击的按钮是
    连续的LinearLayout =(LinearLayout中)V;    TextView的孩子=(TextView中)row.getChildAt(0);
    如果(child.getText()。的toString()。等于(管理)){
        意图I =新意图(com.vorteksed.checkinform.ADMINLOGINACTIVITY);
        i.putExtra(verificationFor,ADMIN_LOGIN);
        context.startActivity(ⅰ);
    }其他{
        context.userClicked(child.getText()的toString());        //从列表中选择用户名后焦点的引脚区
        如果(context.currentList!= NULL){
            context.etLoginPin.setVisibility(View.VISIBLE);
            context.tvPin.setVisibility(View.VISIBLE);
            context.etLoginPin.requestFocus();
            context.etLoginPin.setText();
            InputMethodManager IMM =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(context.etLoginPin,0);
        }
    }
}
}

这是行不通的。它只而它的pssed $ P $突出的项目。我只要一放开,亮点是走了。

下面是解

删除的android:背景=@绘制/ list_selector_background从列表视图项目布局行。是不是需要在所有的选择。这里是适配器的code:

 公共类UsersAdapter延伸BaseAdapter {LoginActivity背景;
私人列表<串GT; listOfUsers;
布尔[] arrBgcolor;
私人INT蓝色= Color.BLUE;
私人INT透明= Color.TRANSPARENT;公共UsersAdapter(LoginActivity _context,列表与LT;弦乐> listOfUsers){
    上下文= _context;
    this.listOfUsers = listOfUsers;    arrBgcolor =新的布尔[listOfUsers.size()];
    resetArrbg();}私人无效resetArrbg(){
    的for(int i = 0; I< arrBgcolor.length;我++){
        arrBgcolor [I] = FALSE;
    }
}公众诠释的getCount(){
    返回listOfUsers.size();
}公共对象的getItem(INT位置){
    返回listOfUsers.get(位置);
}众长getItemId(INT位置){
    返回的位置;
}公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
    字符串项= listOfUsers.get(位置);
    如果(convertView == NULL){
        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.users_row,NULL);
    }
    TextView的用户名=(的TextView)convertView.findViewById(R.id.tvAllUsersName);
    UserName.setText(输入);    的LinearLayout LinLayout =(的LinearLayout)convertView.findViewById(R.id.layoutUsersRow);
    LinLayout.setFocusableInTouchMode(假);
    LinLayout.setFocusable(假);
    LinLayout.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            handleClick(V,位置);
        }
    });    如果(arrBgcolor [位置])
        LinLayout.setBackgroundColor(蓝色);
    其他
        LinLayout.setBackgroundColor(透明);    返回convertView;
}公共无效handleClick(视图V,INT位置){
    连续的LinearLayout =(LinearLayout中)V;    resetArrbg();
    arrBgcolor [位置] =真;
    notifyDataSetChanged();    TextView的孩子=(TextView中)row.getChildAt(0);
    如果(child.getText()。的toString()。等于(管理)){
        意图I =新意图(com.vorteksed.checkinform.ADMINLOGINACTIVITY);
        i.putExtra(verificationFor,ADMIN_LOGIN);
        context.startActivity(ⅰ);
    }其他{
        context.userClicked(child.getText()的toString());        //从列表中选择用户名后焦点的引脚区
        如果(context.currentList!= NULL){
            context.etLoginPin.setVisibility(View.VISIBLE);
            context.tvPin.setVisibility(View.VISIBLE);
            context.etLoginPin.requestFocus();
            context.etLoginPin.setText();
            InputMethodManager IMM =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(context.etLoginPin,0);
        }
    }
}}


解决方案

删除选择和尝试以下操作:

修改

 公共类UsersAdapter扩展BaseAdapter实现OnClickListener {        LoginActivity背景;
        私人列表<串GT; listOfUsers;
        布尔[] arrBgcolor;
        私人INT蓝色= Color.BLUE;        公共UsersAdapter(LoginActivity _context,列表与LT;弦乐> listOfUsers){
            上下文= _context;
            this.listOfUsers = listOfUsers;            arrBgcolor =新的布尔[listOfUsers.size()];
            resetArrbg();        }        私人无效resetArrbg(){
            的for(int i = 0; I< arrBgcolor.length;我++){
                arrBgcolor [I] = FALSE;
            }
        }        ......        公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
            字符串项= listOfUsers.get(位置);
            如果(convertView == NULL){
                LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.users_row,NULL);
            }
            TextView的用户名=(的TextView)convertView.findViewById(R.id.tvAllUsersName);
            UserName.setText(输入);            的LinearLayout LinLayout =(的LinearLayout)convertView.findViewById(R.id.layoutUsersRow);
            LinLayout.setFocusableInTouchMode(假);
            LinLayout.setFocusable(假);
            LinLayout.setOnClickListener(本);            //添加以下,位置就是从这里访问
            如果(arrBgcolor [位置]){
                convertView.setBackgroundColor(蓝色);
            }
            convertView.setOnClickListener(新OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    resetArrbg();
                    arrBgcolor [位置] =真;
                    notifyDataSetChanged();
                }
            });
            返回convertView;
        }

I need to highlight a listview item when i touch it and to stay highlighted. I tried everything I found but nothing worked. Here is my code:

This is the listview:

<ListView
    android:id="@+id/lvUsers"
    android:layout_width="300dp"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:background="@drawable/border2"
    android:choiceMode="singleChoice"
    android:padding="5dp" >
</ListView>

The @drawable/border2 is just a border around the listview.

This is the listview item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutUsersRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_selector_background"
android:orientation="vertical" >

<TextView
    android:id="@+id/tvAllUsersName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="User name"
    android:textAppearance="?android:attr/textAppearanceLarge" />

This is list_selector_background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/blue" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/blue" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/blue" /> <!-- pressed -->
    <item android:drawable="@color/transparent" /> <!-- default -->
</selector>

This is the code from the listview adapter

public class UsersAdapter extends BaseAdapter implements OnClickListener {

LoginActivity context;
private List<String> listOfUsers;

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

public int getCount() {
    return listOfUsers.size();
}

public Object getItem(int position) {
    return listOfUsers.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(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);

    return convertView;
}

public void onClick(View v) {
    // get the row the clicked button is in
    LinearLayout row = (LinearLayout) v;

    TextView child = (TextView) row.getChildAt(0);
    if (child.getText().toString().equals("Admin")) {
        Intent i = new Intent("com.vorteksed.checkinform.ADMINLOGINACTIVITY");
        i.putExtra("verificationFor", "ADMIN_LOGIN");
        context.startActivity(i);
    } else {
        context.userClicked(child.getText().toString());

        // focus the pin field after selecting user name from the list
        if (context.currentList != null) {
            context.etLoginPin.setVisibility(View.VISIBLE);
            context.tvPin.setVisibility(View.VISIBLE);
            context.etLoginPin.requestFocus();
            context.etLoginPin.setText("");
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(context.etLoginPin, 0);
        }
    }
}
}

This is not working. It only highlights the item while it's pressed. As soon as i release it, the highlight is gone.

HERE IS THE SOLUTION

Remove the android:background="@drawable/list_selector_background" line from the listview item layout. The selector is not needed at all. And here is the adapter's code:

public class UsersAdapter extends BaseAdapter {

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

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 int getCount() {
    return listOfUsers.size();
}

public Object getItem(int position) {
    return listOfUsers.get(position);
}

public long getItemId(int position) {
    return position;
}

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(new OnClickListener() {
        public void onClick(View v) {
            handleClick(v, position);
        }
    });

    if (arrBgcolor[position])
        LinLayout.setBackgroundColor(blue);
    else 
        LinLayout.setBackgroundColor(transparent);

    return convertView;
}

public void handleClick(View v, int position) {
    LinearLayout row = (LinearLayout) v;

    resetArrbg();
    arrBgcolor[position] = true;
    notifyDataSetChanged();

    TextView child = (TextView) row.getChildAt(0);
    if (child.getText().toString().equals("Admin")) {
        Intent i = new Intent("com.vorteksed.checkinform.ADMINLOGINACTIVITY");
        i.putExtra("verificationFor", "ADMIN_LOGIN");
        context.startActivity(i);
    } else {
        context.userClicked(child.getText().toString());

        // focus the pin field after selecting user name from the list
        if (context.currentList != null) {
            context.etLoginPin.setVisibility(View.VISIBLE);
            context.tvPin.setVisibility(View.VISIBLE);
            context.etLoginPin.requestFocus();
            context.etLoginPin.setText("");
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(context.etLoginPin, 0);
        }
    }
}

}

解决方案

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;
        }

这篇关于亮点列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆