功能:
新浪微博消息页面
使用说明:
先用自定义的一个类:HorizontalPager 实现左右滑动,实现起来比较清爽、简单
HorizontalPager 类在第一篇学UI博客
看图吧!先看效果!!!
滑动触发事件。
接下来看代码吧!
界面activity:
package com.bbswp.demo;
import com.bbswp.demo.view.HorizontalPager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private HorizontalPager mPager;
private ImageButton image1;
private ImageButton image2;
private ImageButton image3;
private ImageButton image4;
private ImageView imageView;
private int startLeft;
private int location;
private int pos = 25;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (HorizontalPager) findViewById(R.id.horizontal_pager);
mPager.setOnScreenSwitchListener(onScreenSwitchListener);
image1 = (ImageButton) findViewById(R.id.btn_1);
image2 = (ImageButton) findViewById(R.id.btn_2);
image3 = (ImageButton) findViewById(R.id.btn_3);
image4 = (ImageButton) findViewById(R.id.btn_4);
imageView = (ImageView) findViewById(R.id.imageView);
}
private final HorizontalPager.OnScreenSwitchListener onScreenSwitchListener =
new HorizontalPager.OnScreenSwitchListener() {
@Override
public void onScreenSwitched(final int screen) {
switch (screen) {
case 0:
image1.performClick();
break;
case 1:
image2.performClick();
break;
case 2:
image3.performClick();
break;
case 3:
image4.performClick();
break;
default:
break;
}
}
};
public void startAnimation(int startX, int toX, int startY, int toY) {
TranslateAnimation anim = new TranslateAnimation(startX, toX, startY, toY);
anim.setDuration(200);
anim.setFillAfter(true);
imageView.startAnimation(anim);
}
public void onClick1(View view) {
location = imageView.getWidth()+pos;
toast(1);
mPager.setCurrentScreen(0, true);
startAnimation(0, 0, 0, 0);
startLeft = 0;
}
public void onClick2(View view) {
location = imageView.getWidth()+pos;
toast(2);
mPager.setCurrentScreen(1, true);
startAnimation(startLeft, location*1, 0, 0);
startLeft = location*1;
}
public void onClick3(View view) {
location = imageView.getWidth()+pos;
toast(3);
mPager.setCurrentScreen(2, true);
startAnimation(startLeft, location*2, 0, 0);
startLeft = location*2;
}
public void onClick4(View view) {
location = imageView.getWidth()+pos;
toast(4);
mPager.setCurrentScreen(3, true);
startAnimation(startLeft, location*3, 0, 0);
startLeft = location*3;
}
private void toast(int i) {
Toast.makeText(this, "第几个:" + i, Toast.LENGTH_SHORT).show();
}
}
上面还是比较简单吧!
当然还需要另一个左右滑动的类,在第一个博客里有:http://blog.csdn.net/hudan2714/article/details/8216818
这里就不再帖出来了。类代码是:HorizontalPager
当然最后还是xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="@drawable/titlebar_bg_nor"
android:gravity="top"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="70dip"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btn_1"
style="@style/titleIcon"
android:background="@drawable/msg_group_at"
android:onClick="onClick1" />
<ImageButton
android:id="@+id/btn_2"
style="@style/titleIcon"
android:background="@drawable/msg_group_comment"
android:onClick="onClick2" />
<ImageButton
android:id="@+id/btn_3"
style="@style/titleIcon"
android:background="@drawable/msg_group_message"
android:onClick="onClick3" />
<ImageButton
android:id="@+id/btn_4"
style="@style/titleIcon"
android:background="@drawable/msg_group_notification"
android:onClick="onClick4" />
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="20dip"
android:layout_height="2dip"
android:layout_marginLeft="80dip"
android:layout_marginBottom="3dip"
android:background="@drawable/title_bar_mark" />
</LinearLayout>
<com.bbswp.demo.view.HorizontalPager
android:id="@+id/horizontal_pager"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_gravity="bottom"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="我是@页面"
android:textSize="24dip"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="我是消息页面"
android:textSize="24dip"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="我是私信页面"
android:textSize="24dip"
android:textStyle="bold" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="我是通知页面"
android:textSize="24dip"
android:textStyle="bold" />
</com.bbswp.demo.view.HorizontalPager>
</LinearLayout>
如果不想使用android.support.v4.view 包,可以使用此方法来实现滑动功能了!
当然,后面也会使用android.support.v4.view包来实现此功能,分享给大家!!!
不能少了源码分享给大家的!
欢迎大家下载,一起学习交流!!!
下载地址:http://download.csdn.net/detail/hudan2714/4809950