Android StackView用法

来源:互联网 发布:淘宝充气娃娃哪家好 编辑:程序博客网 时间:2024/06/01 10:22

效果图

这里写图片描述

代码

首先我们看布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.stackviewdemo.MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">    <TextView        android:id="@+id/textview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="25sp"        android:textColor="#ff0000"        android:text="当前位置"        />        <Button            android:id="@+id/btn_down"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="向下轮播"            />        <Button            android:id="@+id/btn_up"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="向上轮播"            />    </LinearLayout>    <StackView        android:id="@+id/stackview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:loopViews="true"        /></LinearLayout>

布局文件很简单,主要就是使用了一个StackView,这里我们给它加了一个属性loopViews为true,表示可以循环滑动。

然后我们看Java代码

public class MainActivity extends AppCompatActivity {    private StackView stackView;    private int[] imageIds = {R.drawable.ym1,R.drawable.ym2,R.drawable.ym3,R.drawable.ym4};    private List<Integer> images = new ArrayList<>();    private ImageAdapter imageAdapter;    private TextView textView;    private Timer down;    private Timer timerup;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        stackView = (StackView) findViewById(R.id.stackview);        textView = (TextView) findViewById(R.id.textview);        initData();        imageAdapter = new ImageAdapter(images, this);        stackView.setAdapter(imageAdapter);        stackView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                textView.setText("第"+(position+1)+"个杨幂");            }        });    }    public void initData(){        for (int i = 0; i < imageIds.length; i++) {            images.add(imageIds[i]);        }    }    public void click(View view){        switch (view.getId()){            case R.id.btn_down:                if(timerup!=null){                    timerup.cancel();                }                down = new Timer();                down.schedule(new TimerTask() {                    @Override                    public void run() {                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                stackView.showNext();                            }                        });                    }                },0,1000);                break;            case R.id.btn_up:                if(down!=null){                    down.cancel();                }                timerup = new Timer();                timerup.schedule(new TimerTask() {                    @Override                    public void run() {                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                stackView.showPrevious();                            }                        });                    }                },0,1000);                break;        }    }}

给StackView设置了一个ImageAdapter

public class ImageAdapter extends BaseAdapter {    private List<Integer> mImages;    private Context mContext;    public ImageAdapter(List<Integer> mImages,Context context){        this.mImages = mImages;        mContext = context;    }    @Override    public int getCount() {        return mImages.size();    }    @Override    public Object getItem(int position) {        return mImages.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ImageView imageView = new ImageView(mContext);        imageView.setImageResource(mImages.get(position));        return imageView;    }}
0 0
原创粉丝点击