【问题标题】:Unable to the the Image to the desired position in android's custom listview无法将图像放到 android 的自定义列表视图中的所需位置
【发布时间】:2015-05-15 07:13:39
【问题描述】:

最近,我设法在 android 中创建了一个自定义列表视图,用于在列表中显示图像。但是,我有一个无法解决的问题。 我尝试做的非常简单,1. 通过 web.xml 向与 json 数据具有相同日期的列表项显示闪烁的图标。 2. 显示列表视图的第 2 和第 3 位置的图像。 3. 其余列表项将不显示任何图像。

我在自定义适配器的 getView 方法中有一个简单的代码,它实际上将当前日期与通过 web.xml 从 json 数据中检索到的日期进行比较。我在下面设置了我正在使用的功能。

如果当前日期等于 json 数据中的日期,它将显示一个闪烁的图像(出于测试目的,我使用的是 2015/05/14)。如果位置等于第二或第三位置,则显示一个图标。否则什么都不显示。 但是,一些图像显示在列表视图的随机位置。我的代码中有错误吗?不符合条件的图标怎么可能不显示?

if (days.get(position).trim().toString().equals("2015/05/14")||days.get(position).trim().toString()=="2015/05/14") {

    holder.left.setBackgroundResource(R.drawable.blinker);
    AnimationDrawable frameAnimation = (AnimationDrawable) holder.left
            .getBackground();

    // Start the animation (looped playback by default).
    frameAnimation.start();

}else{
     holder.left.setImageResource(android.R.color.transparent);
}

if(position==1||position==2){

    holder.left.setImageResource(R.drawable.new1);

}else{
     holder.left.setImageResource(android.R.color.transparent);
}

if(position==getCount()-1){
     holder.left.setImageResource(android.R.color.transparent);
}



return convertView;
}

【问题讨论】:

    标签: android json listview android-listview


    【解决方案1】:
    if (days.get(position).trim().toString().equals("2015/05/14")||days.get(position).trim().toString()=="2015/05/14") {
    
        holder.left.setBackgroundResource(R.drawable.blinker);
        AnimationDrawable frameAnimation = (AnimationDrawable) holder.left
            .getBackground();
    
        // Start the animation (looped playback by default).
        frameAnimation.start();
    
        // set variable with index of blinking row
        mBlinkingIndex = position;
    
    
    
       }else{
         holder.left.setImageResource(android.R.color.transparent);
       }
    

    在后续调用getView()时,我们使用mBlinkingIndex来判断。

    因为我们需要设置第二行和第三行drawable new1

        if((position = (mBlinkingIndex+1) || (position == (mBlinkingIndex+2)){
            // this row is special
            holder.left.setImageResource(R.drawable.new1);
    
        }else{
            // this row is not special
             holder.left.setImageResource(android.R.color.transparent);
        }
    

    所以,getView() 看起来像:

        if (days.get(position).trim().toString().equals("2015/05/14")||days.get(position).trim().toString()=="2015/05/14") {
    
        holder.left.setBackgroundResource(R.drawable.blinker);
        AnimationDrawable frameAnimation = (AnimationDrawable) holder.left
            .getBackground();
    
        // Start the animation (looped playback by default).
        frameAnimation.start();
    
        // set variable with index of blinking row
        mBlinkingIndex = position;
    
    
    
       }else{
         holder.left.setImageResource(android.R.color.transparent);
       }
    
       if((position = (mBlinkingIndex+1) || (position == (mBlinkingIndex+2)){
            // this row is special
            holder.left.setImageResource(R.drawable.new1);
    
        }else{
            // this row is not special
             holder.left.setImageResource(android.R.color.transparent);
        }
    

    【讨论】:

    • 谢谢库沙尔!当我有时间时,我会测试你的方法!我会告诉你结果!
    • @user3867627 你检查过上面的解决方案了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多