首先新建一个Android项目,命名为ViewFlipperTest
如图:项目机构,本项目主要操作图中红色箭头标注的文件
1.HgroupAdapter.java文件代码↓主要实现listview数据适配器的定义
package com.hll.ViewFlipperTest; import java.util.List; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; public class HgroupAdapter extends BaseAdapter {
private LayoutInflater mInflater;
int state;
Context mContext;
String mState;
List<String> mList;
int sel = ; public HgroupAdapter(Context context, List<String> list, int menuState) {
this.mList = list;
this.mContext = context;
this.state = menuState;
mInflater = LayoutInflater.from(context);
} public int getCount() {
// if(mList == null){
// return 0;
// }
// return mList.size();
return ;
} public Object getItem(int position) {
return mList.get(position);
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grouplist, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
} static class ViewHolder {
TextView group_name;
TextView time;
TextView info;
}
}
2.ViewFlipperTest.java 程序启动的主文件↓
package com.hll.ViewFlipperTest; import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper; @SuppressLint("NewApi")
public class ViewFlipperTest extends Activity implements OnTouchListener,
OnGestureListener, OnDoubleTapListener {
private ViewFlipper mFlipper; // 翻转视图
GestureDetector mGestureDetector; // 手势识别
private int mCurrentLayoutState; // 当前布局状态
private static final int FLING_MIN_DISTANCE = ;
private static final int FLING_MIN_VELOCITY = ; TextView counttv;
Button buttonNext1 = null;
Button buttonNext2 = null; ListView lv1 = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); findView();
setListener();
} /*
* 查找控件
*/
@SuppressWarnings("deprecation")
public void findView() {
mFlipper = (ViewFlipper) findViewById(R.id.details);
mFlipper.setLongClickable(true);
mGestureDetector = new GestureDetector(this);
mCurrentLayoutState = ; counttv = (TextView) findViewById(R.id.counttv);
buttonNext1 = (Button) findViewById(R.id.Button_next1);
buttonNext2 = (Button) findViewById(R.id.Button_next2);
lv1 = (ListView) findViewById(R.id.list1); lv1.setAdapter(new HgroupAdapter(this, null, ));
} public void setListener() { mFlipper.setOnTouchListener(this);
lv1.setOnTouchListener(this);
counttv.setText("");
buttonNext1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mFlipper.showNext();
counttv.setText("");
}
});
buttonNext2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mFlipper.showNext();
counttv.setText("");
} });
} //
protected Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration();
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
} protected Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration();
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
} protected Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration();
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
} protected Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration();
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
} public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
} public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) { mFlipper.setInAnimation(inFromRightAnimation());
mFlipper.setOutAnimation(outToLeftAnimation());
mFlipper.showNext();
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) { mFlipper.setInAnimation(inFromLeftAnimation());
mFlipper.setOutAnimation(outToRightAnimation());
mFlipper.showPrevious();
}
return false;
} public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onLongPress", Toast.LENGTH_LONG)
.show();
} public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Toast.makeText(getApplicationContext(), "onScroll", Toast.LENGTH_LONG)
.show();
return false;
} public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onShowPress", Toast.LENGTH_LONG)
.show();
} public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onSingleTapUp", Toast.LENGTH_LONG)
.show();
return false;
} public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
} public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onDoubleTap", Toast.LENGTH_LONG)
.show();
return false;
} public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
} public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onSingleTapConfirmed", Toast.LENGTH_LONG)
.show();
return false;
} }
3. main.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="@drawable/bg"
android:orientation="vertical" > <FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="vertical" > <TextView
android:id="@+id/counttv"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/a4" />
</FrameLayout> <ViewFlipper
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoStart="false"
android:flipInterval=""
android:inAnimation="@anim/push_left_in"
android:outAnimation="@anim/push_left_out"
android:persistentDrawingCache="animation" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <Button
android:id="@+id/Button_next1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next1" >
</Button> <TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:singleLine="true"
android:text="系统消息"
android:textSize="20dip" /> <ListView
android:id="@+id/list1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="@drawable/divider_horizontal_bright" >
</ListView>
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <Button
android:id="@+id/Button_next2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next2" >
</Button> <ImageView
android:id="@+id/image2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/mail3" >
</ImageView>
</LinearLayout> </ViewFlipper>
</LinearLayout>
4. grouplist.xml ListView 列表项模版文件↓
<?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="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dip"
android:singleLine="true"
android:layout_marginLeft="10dip"
android:text="版本更新"
/> <TextView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dip"
android:singleLine="true"
android:text="2010-11-01"
android:gravity="right"
android:layout_marginRight="6dip"
/> </LinearLayout> <TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dip"
android:singleLine="true"
android:text="版本更新为2.1.1版本,请及时更新.网址..."
android:gravity="right"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
5.使用Android模拟器或者连接Android智能手机运行程序,滑动手机屏幕可以看到翻页的效果。
本项目代码源于网络,感谢无私分享的人。