StackView使用(堆叠列表)

作者: Lee_5566 | 来源:发表于2019-08-22 11:31 被阅读12次
image.png

目录

StackView

StackView也是AdapterViewAnimator的子类,它也用于显示Adapter提供的一系列View。 StackView将会以堆叠(Stack)的方式来显示多个列表项。

StackView操作

1.拖走StackView中处于顶端的View,下一个View将会显示出来。将上一个View拖进StackView,将使之显示出来。
2.通过调用StackView的showNext()、showPrevious()控制显示下一个、上一个组件。

实战

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <StackView
        android:id="@+id/stackview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/prev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="prev"
        android:text="上一个"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/next"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="next"
        android:text="下一个"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/prev"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

代码文件;

package com.example.user.picture;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.StackView;

public class MainActivity extends AppCompatActivity {

    // 图片资源
    int[] picture = new int[]{R.drawable.p001,R.drawable.p002,
            R.drawable.p003,R.drawable.p004};
    StackView stackView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取stackView
        stackView = (StackView)findViewById(R.id.stackview);

        // 为获取stackView添加适配器
        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return picture.length;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // 创建图片控件
                ImageView imageview = new ImageView(MainActivity.this);
                // 为控件添加资源
                imageview.setImageResource(picture[position]);

                imageview.setScaleType(ImageView.ScaleType.FIT_XY);

                imageview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));

                return imageview;
            }
        };

        stackView.setAdapter(adapter);
    }

    // 设定按钮函数
    public void prev(View source){
        stackView.showPrevious();
    }

    // 设定按钮函数
    public void next(View source){
        stackView.showNext();
    }
}

运行效果

image.png
image.png

参考

StackView实现卡片堆叠如此简单

相关文章

  • StackView使用(堆叠列表)

    目录 StackView StackView也是AdapterViewAnimator的子类,它也用于显示Adap...

  • StackView卡片堆叠

    一、认识StackView StackView也是AdapterView Animator的子类,它也用于显示Ad...

  • storyboard中的一些方法总结

    storyBoard上的StackView UIStackView是一个管理一组堆叠视图的控制器类视图,所谓堆叠视...

  • StackView

    Android中StackView的使用Android StackView用法

  • iOS-UIStackView浅析

    一、UIStackView简介 概念:一个堆叠视图的容器,iOS9的新特性。 用途:StackView及其子视图会...

  • iOS UIStackView

    一、UIStackView简介 概念:一个堆叠视图的容器,iOS9的新特性。用途:StackView及其子视图会自...

  • StackView使用问题

    StackView隐藏其子视图时发生约束冲突StackView在隐藏它的子view时,会使子view高度约束为0 ...

  • 系统小组件

    Android Widget 小部件(四---完结) 使用ListView、GridView、StackView...

  • swift UIStackView 背景色 无效

    合理使用 stackview,可以减少constraint的使用。constraint太多,也会对view的维护造...

  • Android StackView 使用示例

    cell.xml 布局文件: 主布局文件: 主程序代码: 运行效果: 参考文献:《疯狂Android讲义(第2版)》

网友评论

    本文标题:StackView使用(堆叠列表)

    本文链接:https://www.haomeiwen.com/subject/fzhpsctx.html