抱歉没有发布任何代码。 在 stackoverflow 和 google 上搜索,但找不到合适的解决方案,所以如果您对此有任何想法或技巧,请帮帮我。
要求:
我有许多按钮根据网络的要求以编程方式创建。并将所有按钮的背景图像设置为灰度。我已经完成了这部分,但是当滚动水平 scollview 时,我想更改该按钮(滚动时可见)背景图像彩色(初始加载灰度),并且在特定时间让我们假设 10 秒按钮背景设置初始状态(灰度) .
主题:
Change button background image when scolling on visiable view's button. for cetain time interval(10 second).
`
问题:
我无法获取当前在水平 View 中可见的按钮,也无法更改该按钮的背景彩色图像(第一次 View 加载)。我只想以编程方式实现这一点,因为所有图像都来自服务器端。
任何想法,如何完成这项工作或这项任务的任何引用。
注意:
我创建按钮
View 并设置图像背景,并在线性布局
上添加图像,并将该线性布局添加到其他 mainlinearlayout
并在 horizontalview
布局上添加 main Linearlayout。
我的代码的示例 fragment :
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);
}
............
它可以返回带有按钮 View 的相对布局的商业分类广告列表。我只是添加按钮背景并在此处设置灰度首次加载 View 。
public class CommericalClassifiedAds extends RelativeLayout {
}
最佳答案
让我们看看我是否理解正确。要更改按钮后面的图像,您可以使用 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。
我对错误有所保留,它未经测试,但它可以让您了解如何解决您的问题。
关于android - 在android中滚动时更改按钮的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28140902/