突出显示列表视图项
Posted
技术标签:
【中文标题】突出显示列表视图项【英文标题】:highlight listview item 【发布时间】:2012-11-21 21:31:20 【问题描述】:当我触摸它并保持突出显示时,我需要突出显示一个列表视图项。我尝试了我发现的一切,但没有任何效果。这是我的代码:
这是列表视图:
<ListView
android:id="@+id/lvUsers"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:background="@drawable/border2"
android:choiceMode="singleChoice"
android:padding="5dp" >
</ListView>
@drawable/border2
只是列表视图的边框。
这是列表视图项布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutUsersRow"
android:layout_
android:layout_
android:background="@drawable/list_selector_background"
android:orientation="vertical" >
<TextView
android:id="@+id/tvAllUsersName"
android:layout_
android:layout_
android:padding="10dp"
android:text="User name"
android:textAppearance="?android:attr/textAppearanceLarge" />
这是 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>
这是来自列表视图适配器的代码
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);
这不起作用。它仅在按下时突出显示该项目。一放开,亮点就消失了。
解决方案在这里
从列表视图项布局中删除android:background="@drawable/list_selector_background"
行。根本不需要选择器。这是适配器的代码:
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);
【问题讨论】:
查看此链接。它可能会有所帮助:***.com/questions/8565999/… 已经看过了。没有帮助。 【参考方案1】:删除选择器并尝试以下操作:
编辑
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;
【讨论】:
这提供了多项选择。而且当我滚动列表视图时,选择会在几个项目上重复,尽管我只选择了一个。 选择重复,因为您没有处理 convertView 背景。使用 ArrayList 可以解决问题,因为您正在逐个检查每个位置。 @mango 我处理了多选,但是滚动时重复选择呢? 什么意思?你的意思是选择发生在不同的位置?试试上面编辑的代码。 这就是我所说的:如果我单击第一个项目,它会突出显示它。但是,如果我向下滚动,则会突出显示更多项目。为什么会这样?【参考方案2】:////@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>
/////////////////在ListView上
<ListView
android:id="@+id/list_slidermenu"
android:layout_
android:layout_
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"
android:divider="#060606"/>
///////////////////列表视图项
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="@drawable/list_selector">
<ImageView
android:id="@+id/icon"
android:layout_
android:layout_
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>
【讨论】:
以上是关于突出显示列表视图项的主要内容,如果未能解决你的问题,请参考以下文章
如何使用在列表项单击时动态生成的文本视图标签突出显示我在列表视图中的当前位置