很抱歉没有发布任何代码。 在stackoverflow和google上搜索但找不到合适的解决方案,所以如果你有任何想法或技巧,请你帮我解决。
要求:
我根据网络要求以编程方式创建了许多按钮。并使用灰度设置所有按钮的背景图像。我已经完成了这个部分但是当滚动水平scollview然后我想要更改那些按钮(滚动时可见)背景图像着色(初始加载灰度)和一定时间让我们假设10秒按钮背景设置初始状态(灰度)
主题:
在可见视图上晃动时更改按钮背景图像 按钮。因为时间间隔(10秒)。
`
问题:
我无法在水平视图中看到当前可见的按钮并更改该按钮背景彩色图像(第一次加载视图)。所有我想以编程方式进行此操作,因为所有图像都来自服务器端。
任何想法,怎么做这个工作或任何参考这个任务。
注意:
我创建 button
视图并设置图像背景并在 linearlayout
上添加图片,并添加其他 mainlinearlayout
<的线性布局/ em>并在 horizontalview
布局上添加mainlinearlayout。
示例剪切了我的代码:
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);
}
...............
CommericalClassifiedAds列表,它可以使用buttonview返回relativelayout。我只是添加按钮背景,并在第一次加载视图时设置灰度。
public class CommericalClassifiedAds extends RelativeLayout {
}
答案 0 :(得分:2)
让我们看看我是否帮助你。要更改按钮后面的图像,可以使用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。
我已经预订了错误,但未经过测试,但它可以让您了解如何解决问题。