gpt4 book ai didi

android - 如何在 android 中单击按钮时滚动 Horizo​​ntalScrollView?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:47:50 25 4
gpt4 key购买 nike

我的 android 应用程序中有水平 ScrollView ,带有下一个和上一个按钮。我只想在 scollview 需要滚动时显示这些按钮。即, ScrollView 内容的宽度超过显示宽度。还想隐藏上一个和下一个按钮分别到达第一个和最后一个项目。单击这些按钮时如何转到下一个/上一个项目?

ma​​in.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>

Activity

 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' 值对我来说不正确。如何为此找到最大滚动值?提前致谢

最佳答案

这可能会来得有点晚,但对于将面临此问题的任何人,我建议替代解决方案。

首先,使用与 Horizo​​ntalScrollView 不同的组件。以下是选项:

选项 1: Horizontal ListView - 将此类添加到您的项目中(创建一个单独的包,例如 com.yourproject.widgets)。此外,您还需要创建自定义适配器,请参阅此 example 中的操作方式.我建议您创建单独的适配器类 (exp. Horizo​​ntalListViewAdapter) 并将其放入已创建的 com.yourproject.widgets 包中。

  • 将此小部件添加到您在 xml 中的布局(将其放在需要模仿滚动行为的按钮之间)您需要添加如下内容:
<com.yourproject.widgets.HorizontalListView
android:id="@+id/hList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
  • 在使用小部件的 Activity/Fragment 中引用它(连同按钮)
HorizontalListView mHList = (HorizontalListView) findViewById (R.id.hList); 
Button bPrevoius = (Button) findViewById (R.id.btnPrevoius);
Button bNext = (Button) findViewById (R.id.btnNext);
  • 将 onClickListeners 添加到按钮。使用 Horizo​​ntalListView 小部件中预定义的 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: 这个问题的最新解决方案可以是新的小部件 RecyclerView在 Android L 中引入(添加 android:scrollbars="vertical"似乎可以解决问题;除此之外应该具有传统的 ListView 行为)。有关更多信息,请查看官方文档。

关于android - 如何在 android 中单击按钮时滚动 Horizo​​ntalScrollView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com