界面布局文件 res\layout\slide_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="180dp"
android:numColumns="4"
android:horizontalSpacing="1pt"
android:verticalSpacing="1pt"
/>
图标布局文件 res\layout\label_icon.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="55dp"
android:layout_height="55dp"
android:scaleType="centerCrop"
android:gravity="center"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
自定义动画资源 res\anim\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>
自定义动画资源 res\anim\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>
主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorGray"
>
<ViewSwitcher
android:id="@+id/viewSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/prevBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:onClick="prev"
android:text="@string/prev"
/>
<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:onClick="next"
android:text="@string/next"
/>
</RelativeLayout>
主程序代码:
package com.toby.personal.testlistview;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;
public class MainActivity extends AppCompatActivity {
final private static String TAG = "Toby_Test";
final private static int NUMBER_PRE_SCREEN = 12;
private static class DataItem {
String dataName;
Drawable drawable;
}
private ArrayList<DataItem> items = new ArrayList<>();
private int screenNo = -1;
private int screenCount;
ViewSwitcher viewSwitcher;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = LayoutInflater.from(this);
for (int i = 0; i < 40; ++i) {
String label = String.valueOf(i);
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher, null);
DataItem item = new DataItem();
item.dataName = label;
item.drawable = drawable;
items.add(item);
}
screenCount = items.size() % NUMBER_PRE_SCREEN == 0 ?
items.size() / NUMBER_PRE_SCREEN : items.size() / NUMBER_PRE_SCREEN + 1;
viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
return inflater.inflate(R.layout.slide_list_view, null);
}
});
next(null);
}
public void prev(View view) {
if (screenNo > 0) {
--screenNo;
viewSwitcher.setInAnimation(this, android.R.anim.slide_in_left);
viewSwitcher.setOutAnimation(this, android.R.anim.slide_out_right);
((GridView) viewSwitcher.getNextView()).setAdapter(adapter);
viewSwitcher.showPrevious();
}
}
public void next(View view) {
if (screenNo < screenCount - 1) {
++screenNo;
viewSwitcher.setInAnimation(this, R.anim.slide_in_right);
viewSwitcher.setOutAnimation(this, R.anim.slide_out_left);
((GridView) viewSwitcher.getNextView()).setAdapter(adapter);
viewSwitcher.showNext();
}
}
private BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
if (screenNo == screenCount - 1 && items.size() % NUMBER_PRE_SCREEN != 0) {
return items.size() % NUMBER_PRE_SCREEN;
}
return NUMBER_PRE_SCREEN;
}
@Override
public DataItem getItem(int i) {
return items.get(screenNo * NUMBER_PRE_SCREEN + i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
view = inflater.inflate(R.layout.label_icon, null);
}
final DataItem dataItem = getItem(position);
if (dataItem != null) {
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
if (imageView != null) {
imageView.setImageDrawable(dataItem.drawable);
}
TextView textView = (TextView) view.findViewById(R.id.textView);
if (textView != null) {
textView.setText(dataItem.dataName);
}
}
return view;
}
};
}
运行效果图:
参考文献:《疯狂Android讲义(第2版)》