【问题标题】:buttons for scrolling left and right in HorizontalScrollView in androidandroid中HorizontalScrollView中左右滚动的按钮
【发布时间】:2014-06-17 02:56:22
【问题描述】:
我有一个“HorizontalScrollView”的子类,其中有可变大小的图像。我想在 HorizontalScrollView 下方左右显示两个按钮。单击左键时,horizontalscollView 应按左侧第一个不可见图像的大小向左滚动,因此现在该图像可见并且与右键相同。如果到达列表末尾,我还希望相应的按钮消失。
谁能帮我做这个
【问题讨论】:
标签:
java
android
android-layout
scroll
horizontal-scrolling
【解决方案1】:
只需像这样正确地实现监听器:
此方法获取一组图像 id 并滚动到数据中提供的那个
适当地使用 getLeft 和 getRight
/**
* handles button presses that requiere extra data to be passed along
* for dynamic created buttons
* in this app it uses mainly for the navigation bar buttons
* @author new
*
*/
public class ButtonHandler implements OnClickListener,Runnable
{
private int_data;//current left or right location
private ArrayList<Integer> _resultId;//the id's of the imagess
private Activity _activity;
public ButtonHandler(String data,ArrayList<Integer> resultId,Activity activity)
{
_data=data;
_resultId=resultId;
_activity=activity;
}
@Override
public void run()
{
View currentView=_activity.findViewById(_resultId.get(_data));
ScrollView scrollView=(ScrollView) _activity.findViewById(R.id.resultScroll);
scrollView.scrollTo(0, currentView.getLeft());
//get new image for nextClick
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
for (Int i:_resultId)
{
if (_resultId.getLocalVisibleRect(scrollBounds))
{// Any portion of the imageView, even a single pixel, is within the visible window
data=i;
}
}
}
@Override
public void onClick(View v)
{
new Handler().post(this);
}
}