I have an XML
with two ListView
, one with a list of clients filled by a select query (lv_cli
) and the other with the details of the client selected (lv_cli_det
). I would like to keep the client selected in the lv_cli
while the lv_cli_det
show the details.
我有一个带有两个ListView的XML,一个带有由select查询(lv_cli)填充的客户列表,另一个带有所选客户的详细信息(lv_cli_det)。我希望在lv_cli中保持选中的客户端,而lv_cli_det则显示详细信息。
XML:
XML:
<ListView
android:id="@+id/cli_lista"
android:layout_width="512dp"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
>
</ListView>
<ListView
android:id="@+id/cli_lista_det"
android:layout_width="512dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/cli_lista"
android:fadeScrollbars="false" >
</ListView>
Java:
Java:
Cursor cursor = db.rawQuery("Select NrCl||';'||Nome From Clientes", null);
final ListView t = (ListView)findViewById(R.id.cli_lista);
ArrayAdapter<String> myarrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listItems);
t.setAdapter(myarrayAdapter);
final ListView td = (ListView)findViewById(R.id.cli_lista_detalhe);
final ArrayAdapter<String> myarrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listItems2);
t.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView)view).getText().toString();
String[] strArray = item.split("\\;");
cli.load(strArray[0].toString());
td.setAdapter(myarrayAdapter2);
listItems2.clear();
listItems2.add("Nome: " + cli.getNome());
listItems2.add("Morada: " + cli.getMorada());
listItems2.add("Localidade: " + cli.getLoca());
listItems2.add("Código Postal: " + cli.getCp());
listItems2.add("Pais: " + cli.getPais());
listItems2.add("Nif: " + cli.getNif());
listItems2.add("Tel: " + cli.getTel());
listItems2.add("Tlm: " + cli.getTlm());
listItems2.add("Tipo Preço: " + cli.getTipoPvn());
listItems2.add("Cond. Pagamento: " + cli.getCpg());
listItems2.add("Obs: " + cli.getObs());
td.setAdapter(myarrayAdapter2);
myarrayAdapter2.notifyDataSetChanged();
}
});
9 个解决方案
#1
124
To hold the color of listview item when you press it, include the following line in your listview item layout:
要保持列表视图项目的颜色,请在列表视图项目布局中包含以下行:
android:background="@drawable/bg_key"
Then define bg_key.xml
in drawable
folder like this:
然后定义bg_key。可绘制文件夹中的xml如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
Finally, include this in your ListView
onClickListener
:
最后,在ListView onClickListener中包含以下内容:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
... // Anything
}
});
This way, only one item will be color-selected at any time. You can define your color values in res/values/colors.xml
with something like this:
这样,在任何时候只有一个项目会被颜色选择。你可以用res/values/colors来定义你的颜色值。xml有如下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed_color">#4d90fe</color>
<color name="default_color">#ffffff</color>
</resources>
#2
147
Found the proper way. It's very simple. In resource describe following:
找到合适的方法。很简单。资源描述:
android:choiceMode="singleChoice"
android:listSelector="#666666"
(or you may specify a resource link instead of color value)
(或者您可以指定一个资源链接而不是颜色值)
Programmatical:
Programmatical:
listView.setSelector(Drawable selector)
listView.setSelector(int resourceId)
listView.setChoiceMode(int mode)
mode can be one of these: AbsListView.CHOICE_MODE_SINGLE, AbsListView.CHOICE_MODE_MULTIPLE, AbsListView.CHOICE_MODE_NONE (default)
模式可以是其中之一:AbsListView。CHOICE_MODE_SINGLE AbsListView。CHOICE_MODE_MULTIPLE AbsListView。CHOICE_MODE_NONE(默认)
(AbsListView is the abstract ancestor for the ListView class)
(AbsListView是ListView类的抽象祖先)
P.S. manipulations with onItemClick and changing view background are bankrupt, because a view itself is a temporary object. Hence you must not to track a view.
使用onItemClick和更改视图背景的P.S.操作会失败,因为视图本身是一个临时对象。因此,您不能跟踪视图。
If our list is long enough, the views associated with scrolled out items will be removed from hierarchy, and will be recreated when those items will shown again (with cached display options, such as background). So, the view we have tracked is now not an actual view of the item, and changing its background does nothing to the actual item view. As a result we have multiple items selected.
如果我们的列表足够长,那么与scrollout项相关的视图将从层次结构中删除,当这些项再次显示时,将重新创建(使用缓存的显示选项,比如后台)。因此,我们跟踪的视图现在不是项目的实际视图,更改其背景对实际的项目视图没有任何影响。因此,我们选择了多个项目。
#3
34
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
for (int j = 0; j < adapterView.getChildCount(); j++)
adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
// change the background color of the selected element
view.setBackgroundColor(Color.LTGRAY);
});
Perhaps you might want to save the current selected element in a global variable using the index i.
也许您可能希望使用索引i将当前选中的元素保存到全局变量中。
#4
9
Simplistic way is,if you are using listview in a xml,use this attributes on your listview,
简单的方法是,如果您在xml中使用listview,请在listview中使用这些属性,
android:choiceMode="singleChoice"
android:listSelector="#your color code"
if not using xml,by programatically
如果不使用xml,则按编程方式使用。
listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
listview.setSelector(android.R.color.holo_blue_light);
#5
3
You need selector like this:
你需要这样的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- State when a row is being pressed, but hasn't yet been activated (finger down) -->
<item android:drawable="@color/app_primary_color_light" android:state_pressed="true" />
<!-- Used when the view is "activated". -->
<item android:drawable="@color/app_primary_color" android:state_activated="true" />
<!-- Default, "just hangin' out" state. -->
<item android:drawable="@android:color/transparent" /></selector>
And then set android:choiceMode="singleChoice"
to your ListView.
然后将android:choiceMode="singleChoice"设为ListView。
#6
2
From Avinash Kumar Pankaj's example
来自Avinash Kumar Pankaj的例子
View v;
then at oncreate method
在oncreate方法
v = new View(getActivity());
and then onlistitemclick method i wrote
然后是onlistitemclick方法
public void onListItemClick(ListView listView, View view, int position,
long id) {
v.setBackgroundResource(0);
view.setBackgroundResource(R.color.green);
v = view;
}
It worked for me. Thank you.
它为我工作。谢谢你!
I replaced
我更换了
v = new View(getActivity());
to
来
v = new View(this);
and the code worked well.
代码运行良好。
It is necessary the xml files 'colors' and 'bg_key' from previous examples too, as well as ListView attribute android:background="@drawable/bg_key"
前面的例子中也需要xml文件'colors'和'bg_key'以及ListView属性android:background="@drawable/bg_key"
Mauro
毛罗。
#7
2
*please be sure there is no Ripple at your root layout of list view container
*请确保列表视图容器的根布局没有涟漪
add this line to your list view
将这一行添加到列表视图中。
android:listSelector="@drawable/background_listview"
here is the "background_listview.xml" file
这是“background_listview。xml”文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white_background" android:state_pressed="true" />
<item android:drawable="@color/primary_color" android:state_focused="false" /></selector>
the colors that used in the background_listview.xml file :
在background_listview中使用的颜色。xml文件:
<color name="primary_color">#cc7e00</color>
<color name="white_background">#ffffffff</color>
after these
在这些
(clicked item contain orange color until you click another item)
(单击的项目包含橙色,直到单击另一个项目)
#8
1
One way you can do this, is to Keep track of the current selected position in your activity:
你可以这样做的一个方法是,跟踪当前选定的位置在你的活动:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
currentPosition = position
lv_cli.notifyDataSetChanged();
}
Now, be sure you assign an ID to the parent layout (linearLayout, boxLayout, relativeLayout, .. Whatever you prefer) of your list item.
现在,请确保您为父布局(linearLayout, boxLayout, relativeLayout, .)分配了一个ID。无论你喜欢什么)你的列表项目。
Then in your ListView you can do something Like this:
在ListView中你可以这样做:
layoutBackground = (LinearLayout) convertView.findViewById(R.id.layout_background);
if (YourActivity.this.currentPosition == position) {
layoutBackground.setBackgroundColor(YourActivity.this.getResources().getColor(R.color.hilight_color));
} else{
layoutBackground.setBackgroundResource(R.drawable.list_item_drawable);
}
Basically, you just set the hilight color to the layout as a background when it equals your current selected position. Notice how I set a drawable background resource when the item is not selected. This could be in your case different (since you posted no code). In my case, this drawable is a selector which makes sure the item is hi-lighted when pressed.
基本上,当它等于你当前选择的位置时,你只需将hilight颜色设置为布局的背景。注意,当没有选择项时,我如何设置可绘制的背景资源。这可能与您的情况不同(因为您没有发布代码)。在我的例子中,这个可绘图器是一个选择器,它确保在按下时项目是高亮度的。
NOTE: This simple code doesn't use a view-holder, but I really recommend using one.
注意:这个简单的代码不使用viewholder,但是我确实建议使用它。
#9
0
Use the id instead:
使用id而不是:
This is the easiest method that can handle even if the list is long:
即使列表很长,这也是最容易处理的方法:
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
//Handle your items.
//StringHolder.mSelectedItem is a public static variable.
if(getItemId(position)==StringHolder.mSelectedItem){
rowView.setBackgroundColor(Color.LTGRAY);
}else{
rowView.setBackgroundColor(Color.TRANSPARENT);
}
return rowView;
}
And then in your onclicklistener:
然后在onclicklistener中:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
StringHolder.mSelectedItem = catagoryAdapter.getItemId(i-1);
catagoryAdapter.notifyDataSetChanged();
.....
#1
124
To hold the color of listview item when you press it, include the following line in your listview item layout:
要保持列表视图项目的颜色,请在列表视图项目布局中包含以下行:
android:background="@drawable/bg_key"
Then define bg_key.xml
in drawable
folder like this:
然后定义bg_key。可绘制文件夹中的xml如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
Finally, include this in your ListView
onClickListener
:
最后,在ListView onClickListener中包含以下内容:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
... // Anything
}
});
This way, only one item will be color-selected at any time. You can define your color values in res/values/colors.xml
with something like this:
这样,在任何时候只有一个项目会被颜色选择。你可以用res/values/colors来定义你的颜色值。xml有如下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed_color">#4d90fe</color>
<color name="default_color">#ffffff</color>
</resources>
#2
147
Found the proper way. It's very simple. In resource describe following:
找到合适的方法。很简单。资源描述:
android:choiceMode="singleChoice"
android:listSelector="#666666"
(or you may specify a resource link instead of color value)
(或者您可以指定一个资源链接而不是颜色值)
Programmatical:
Programmatical:
listView.setSelector(Drawable selector)
listView.setSelector(int resourceId)
listView.setChoiceMode(int mode)
mode can be one of these: AbsListView.CHOICE_MODE_SINGLE, AbsListView.CHOICE_MODE_MULTIPLE, AbsListView.CHOICE_MODE_NONE (default)
模式可以是其中之一:AbsListView。CHOICE_MODE_SINGLE AbsListView。CHOICE_MODE_MULTIPLE AbsListView。CHOICE_MODE_NONE(默认)
(AbsListView is the abstract ancestor for the ListView class)
(AbsListView是ListView类的抽象祖先)
P.S. manipulations with onItemClick and changing view background are bankrupt, because a view itself is a temporary object. Hence you must not to track a view.
使用onItemClick和更改视图背景的P.S.操作会失败,因为视图本身是一个临时对象。因此,您不能跟踪视图。
If our list is long enough, the views associated with scrolled out items will be removed from hierarchy, and will be recreated when those items will shown again (with cached display options, such as background). So, the view we have tracked is now not an actual view of the item, and changing its background does nothing to the actual item view. As a result we have multiple items selected.
如果我们的列表足够长,那么与scrollout项相关的视图将从层次结构中删除,当这些项再次显示时,将重新创建(使用缓存的显示选项,比如后台)。因此,我们跟踪的视图现在不是项目的实际视图,更改其背景对实际的项目视图没有任何影响。因此,我们选择了多个项目。
#3
34
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
for (int j = 0; j < adapterView.getChildCount(); j++)
adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
// change the background color of the selected element
view.setBackgroundColor(Color.LTGRAY);
});
Perhaps you might want to save the current selected element in a global variable using the index i.
也许您可能希望使用索引i将当前选中的元素保存到全局变量中。
#4
9
Simplistic way is,if you are using listview in a xml,use this attributes on your listview,
简单的方法是,如果您在xml中使用listview,请在listview中使用这些属性,
android:choiceMode="singleChoice"
android:listSelector="#your color code"
if not using xml,by programatically
如果不使用xml,则按编程方式使用。
listview.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
listview.setSelector(android.R.color.holo_blue_light);
#5
3
You need selector like this:
你需要这样的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- State when a row is being pressed, but hasn't yet been activated (finger down) -->
<item android:drawable="@color/app_primary_color_light" android:state_pressed="true" />
<!-- Used when the view is "activated". -->
<item android:drawable="@color/app_primary_color" android:state_activated="true" />
<!-- Default, "just hangin' out" state. -->
<item android:drawable="@android:color/transparent" /></selector>
And then set android:choiceMode="singleChoice"
to your ListView.
然后将android:choiceMode="singleChoice"设为ListView。
#6
2
From Avinash Kumar Pankaj's example
来自Avinash Kumar Pankaj的例子
View v;
then at oncreate method
在oncreate方法
v = new View(getActivity());
and then onlistitemclick method i wrote
然后是onlistitemclick方法
public void onListItemClick(ListView listView, View view, int position,
long id) {
v.setBackgroundResource(0);
view.setBackgroundResource(R.color.green);
v = view;
}
It worked for me. Thank you.
它为我工作。谢谢你!
I replaced
我更换了
v = new View(getActivity());
to
来
v = new View(this);
and the code worked well.
代码运行良好。
It is necessary the xml files 'colors' and 'bg_key' from previous examples too, as well as ListView attribute android:background="@drawable/bg_key"
前面的例子中也需要xml文件'colors'和'bg_key'以及ListView属性android:background="@drawable/bg_key"
Mauro
毛罗。
#7
2
*please be sure there is no Ripple at your root layout of list view container
*请确保列表视图容器的根布局没有涟漪
add this line to your list view
将这一行添加到列表视图中。
android:listSelector="@drawable/background_listview"
here is the "background_listview.xml" file
这是“background_listview。xml”文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white_background" android:state_pressed="true" />
<item android:drawable="@color/primary_color" android:state_focused="false" /></selector>
the colors that used in the background_listview.xml file :
在background_listview中使用的颜色。xml文件:
<color name="primary_color">#cc7e00</color>
<color name="white_background">#ffffffff</color>
after these
在这些
(clicked item contain orange color until you click another item)
(单击的项目包含橙色,直到单击另一个项目)
#8
1
One way you can do this, is to Keep track of the current selected position in your activity:
你可以这样做的一个方法是,跟踪当前选定的位置在你的活动:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
currentPosition = position
lv_cli.notifyDataSetChanged();
}
Now, be sure you assign an ID to the parent layout (linearLayout, boxLayout, relativeLayout, .. Whatever you prefer) of your list item.
现在,请确保您为父布局(linearLayout, boxLayout, relativeLayout, .)分配了一个ID。无论你喜欢什么)你的列表项目。
Then in your ListView you can do something Like this:
在ListView中你可以这样做:
layoutBackground = (LinearLayout) convertView.findViewById(R.id.layout_background);
if (YourActivity.this.currentPosition == position) {
layoutBackground.setBackgroundColor(YourActivity.this.getResources().getColor(R.color.hilight_color));
} else{
layoutBackground.setBackgroundResource(R.drawable.list_item_drawable);
}
Basically, you just set the hilight color to the layout as a background when it equals your current selected position. Notice how I set a drawable background resource when the item is not selected. This could be in your case different (since you posted no code). In my case, this drawable is a selector which makes sure the item is hi-lighted when pressed.
基本上,当它等于你当前选择的位置时,你只需将hilight颜色设置为布局的背景。注意,当没有选择项时,我如何设置可绘制的背景资源。这可能与您的情况不同(因为您没有发布代码)。在我的例子中,这个可绘图器是一个选择器,它确保在按下时项目是高亮度的。
NOTE: This simple code doesn't use a view-holder, but I really recommend using one.
注意:这个简单的代码不使用viewholder,但是我确实建议使用它。
#9
0
Use the id instead:
使用id而不是:
This is the easiest method that can handle even if the list is long:
即使列表很长,这也是最容易处理的方法:
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
//Handle your items.
//StringHolder.mSelectedItem is a public static variable.
if(getItemId(position)==StringHolder.mSelectedItem){
rowView.setBackgroundColor(Color.LTGRAY);
}else{
rowView.setBackgroundColor(Color.TRANSPARENT);
}
return rowView;
}
And then in your onclicklistener:
然后在onclicklistener中:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
StringHolder.mSelectedItem = catagoryAdapter.getItemId(i-1);
catagoryAdapter.notifyDataSetChanged();
.....