bif*_*fko 35 android android-listview
我需要在ListView
选中的一行中突出显示一行(向用户显示他选择的内容),因此,这不是将要选择的那一行,而是他之前选择的那一行.
我已经有了以下位置:
ListView.setSelection(position);
Run Code Online (Sandbox Code Playgroud)
现在我想要的是选择这个特定的行并突出显示它.
onCreate()
活动中包含以下内容的代码ListView
:
public class CountryView extends Activity
{
protected static final String LOG_TAG = null;
/** Called when the activity is first created. */
String[] lv_arr = {};
ListAdapter adapter;
TextView t;
private ListView lvUsers;
private ArrayList<Coun> mListUsers;
String responce=null;
public int d;
int selectedListItem = -1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.country);
Intent data =getIntent();
mListUsers = getCoun();
lvUsers = (ListView) findViewById(R.id.counlistView);
lvUsers.setAdapter(new ListAdapter(this, R.id.counlistView, mListUsers));
selectedListItem=data.getExtras().getInt("PositionInList");
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setOnItemClickListener(new OnItemClickListener()
{
int positionItem;
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Intent pongIntent = new Intent(getApplicationContext(),Trav.class);
int counId=mListUsers.get(position).id;
pongIntent.putExtra("response",mListUsers.get(position).p);
pongIntent.putExtra("responseCounID",counId);
//Put the position of the choose list inside extra
positionItem=position;
pongIntent.putExtra("PositionInListSet",positionItem);
setResult(Activity.RESULT_OK,pongIntent);
Log.i("CounID *******************************"," "+counId);
finish();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
rek*_*eru 37
由于默认情况下ListViews
设置为选择模式NONE
,因此在触摸模式下该setSelection
方法不会产生视觉效果.
为了保持先前的选择/可视显示明确的选择,首先必须适当地设置列表视图的选择模式:
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Run Code Online (Sandbox Code Playgroud)
阅读这些方法的API文档非常有用:
Run Code Online (Sandbox Code Playgroud)void android.widget.AdapterView.setSelection(int position)
设置当前选定的项目.要支持覆盖此方法的可访问性子类,必须首先调用overriden super方法.
参数:
position
要选择的数据项的索引(从0开始).
Run Code Online (Sandbox Code Playgroud)void android.widget.ListView.setChoiceMode(int choiceMode)
定义List的选择行为.默认情况下,列表没有任何选择行为(
CHOICE_MODE_NONE
).通过将choiceMode设置为CHOICE_MODE_SINGLE
,List允许最多一个项目处于选定状态.通过将choiceMode设置为CHOICE_MODE_MULTIPLE
,列表允许选择任意数量的项目.参数:
choiceMode
其中一个CHOICE_MODE_NONE
,CHOICE_MODE_SINGLE
或CHOICE_MODE_MULTIPLE
如果这还不够(假设您希望始终在当前选择旁边显示最后一个选项),则应将最后选择的项目(填充的数据ListAdapter
)存储为lastSelectedItem
,并在适配器的getView
方法中指定不同的背景渲染器的资源,如果它等于此lastSelectedItem
.
如果您的上一次选择不会在选择更改时刷新,则应notifyDataSetChanged
在适配器实例上显式调用该方法.
更新
由于您的活动包含ListView
等待此结果的活动的子项(基于该setResult(Activity.RESULT_OK,pongIntent);
部分),因此最初的想法是正确的,在开始活动时应通过意图传递最后一个位置:
selectedListItem = getIntent().getIntExtra("PositionInList", -1);
lvUsers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvUsers.setSelection(selectedListItem);
Run Code Online (Sandbox Code Playgroud)
的ListView.CHOICE_MODE_SINGLE
,如果你保持在同一活动,但你完成它在每一个itemClick在(选择更改)解决方案将工作,这就是为什么额外的数据应该传递给起点Intent
.
您还可以从适配器设置先前所选项目的背景 - 如上所述 - 覆盖其getView
方法:
lvUsers.setAdapter(new ArrayAdapter(this, R.id.counlistView, groups)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final View renderer = super.getView(position, convertView, parent);
if (position == selectedListItem)
{
//TODO: set the proper selection color here:
renderer.setBackgroundResource(android.R.color.darker_gray);
}
return renderer;
}
});
Run Code Online (Sandbox Code Playgroud)
Dan*_*eón 27
只是:
setChoiceMode
设置背景以支持项目布局中的选择状态,例如:
android:background="?android:attr/activatedBackgroundIndicator"
Run Code Online (Sandbox Code Playgroud)供参考:
Jab*_*ari 15
在布局文件中实现这一点要容易得多,让Android处理剩下的...
1)请确保您有android:choiceMode=""
您设置的ListView布局(singleChoice
,multipleChoice
,等).默认情况下,它设置为none
.
<ListView
android:id="@+id/invite_friends_fragment_contacts_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"/> <!-- THIS LINE -->
Run Code Online (Sandbox Code Playgroud)
2)创建状态选择器XML文件并将其保存在drawables文件夹中.在这个例子中,我们将其命名为state_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/blue" android:state_selected="true"/>
<item android:drawable="@android:color/blue" android:state_activated="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
3)在列表项布局中,添加state_selector.xml文件作为背景:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/state_selector"> <!-- THIS LINE -->
<!-- ALL OF YOUR ELEMENTS WILL GO HERE (TextViews, ImageViews, etc) -->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
如果您正在使用multipleChoice
,则可以相应地覆盖onItemClick()
和设置/取消设置所选项目.Android将更改state_selector.xml文件中指定的背景颜色.
我有一个问题找到一个简单的解决方案,因为这么多的教程和答案包含单选通过单选按钮的信息(严重依赖于RadioGroup).
我的问题是我可以将项目设置为"突出显示"状态,但是在选择下一个项目时无法重置列表.这是我想出的:
listView.setOnItemClickListener(new ItemHighlighterListener ());
Run Code Online (Sandbox Code Playgroud)
有了这个课程:
private class ItemHighlighterListener implements OnItemClickListener{
private View lastSelectedView = null;
public void clearSelection()
{
if(lastSelectedView != null) lastSelectedView.setBackgroundColor(android.R.color.transparent);
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
clearSelection();
lastSelectedView = view;
view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.shape_selected_menu_item));
}
}
Run Code Online (Sandbox Code Playgroud)