随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)
在GitHub上有提供一个非常强大而且绚丽的控件FlipView,用于翻页,类似于翻书一样,可以上下翻页,上面有提供相应jar包(aphid-flipview-library.jar),里面封装了一个控件类FlipViewController,该类继承AdapterView,所以可以把它看成类似于ListView、GridView、Gallery或者Spinner的一个控件,同样使用adapter填充数据,示例如下:
TestFlipViewActivity:
package com.home.testflipview;
import com.aphidmobile.flip.FlipViewController;
import android.os.Bundle;
import android.app.Activity;
public class TestFlipViewActivity extends Activity {
// 翻页控件
private FlipViewController flipView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
flipView = new FlipViewController(this);
flipView.setAdapter(new MyBaseAdapter(this));
setContentView(flipView);
}
@Override
protected void onResume() {
super.onResume();
flipView.onResume();
}
@Override
protected void onPause() {
super.onPause();
flipView.onPause();
}
}
adapter:
package com.home.testflipview;
import java.util.ArrayList;
import java.util.List;
import com.aphidmobile.utils.AphidLog;
import com.aphidmobile.utils.IO;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private static List<Data> list = new ArrayList<Data>();
private LayoutInflater inflater;
// 为list集合添加数据
static {
list.add(new Data(
"Potala Palace",
"potala_palace.jpg",
"http://en.wikipedia.org/wiki/Potala_Palace",
"The <b>Potala Palace</b> is located in Lhasa, Tibet Autonomous Region, China. It is named after Mount Potalaka, the mythical abode of Chenresig or Avalokitesvara."));
list.add(new Data(
"Drepung Monastery",
"drepung_monastery.jpg",
"http://en.wikipedia.org/wiki/Drepung",
"<b>Drepung Monastery</b>, located at the foot of Mount Gephel, is one of the \"great three\" Gelukpa university monasteries of Tibet."));
list.add(new Data(
"Sera Monastery",
"sera_monastery.jpg",
"http://en.wikipedia.org/wiki/Sera_Monastery",
"<b>Sera Monastery</b> is one of the 'great three' Gelukpa university monasteries of Tibet, located 1.25 miles (2.01 km) north of Lhasa."));
list.add(new Data(
"Samye Monastery",
"samye_monastery.jpg",
"http://en.wikipedia.org/wiki/Samye",
"<b>Samye Monastery</b> is the first Buddhist monastery built in Tibet, was most probably first constructed between 775 and 779 CE."));
list.add(new Data(
"Tashilunpo Monastery",
"tashilunpo_monastery.jpg",
"http://en.wikipedia.org/wiki/Tashilhunpo_Monastery",
"<b>Tashilhunpo Monastery</b>, founded in 1447 by Gendun Drup, the First Dalai Lama, is a historic and culturally important monastery next to Shigatse, the second-largest city in Tibet."));
list.add(new Data(
"Zhangmu Port",
"zhangmu_port.jpg",
"http://en.wikipedia.org/wiki/Zhangmu",
"<b>Zhangmu/Dram</b> is a customs town and port of entry located in Nyalam County on the Nepal-China border, just uphill and across the Bhote Koshi River from the Nepalese town of Kodari."));
list.add(new Data(
"Kathmandu",
"kathmandu.jpg",
"http://en.wikipedia.org/wiki/Kathmandu",
"<b>Kathmandu</b> is the capital and, with more than one million inhabitants, the largest metropolitan city of Nepal."));
list.add(new Data(
"Pokhara",
"pokhara.jpg",
"http://en.wikipedia.org/wiki/Pokhara",
"<b>Pokhara Sub-Metropolitan City</b> is the second largest city of Nepal with approximately 250,000 inhabitants and is situated about 200 km west of the capital Kathmandu."));
list.add(new Data(
"Patan",
"patan.jpg",
"http://en.wikipedia.org/wiki/Patan,_Nepal",
"<b>Patan</b>, officially Lalitpur Sub-Metropolitan City, is one of the major cities of Nepal located in the south-central part of Kathmandu Valley."));
}
public MyBaseAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.content, null);
final Data data = list.get(position);
TextView titleView = (TextView) convertView.findViewById(R.id.title);
// 为titleView设置显示内容
titleView.setText(AphidLog.format("%d. %s", position, data.title));
// 为photoView设置图片背景
ImageView photoView = (ImageView) convertView.findViewById(R.id.photo);
// readBitmap:调用封装好的IO操作方法根据图片名称从assets中读取相应图片
photoView.setImageBitmap(IO.readBitmap(inflater.getContext()
.getAssets(), data.imageFilename));
TextView textView = (TextView) convertView
.findViewById(R.id.description);
// 为textView设置要显示的内容
textView.setText(Html.fromHtml(data.description));
Button wikipedia = (Button) convertView.findViewById(R.id.wikipedia);
// 为按钮设置监听事件
wikipedia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 以跳转页面方式打开链接对应的网址
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(data.link));
inflater.getContext().startActivity(intent);
}
});
return convertView;
}
private static class Data {
public String title;
public String imageFilename;
public String link;
public String description;
private Data(String title, String imageFilename, String link,
String description) {
this.title = title;
this.imageFilename = imageFilename;
this.link = link;
this.description = description;
}
}
}
content.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAppearance="@android:style/TextAppearance.Large"
android:textColor="@android:color/black"
android:textStyle="bold" />
<ImageView
android:id="@+id/photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:scaleType="centerCrop" />
<Button
android:id="@+id/wikipedia"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Go to Wikipedia"
android:textAppearance="@android:style/TextAppearance.Large" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:textColor="@android:color/black" />
</LinearLayout>
自己在使用时自定义content布局文件即可,可以实现非常漂亮和灵活的效果。