liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - ViewSwitcher

    

activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ViewSwitcher
android:id="@+id/viewSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent"></ViewSwitcher>
<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="&lt;"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="next"/>
/>
<Button
android:id="@+id/preBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="&gt;"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="prev"/>
</RelativeLayout>
 
slidelistview.xml
 
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:numColumns="4"
android:layout_height="match_parent">
</GridView>
 
labelicon.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"
android:gravity="center">
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
 
 
slide_in_left.xml
 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
 
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
 
slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
 
slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

 

MainActivity.xml
 
package com.example.liguofeng.viewswitcher;

import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

// 1ページ表示数
private static final int NUM_PER_PAGE = 12;

// Item数
private static final int ITEM_COUNT = 50;

// 現在のページ
private int currentPage = -1;

// 総ページ
private int pageCount = 0;

// Itemクラス
public static class Item {
public String name; // 名
public Drawable drawable; // 画像
}

// Itemリスト
private List<Item> itemList = new ArrayList<Item>();

LayoutInflater inflater;
ViewSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = LayoutInflater.from(MainActivity.this);
// ITEM設定
setItemList();
// PAGE設定
setPageCount();
// ViewSwitcher設定
switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
switcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
// GridView
return inflater.inflate(R.layout.slidelistview, null);
}
});

next(null); // 1ページ目
}

// BaseAdapter生成
private BaseAdapter baseAdapter = new BaseAdapter() {
@Override
public int getCount() {

// 最後のページかつItem数があまりある場合
if (currentPage == pageCount - 1 &&
itemList.size() % NUM_PER_PAGE != 0) {
return itemList.size() % NUM_PER_PAGE;
} else {
// 一ページItem数
return NUM_PER_PAGE;
}
}

@Override
public Item getItem(int position) {
return itemList.get(currentPage * NUM_PER_PAGE + position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;

if (convertView == null) {
view = inflater.inflate(R.layout.labelicon, null);
}

ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageDrawable(getItem(position).drawable);

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(getItem(position).name);

return view;
}
};

// NEXTボタンクリック
public void next(View v) {
if (currentPage < pageCount - 1) {
currentPage++; // 現在ページ+1
// 動画設定
switcher.setInAnimation(this, R.anim.slide_in_right);
switcher.setOutAnimation(this, R.anim.slide_out_left);
// 次のVIEW設定
((GridView)switcher.getNextView()).setAdapter(baseAdapter);
// 次のVIEW表示
switcher.showNext();
}
}

// PREVボタンクリック
public void prev(View v) {
if (currentPage > 0) {
currentPage--; // 現在ページ-1
// 動画設定
switcher.setInAnimation(this, R.anim.slide_in_left);
switcher.setOutAnimation(this, R.anim.slide_out_right);
// 次のVIEW設定
((GridView)switcher.getNextView()).setAdapter(baseAdapter);
// 次のVIEW表示
switcher.showPrevious();
}
}

// Item生成
private void setItemList() {
for (int i = 0; i < ITEM_COUNT; i++) {
Item item = new Item();
item.name = "Item" + i;

// なぜかDeprecated、ま~いいや
item.drawable = getResources().getDrawable(R.drawable.ic_launcher);

this.itemList.add(item);
}
}
// 総ページ数
private void setPageCount() {
this.pageCount = ITEM_COUNT % NUM_PER_PAGE == 0 ?
ITEM_COUNT / NUM_PER_PAGE :
ITEM_COUNT / NUM_PER_PAGE + 1;
}
}