遇到设置列表视图项目颜色的问题
我的列表中包含有两个文本视图和一个图像视图的项目。我使用 ArrayAdapter 来膨胀列表。除了单击时更改列表项颜色之外,一切正常。我的列表视图中有 22 个项目。主要是列表视图在屏幕上显示 10 个项目,并滚动其他项目。现在我的问题是,当我单击 0-9(初始 10 个项目)之间的单个项目时,项目会在单击时正确更改其颜色,但是当我滚动并单击时在位置大于 9 的项目(在最初的 10 个项目之后)上,我的活动崩溃了。我指的是 http://www.mail-archive.com/[email protected]/msg09740.html链接在for循环
中编写代码。帮助我解决这个问题。任何建议或解决方案将受到高度赞赏。提前致谢。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // to hide the virtual keyboard
setContentView(R.layout.defect_pic_listview);
try{
adapter = new MyArrayAdapter(this,makeList());
setListAdapter(adapter);
adapter.notifyDataSetChanged();
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
// Toast.LENGTH_SHORT).show();
System.out.println("position"+position);
int first = getListView().getFirstVisiblePosition();
System.out.println("first="+first);
int last = getListView().getLastVisiblePosition();
System.out.println("last="+last);
int total = last - first;
System.out.println("total="+total);
if(getListView().isItemChecked(position)){
for(int i = 0 ; i <= last ; i++){
System.out.println("i="+i);
if(first+i == position){
getListView().getChildAt(i).setBackgroundColor(Color.GREEN);
System.out.println("l1="+getListView());
// l.getItemAtPosition(i);
// System.out.println("l position"+l);
}
else{
getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
System.out.println("l2="+getListView());
}
}
}
else{
getListView().getChildAt(position - first).setBackgroundColor(Color.TRANSPARENT);
}
}
});
}
catch(Exception e){
Log.d("error",e.getMessage());
}
}
I have list with items having two textview and one imageview.I inflate the list with ArrayAdapter.Everything is working fine except changing list item color on click.I have 22 items in my listview. Primarily listview displaying 10 items on the screen and get the other items on scroll.Now my problem is when I clicks on a single item between 0-9(initial 10 items) item changes their color properly on click, but when i scroll and clicks on an item having position greater then 9(after the initial 10 items) my activity crashes.I am referring http://www.mail-archive.com/[email protected]/msg09740.html link to write the code in for loop
.Help me to get rid of this problem.Any suggestions or solutions will be highly appreciated.Thanx in advance.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // to hide the virtual keyboard
setContentView(R.layout.defect_pic_listview);
try{
adapter = new MyArrayAdapter(this,makeList());
setListAdapter(adapter);
adapter.notifyDataSetChanged();
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
// Toast.LENGTH_SHORT).show();
System.out.println("position"+position);
int first = getListView().getFirstVisiblePosition();
System.out.println("first="+first);
int last = getListView().getLastVisiblePosition();
System.out.println("last="+last);
int total = last - first;
System.out.println("total="+total);
if(getListView().isItemChecked(position)){
for(int i = 0 ; i <= last ; i++){
System.out.println("i="+i);
if(first+i == position){
getListView().getChildAt(i).setBackgroundColor(Color.GREEN);
System.out.println("l1="+getListView());
// l.getItemAtPosition(i);
// System.out.println("l position"+l);
}
else{
getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
System.out.println("l2="+getListView());
}
}
}
else{
getListView().getChildAt(position - first).setBackgroundColor(Color.TRANSPARENT);
}
}
});
}
catch(Exception e){
Log.d("error",e.getMessage());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我正在回答我自己的问题。
这是完美的运行代码:-
I am giving the answer to my own question.
Here's the perfect running code:-
我成功使用的一个更干净(?)的解决方案是创建一个 LinearLayout 的扩展小部件(或用于列表项布局的任何根视图类型),它实现 可检查。
然后在您的项目布局 xml 中:
A cleaner(?) solution I've successfully used is to create an extension widget of LinearLayout (or whatever root view type you use for your listitem layout) that implements Checkable.
Then in your item layout xml:
将此代码用于 for 循环。
我认为您的意图只是更改单击项目的背景颜色。为此,您不需要添加 for 循环。如果您的意图与我所说的相同,则仅在 for 循环的位置使用此代码。
Use this code for the for loop.
I think your intention to only change the bacground color of clicked item. For that you not need to add for loop. if your intention is same as i said then use this code only at the place of for loop.
还有其他方法可以做。
首先你需要添加一个选择器xml,例如listviewitem_bg.xml
然后将其设置为列表视图单元格的背景。
There is other way to do.
First you need to add a selector xml e.g. listviewitem_bg.xml
and then set it as a background of your list view cell.
您可以这样尝试:
这会给您带来如下行为:
You can try like this:
This will give you behavior like:
执行类似的操作
你可以在 oncreate 之外
,然后在 oncreate 中执行
you can do something like this
@ outside of on create
then in oncreate