【问题标题】:Change Background Image of button when scrolling in android在android中滚动时更改按钮的背景图像
【发布时间】:2023-07-11 21:23:01
【问题描述】:

很抱歉没有发布任何代码。 在 * 和 google 上搜索,但找不到合适的解决方案,如果您对此有任何想法或技巧,请您帮帮我。

要求:

我有许多按钮根据 web 的要求以编程方式创建。并将所有按钮的背景图像设置为灰度。我已经完成了这部分,但是当滚动水平 scollview 时,我想更改该按钮(滚动时可见)背景图像有色(初始加载灰度)和某些时间让我们假设 10 秒按钮背景设置初始状态(灰度) .

主题:

在可见视图上滑动时更改按钮背景图像 按钮。一定的时间间隔(10秒)。

`

问题:

我无法让当前按钮在水平视图中可见,并且无法更改该按钮的背景颜色图像(第一次加载视图)。我只想以编程方式进行,因为所有图像都来自服务器端。

任何想法,如何完成这项工作或对此任务的任何参考。

注意: 我创建 button 视图并设置图像背景并在 linearlayout 上添加图像并添加其他线性布局 mainlinearlayout 并添加 mainlinearlayout在 horizontalview 布局上。

我的代码截图

public class CustomAdWithTitle extends LinearLayout{
private LinearLayout LinearCollectionAds;
private List<CommericalClassifiedAds> listCommericalAds;

        HorizontalScrollView  commercialH = new HorizontalScrollView(mcontext);
        commercialH.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        LinearLayout    commercialL = new LinearLayout(mcontext);
        commercialL.setOrientation(LinearLayout.HORIZONTAL);
        commercialL.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    LinearLayout    LinearCollectionAds= new LinearLayout(mcontext);
    LinearCollectionAds.setOrientation(LinearLayout.VERTICAL);
    LinearCollectionAds.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
for (int i = 0; i < listCommericalAds.size(); i++) {
                try {
                    commercialL.addView(listCommericalAds.get(i));
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }

            }

            commercialH.addView(commercialL);
            LinearCollectionAds.addView(commercialH);
   this.addView(LinearCollectionAds);
}

.......

它可以返回带有按钮视图的相对布局的商业分类广告列表。我只是在第一次加载视图时添加按钮背景并在此处设置灰度。

public class CommericalClassifiedAds extends RelativeLayout  {
}

【问题讨论】:

  • 为什么不使用带有自定义适配器的列表视图并在自定义适配器类 getview() 等中完成所有工作?

标签: android scroll horizontal-scrolling horizontalscrollview


【解决方案1】:

让我们看看我是否说对了。要更改按钮后面的图像,您可以使用 StateListDrawable

StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] { android.R.attr.state_selected }, bitmapFromServer);
stateList.addState(new int[] {}, bitmapGrayScale);

在创建时将其添加到每个 CommericalClassifiedAds 背景,而不是按钮背景。 Register a Scroll listener 到 CommercialH 并在其中更新 UI。

private static boolean wait;
private static Handler handler = new Handler();
private static Runnable delayRunnable = new Runnable() {

            @Override
            public void run() {
                int childcount = commercialL.getChildCount();
                for (int i=0; i < childcount; i++){
                commercialL.getChildAt(i).setSelected(false);
            }
        };

@Override
public void onScrollChanged() {
    //this method can be called very frequently so only run selective
    if (!wait) {
        handler.removeCallbacks(delayRunnable);
        wait = true;
        int childcount = commercialL.getChildCount();
        for (int i=0; i < childcount; i++){
            commercialL.getChildAt(i).setSelected(true);
        }
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                wait = false;
            }
        }, 2000);
        handler.postDelayed(delayRunnable, 10000);
    }
}

如果它运行缓慢,我会首先重新考虑布局结构。看起来你的布局比需要的多,然后优化 onScroll。

我保留错误,它未经测试,但它可以让你知道如何解决你的问题。

【讨论】:

  • 非常感谢@adam...它会试试这个。
  • 不工作,在 google 上搜索很多。但找不到合适的解决方案。
最近更新 更多