Jea*_*Roy 36 android listview highlighting android-listview
因此,我有一个包含2 ListView个小部件的活动,当您在第一个中选择一个值时,第二个将填充与第一个中的选择相关的值ListView.这个机制没有问题,但现在我希望用户选择保持突出显示.我已经阅读了与此主题相关的一个很好的问题,似乎有很多方法可以实现这一点但是在尝试了4-5之后,我仍然无法让它工作.
我已经ListView使用android:listSelector="#CCCCCC"XML属性在第二个工作了,但是一旦OnItemClickListener引入混合(这就像我第一次使用的那个ListView),这似乎被擦干净了.
到目前为止,这是我得到的:
自定义OnItemClickListener我发现浏览关于这个主题的各种答案(稍微修改它,以便它加载我的信息第二个ListView):
private class ItemHighlighterListener implements OnItemClickListener {
private View oldSelection = null;
public void clearSelection() {
if(oldSelection != null) {
oldSelection.setBackgroundColor(android.R.color.transparent);
}
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
clearSelection();
oldSelection = view;
view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.list_selector));
loadClubs(mXMLPortalOptions.getRegion(pos).getId());
mClubList.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_white, mClubs));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的list_selector.xml档案:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"><shape>
<solid android:color="#CCCCCC" />
</shape></item>
<item android:state_selected="false"><shape>
<solid android:color="#FFFFFF" />
</shape></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
方法(OnItemClick)被调用并执行,但我的背景ListItem保持相同的颜色:/
我无法相信这个简单的任务已经证明是如此复杂.
如果我省略了可能有用的代码,或者我的问题缺乏细节,请随时指出,我会尽力解释自己.
Sha*_*ful 62
为所选项目放置一个位置变量.改变onItemClicked()方法中的位置.检查列表适配器内的选定位置,getView()并设置所选项目的背景.
public class TestAdapter extends BaseAdapter
{
private Context context;
private ArrayList<TestList> testList;
private int selectedIndex;
private int selectedColor = Color.parseColor("#1b1b1b");
public TestAdapter(Context ctx, ArrayList<TestList> testList)
{
this.context = ctx;
this.testList = testList;
selectedIndex = -1;
}
public void setSelectedIndex(int ind)
{
selectedIndex = ind;
notifyDataSetChanged();
}
@Override
public int getCount()
{
return testList.size();
}
@Override
public Object getItem(int position)
{
return testList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
private class ViewHolder
{
TextView tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
ViewHolder holder;
if(convertView == null)
{
vi = LayoutInflater.from(context).inflate(R.layout.test_list_item, null);
holder = new ViewHolder();
holder.tv = (TextView) vi;
vi.setTag(holder);
}
else
{
holder = (ViewHolder) vi.getTag();
}
if(selectedIndex!= -1 && position == selectedIndex)
{
holder.tv.setBackgroundColor(Color.BLACK);
}
else
{
holder.tv.setBackgroundColor(selectedColor);
}
holder.tv.setText("" + (position + 1) + " " + testList.get(position).getTestText());
return vi;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在单击列表项时设置selectedIndex变量.
public class TestActivity extends Activity implements OnItemClickListener
{
// Implemented onItemClickListener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
adapter.setSelectedIndex(position);
}
}
Run Code Online (Sandbox Code Playgroud)
为了扩展Shaiful的伟大解决方案,你可能无法让他在你的情况下工作.
如果您使用的是全部代码public void onListItemClick(ListView l, View v, int index, long id),如果您正在使用片段并且必须声明接口而不是实现OnListItemClickListener,或者导致IDE生成错误的任何内容,则可能必须静态访问变量和方法.
public static int selectedPosition = 0;
ArrayAdapter<Your_obj> adapter = null;
@Override
public void onListItemClick(ListView l, View v, int index, long id) {
super.onListItemClick(l, v, index, id);
selectedPosition = index;
Your_adapter.setSelectedIndex(selectedPosition);
adapter.notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)
在Your_adapter中:
private static int selectedIndex;
//public Your_adapter...
public static void setSelectedIndex(int ind) {
selectedIndex = ind;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
WellHolder holder = null;
if (null == convertView) {
//set up your "holder"
}
if (position == selectedIndex) {
convertView.setBackgroundColor(convertView.getResources().getColor(R.color.cyan));
}
else {
convertView.setBackgroundColor(convertView.getResources().getColor(R.color.silver));
}
return convertView;
}
Run Code Online (Sandbox Code Playgroud)
其他一些区别是您不必将任何变量初始化为"0"或"-1",并在您的活动中调用notifyDataSetChanged().
再次感谢您的解决方案@Shaiful.它确实帮我节省了时间,试图让iOS中的默认设置适用于Android,同时避免选择器/项目/聚焦/按下/等.
| 归档时间: |
|
| 查看次数: |
43266 次 |
| 最近记录: |