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版)》

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,893評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,161評論 4 61
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • 我靜坐在月亮之上 借著如水的月光 閱讀著我喜歡的詩歌 而此時,月亮正漂浮在海面 腳下的海水清澈 看得見海龜和五彩的...
    心素如簡之人淡如菊閱讀 294評論 0 4
  • 你說你要讓我傳遞給你我還喜歡你的消息,而不是我總是讓你以為你在我的生命里是可有可無的附屬品。 你說你喜歡我的傻里傻...
    微醺一世閱讀 406評論 2 1

友情鏈接更多精彩內(nèi)容