StackView


content_main.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 :orientation= "horizontal"
    >
    <StackView
        android :id= "@+id/mStackView"
        android :layout_width= "match_parent"
        android :layout_height= "wrap_content"
        android :loopViews= "false"
        />
    <LinearLayout
        android :orientation= "horizontal"
        android :layout_width= "wrap_content"
        android :layout_height= "wrap_content" >
        <Button
            android :layout_width= "wrap_content"
            android :layout_height= "wrap_content"
            android :text= "上一个"
            android :onClick= "prev"
            />
        <Button
            android :layout_width= "wrap_content"
            android :layout_height= "wrap_content"
            android :text= "下一个"
            android :onClick= "next"
            />
    </LinearLayout>

</LinearLayout>


cell.xml布局
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns: android ="http://schemas.android.com/apk/res/android"
    android :orientation= "vertical"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent"
    >

    <ImageView
        android :id= "@+id/image1"
        android :layout_width= "150dp"
        android :layout_height= "200dp" />
</LinearLayout>



用SimpleAdapter将数据显示在布局上
package com.eson.stackview ;

import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
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 StackViewTest extends AppCompatActivity {

    int [] imageIds= new int []{
            R.drawable. img1, R.drawable. img2, R.drawable. img3, R.drawable. img4,
            R.drawable. img5 ,R.drawable. img6 ,R.drawable. img7 ,R.drawable. img8 ,
            R.drawable. img9 ,R.drawable. img10 ,R.drawable. img11 ,R.drawable. img12 ,
    } ;
    private StackView stackView ;

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

        stackView = (StackView) findViewById(R.id. mStackView );
        //创建一个List对象,List对象的元素是Map
        List<Map<String , Object>> listItems= new ArrayList<>() ;
        for ( int i = 0 ; i < imageIds . length; i++) {
            Map<String ,Object> listItem= new HashMap<>();
            listItem.put( "image" ,imageIds [i]) ;
            listItems.add(listItem) ;
        }
        //创建一个SimpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems , R.layout. cell , new String[]{ "image"} , new int []{R.id. image1}) ;
        stackView .setAdapter(simpleAdapter) ;

    }
    public void prev (View view){
        //显示上一个组件
        stackView .showPrevious() ;
    }
    public void next (View view){
        //显示下一个组件
        stackView .showNext() ;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值