我试图通过使用接口和以下代码修改它的边距来移动LinearLayout视图:
@Override public void onListScroll(int offset) { tabBarOffset += offset; if (tabBarOffset < 0) tabBarOffset = 0; if (tabBarOffset > 50) tabBarOffset = 50; View tabBar = findViewById(R.id.movingTabBar); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) tabBar.getLayoutParams(); params.topMargin = tabBarOffset; }代码适用于第一次调用 - 创建活动时。 但是,只要在之后调用代码块,就不会发生视图中的任何更改。 我可以确认margin参数被多次改变,因为有三个事实:1)代码在启动时工作2)记录marginTop值总是给出新值(它正在改变)3)在层次结构视图中我可以看到新的保证金价值
所以我想我只需要调用一些方法来更新视图本身? 让它重绘? 或者我可能需要在UI线程上调用一些代码? 因为此代码是从接口回调运行的。
I am trying to move a LinearLayout view by modifying it's margins using an interface and following code:
@Override public void onListScroll(int offset) { tabBarOffset += offset; if (tabBarOffset < 0) tabBarOffset = 0; if (tabBarOffset > 50) tabBarOffset = 50; View tabBar = findViewById(R.id.movingTabBar); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) tabBar.getLayoutParams(); params.topMargin = tabBarOffset; }the code works with the first call -- when the activity is created. But whenever the code block is called after, no changes in the view are being happened. I can confirm that the margin parameter is trully being changed, because of three facts : 1) the code works on start up 2) logging the marginTop value always gives new values ( it is being changed) 3) in the Hierarchy View i could see the new margin value
so i am thinking i just have to call some method to update the view itself? to make it redraw? or may be i have to call some code on the UI thread? because this code is being run from interface callback.
最满意答案
试试这个:
@Override public void onListScroll(final int offset) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { tabBarOffset += offset; if (tabBarOffset < 0) tabBarOffset = 0; if (tabBarOffset > 50) tabBarOffset = 50; ViewGroup tabBar = findViewById(R.id.movingTabBar); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) tabBar.getLayoutParams(); params.topMargin = tabBarOffset; tabBar.requestLayout(); } }); }try this:
@Override public void onListScroll(final int offset) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { tabBarOffset += offset; if (tabBarOffset < 0) tabBarOffset = 0; if (tabBarOffset > 50) tabBarOffset = 50; ViewGroup tabBar = findViewById(R.id.movingTabBar); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) tabBar.getLayoutParams(); params.topMargin = tabBarOffset; tabBar.requestLayout(); } }); }更多推荐
发布评论