【问题标题】:Listview item with button random selection while scrolling [duplicate]滚动时带有按钮随机选择的Listview项目[重复]
【发布时间】:2013-08-13 12:07:53
【问题描述】:

我在滚动时面临列表视图项的奇怪选择。

初始选择截图(选择第一个条目)

滚动列表视图项目后自动选择为什么? (见下面的截图)

适配器源代码为here

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(CustomizedListView.KEY_TITLE));
    artist.setText(song.get(CustomizedListView.KEY_ARTIST));
    duration.setText(song.get(CustomizedListView.KEY_DURATION));
    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}

在 setonitemclicklistner() 中使按钮可见是问题所在

list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
view.setSelected(true);
view.setBackgroundResource(R.drawable.gradient_bg_hover);   
TextView title;
TextView artist;
title = (TextView)view.findViewById(R.id.title); // title
artist = (TextView)view.findViewById(R.id.artist); // artist
title.setTextColor(getResources().getColor(android.R.color.white));
artist.setTextColor(getResources().getColor(android.R.color.white));

ImageButton btnChild = (ImageButton)view.findViewById(R.id.arrow);
btnChild.setVisibility(View.VISIBLE);

if(lastselected!= null)
{

    title = (TextView)lastselected.findViewById(R.id.title); // title
    artist = (TextView)lastselected.findViewById(R.id.artist); // artist
    title.setTextColor(getResources().getColor(android.R.color.black));
    artist.setTextColor(getResources().getColor(android.R.color.black));

     btnChild = (ImageButton)lastselected.findViewById(R.id.arrow);
    btnChild.setVisibility(View.INVISIBLE);
    lastselected.setBackgroundResource(R.drawable.gradient_bg);
}

lastselected= view;

图像按钮可见后,getview 会为下一个显示项目回收相同的视图。我不知道如何解决这个问题。

【问题讨论】:

  • 请出示相关代码,否则无人帮助。
  • 您需要哪部分代码?请告诉我。
  • @ankur:我的问题不在于禁用颜色。它是为了避免在滚动时自动选择列表项。
  • 我确实读过那篇文章。 listview 行为与我上面发布的内容不同。

标签: android listview


【解决方案1】:

您可以使用 onScrollListener 来检测列表何时滚动并防止选择项目。

看看这里(click)

onScroll() 

滚动列表或网格时调用的回调方法。这将在滚动完成后调用

onScrollStateChanged()

在滚动列表视图或网格视图时调用的回调方法。如果视图正在滚动,该方法将在滚动的下一帧渲染之前调用。

【讨论】:

  • 它的滚动监听器。这工作正常..问题是当列表项被自动选择时......这与滚动无关。
  • 抱歉,我误解了我想的问题。
【解决方案2】:

这是适配器getView() 的常见缺陷。当视图被回收时(convertView 不为空),您需要将回收的视图重置为其初始状态。在这种情况下,重置回收列表行的选中/选中状态。

【讨论】:

  • 是的。我像 'if(isSelected[position]== true) { vi.setSelected(true); } 其他 { vi.setSelected(false); }' 但还是同样的问题
  • 你是对的!当用户选择列表项时,列表有一个不可见的按钮我使按钮在 setOnItemClickListener() 中可见,并且此更改正在 getview 中回收。任何线索如何进行?
【解决方案3】:

试试这个:

Selected Item issue while scrolling listview

由于延迟加载,无法保证视图的状态。如果您存储一个额外的布尔值,或者在您的情况下将另一对字符串存储到您的 HashMap 中(如果更容易)来标记是否选择了歌曲,则会更加精确。实现 ViewHolders 也不会受到伤害。希望这会有所帮助。

【讨论】:

    【解决方案4】:

    使用

    listview.setSelector(drawable)
    

    如果您想更改选择器或禁用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      相关资源
      最近更新 更多