我的Android应用程序中有水平滚动视图,带有下一个和上一个按钮。我想仅在scollview需要滚动时显示这些按钮。滚动视图内容的宽度超出显示宽度。还要隐藏上一个和下一个按钮时分别到达第一个和最后一个项目。点击这些按钮后如何下一个/上一个项目?
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<Button
android:id="@+id/btnPrevoius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Previous"
android:visibility="gone" />
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_toLeftOf="@+id/btnNext"
android:layout_toRightOf="@+id/btnPrevoius"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Next"
android:visibility="gone" />
</RelativeLayout>
活动
public class SampleActivity extends Activity {
private static LinearLayout linearLayout;
private static HorizontalScrollView horizontalScrollView;
private static Button btnPrevious;
private static Button btnNext;
private static int displayWidth = 0;
private static int arrowWidth = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
btnPrevious = (Button) findViewById(R.id.btnPrevoius);
btnNext = (Button) findViewById(R.id.btnNext);
for (int i = 0; i < 15; i++) {
Button button = new Button(this);
button.setTag(i);
button.setText("---");
linearLayout.addView(button);
}
ViewTreeObserver vto = linearLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = linearLayout.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
Display display = getWindowManager().getDefaultDisplay();
displayWidth = display.getWidth();
if (linearLayout.getMeasuredWidth() > (displayWidth - 40)) {
btnPrevious.setVisibility(View.VISIBLE);
btnNext.setVisibility(View.VISIBLE);
}
}
});
btnPrevious.setOnClickListener(listnerLeftArrowButton);
horizontalScrollView.setOnTouchListener(listenerScrollViewTouch);
}
private OnTouchListener listenerScrollViewTouch = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
showHideViews();
return false;
}
};
private OnClickListener listnerLeftArrowButton = new OnClickListener() {
@Override
public void onClick(View v) {
horizontalScrollView.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0));
}
};
public static void showHideViews() {
int maxScrollX = horizontalScrollView.getChildAt(0).getMeasuredWidth()- displayWidth;
Log.e("TestProjectActivity", "scroll X = " +horizontalScrollView.getScrollX() );
Log.i("TestProjectActivity", "scroll Width = " +horizontalScrollView.getMeasuredWidth() );
Log.d("TestProjectActivity", "Max scroll X = " + maxScrollX);
if (horizontalScrollView.getScrollX() == 0) {
hideLeftArrow();
} else {
showLeftArrow();
}
if (horizontalScrollView.getScrollX() == maxScrollX) {
showRightArrow();
} else {
//hideRightArrow();
}
}
private static void hideLeftArrow() {
btnPrevious.setVisibility(View.GONE);
}
private static void showLeftArrow() {
btnPrevious.setVisibility(View.VISIBLE);
}
private static void hideRightArrow() {
btnNext.setVisibility(View.GONE);
}
private static void showRightArrow() {
btnNext.setVisibility(View.VISIBLE);
}
}
'maxScrollX'
值对我来说不正确。如何为此找到最大滚动值?
在此先感谢
答案 0 :(得分:4)
这可能会有点晚,但对于那些将面临这个问题的人我建议替代解决方案。
首先,使用与HorizontalScrollView不同的组件。以下是选项:
选项1: Horizontal ListView - 将此类添加到项目中(创建一个单独的包,类似于com.yourproject.widgets)。此外,您还需要创建自定义适配器,请参阅此example中的完成方式。我建议你创建单独的适配器类(exp。HorizontalListViewAdapter)并将其放入已创建的com.yourproject.widgets包中。
<com.yourproject.widgets.HorizontalListView
android:id="@+id/hList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
HorizontalListView mHList = (HorizontalListView) findViewById (R.id.hList);
Button bPrevoius = (Button) findViewById (R.id.btnPrevoius);
Button bNext = (Button) findViewById (R.id.btnNext);
将onClickListeners添加到按钮。使用HorizontalListView小部件中预定义的scrollTo()函数。正如您在代码中看到的那样,它需要int dp值才能滚动。如果要向右(下一个)滚动,请添加正值;如果要向左滚动(上一个),请使用负值:
bPrevoius.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//value 500 is arbitrarily given. if you want to achieve
//element-by-element scroll you should get the width of the
//previous element dynamically or if the elements of the
//list have uniform width just put that value instead
mHList.scrollTo(-500);
//if it's the first/last element you can bPrevoius.setEnabled(false)
}
});
bNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mHList.scrollTo(500);
}
});
选项2:此问题的最新解决方案可能是Android L中引入的新窗口小部件RecyclerView(添加android:scrollbars =“vertical”似乎可以执行此操作技巧;除此之外应该有传统的ListView行为)。有关更多信息,请查看官方文档。
答案 1 :(得分:1)
DEVU
Plz看看以下链接
1)http://android-er.blogspot.in/2012/07/implement-gallery-like.html
2)http://androiddreamers.blogspot.in/2012/09/horizontal-scroll-view-example.html
3)http://code.google.com/p/mobyfactory-uiwidgets-android/
如果您遇到任何问题,请告诉我
由于
答案 2 :(得分:-1)
在钛金属加速器中,你可以使用scrollableView
来做到这一点var scrollableView = Ti.UI.createScrollableView({
showPagingControl:true,
scrollingEnabled: true,
top: 360
});
然后,您可以运行所有图像或任何内容的循环,并将它们添加到此视图中。
for(loop){
eval(&#34; var view&#34; + i +&#34; = Ti.UI.createView();&#34;);
profile_image = Ti.UI.createImageView({
image: result[0]['profile_image'],
left:15,
width:82,
height:104,
top: 0
});
eval("view"+i+".add(profile_image);");
eval("scrollableView.addView(view"+i+");");
}
mywin.add(scrollableView);