Android StackView 使用示例

cell.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cellImage"
    android:layout_width="200dp"
    android:layout_height="200dp"
    />

主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@color/colorGray"
    >

    <StackView
        android:id="@+id/stackView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:loopViews="true"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="prev"
        android:text="@string/prev"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="next"
        android:text="@string/next"
        />

</LinearLayout>

主程序代码:

package com.toby.personal.testlistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.SimpleAdapter;
import android.widget.StackView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

    final private static String TAG = "Toby_Test";

    private StackView stackView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final int[] images = new int[]{
                R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04,
                R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08,
                R.drawable.img09, R.drawable.img10, R.drawable.img11, R.drawable.img12,
                R.drawable.dog_001, R.drawable.dog_002, R.drawable.dog_003, R.drawable.dog_004,
                R.drawable.dog_005,
                R.drawable.girl01, R.drawable.girl02, R.drawable.girl03, R.drawable.girl04,
                R.drawable.girl05
        };

        List<Map<String, Object>> listItems = new ArrayList<>();

        for (int i = 0; i < images.length; ++i) {
            Map<String, Object> listItem = new HashMap<>();
            listItem.put("image", images[i]);
            listItems.add(listItem);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell,
                new String[] {"image"}, new int[] {R.id.cellImage});

        stackView = (StackView) findViewById(R.id.stackView);
        stackView.setAdapter(simpleAdapter);
    }


    public void prev(View view) {
        stackView.showPrevious();
    }

    public void next(View view) {
        stackView.showNext();
    }

}

运行效果:


运行效果

参考文献:《疯狂Android讲义(第2版)》

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,711评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,226评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,518评论 19 139
  • 我静坐在月亮之上 借着如水的月光 阅读着我喜欢的诗歌 而此时,月亮正漂浮在海面 脚下的海水清澈 看得见海龟和五彩的...
    心素如简之人淡如菊阅读 1,781评论 0 4
  • 你说你要让我传递给你我还喜欢你的消息,而不是我总是让你以为你在我的生命里是可有可无的附属品。 你说你喜欢我的傻里傻...
    微醺一世阅读 2,744评论 2 1