【问题标题】:How to move views dynamically on scroll in android如何在android中滚动时动态移动视图
【发布时间】:2015-07-31 17:42:02
【问题描述】:

我正在处理任务,要求在用户滚动列表时动态调整视图宽度和高度。

建议的屏幕将包含两个部分

A.

的线性布局
  1. 图像视图
  2. 文本视图和
  3. 按钮垂直。

B.Scroll 视图(线性布局下方)- 这将在内部包含一些子视图。

最初LinearLayout 应该以垂直(具有固定的高度和宽度)顺序显示其成员,但是一旦用户向上滚动,它应该将其成员更改为水平(高度和宽度减小以固定到屏幕水平)

【问题讨论】:

    标签: android android-layout android-linearlayout android-view


    【解决方案1】:

    子类化滚动视图并覆盖其中的 onScrollChanged 方法。在 onScrollChanged 方法中更改顶部线性布局的方向(即您的垂直线性布局)

      public class MyScrollView extends ScrollView {
    
        public MyScrollView(Context context) {
          super(context);
        }
    
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
          super.onScrollChanged(l, t, oldl, oldt);
          linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        }
      }
    

    或者您可以通过添加滚动更改侦听器来做到这一点,如下所示

    scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    
      @Override
      public void onScrollChanged() {
    
        int scrollY = scrollView.getScrollY();
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    
      }
    });
    

    【讨论】:

      【解决方案2】:

      滚动视图只有一个父子。 在滚动视图创建一个线性布局后,然后在其中实现您的代码..

      如下代码...

      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
          <TextView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/txt"/>
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-20
        相关资源
        最近更新 更多