Android中HorizontalScrollView的使用总结

呈现效果:

类似横向ListView,包含两个宽度布满屏幕的item左右滑动

简介:

手垂直滚动视图名为ScrollView;

水平滚动视图名为HorizontalScrollView

主要注意以下3点:

(1)垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。

(2)水平方向滚动时,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent。

(3)滚动视图节点下面必须且只能挂着一个子布局节点,否则会在运行时报错。

Caused by:java.lang.IllegalStateException:ScrollView can host only one direct child

HorizontalScrollView实例:

HorizontalScrollView是Google推出的用来滚动查看视图的控件,已经替代了Gallery。

由于HorizontalScrollView继承自FrameLayout,这意味着你只能在它下面放置一个子控件,即在控件内部只能放一个字控件(一般使用LinearLayout),但是子控件可以包含很多。

缺点:HorizontalScrollView控件内部的View是不会进行回收复用的,有多少张图片就创建多少个视图。这样做比较消耗内存,如果进行大量的图片展示,会出现OOM的错误。

实现步骤:
1.创建一个HorizontalScrollView控件,在其内部添加一个LinearLayout子控件(设置id),因为我们只使用LinearLayout子控件来添加视图,所以HorizontalScrollView不用初始化。
2.创建数据集,然后实例化子控件LinearLayout。
3.创建行布局,寻找行布局,初始化子控件。
4.将行布局添加到LinearLayout中显示。

xml代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

  <HorizontalScrollView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scrollbars="none">

   <LinearLayout
       android:id="@+id/linearLayout"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

   </LinearLayout>

  </HorizontalScrollView>

</RelativeLayout>

item_one_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@color/colorAccent"
        android:text="tv1"/>

</LinearLayout>

item_two_layout.xml: 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="@color/colorPrimaryDark"
        android:text="tv2"/>

</LinearLayout>

Java代码:

private ViewGroup linearLayout;// 容器
    private int size = 2;// item个数
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

    }


    /**
     * 初始化
     * */
    private void initView(){
        linearLayout = findViewById(R.id.linearLayout);
        for (int i = 0; i < size; i++){
            View inflate = null;
            if (0==i){
                inflate = View.inflate(this,R.layout.item_one_layout,null);
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(getScreenWidth(this),300);
                inflate.setLayoutParams(params);
                TextView tv1 = inflate.findViewById(R.id.tv1);
                tv1.setText("第一个页面");
            }
            if (1==i){
                inflate = View.inflate(this,R.layout.item_two_layout,null);
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(getScreenWidth(this),300);
                inflate.setLayoutParams(params);
                TextView tv2 = inflate.findViewById(R.id.tv2);
                tv2.setText("第二个页面");
            }
            linearLayout.addView(inflate);
        }
    }

    /**
     * 屏幕高度
     * @return the height of screen, in pixel
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;
    }

    /**
     * 屏幕宽度
     * @return the width of screen, in pixel
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getSize(point);
        }
        return point.x;
    }


}


 

猜你喜欢

转载自blog.csdn.net/qq_19688207/article/details/131600063