界面如图.有点粗陋,不要在意,只是充个数.
监听屏幕滑动,结合距离和滑动速度来判断翻页.
保证跟效果跟使用RadioGroup+ViewPager+Fragment+FragmentManagement的效果一样!
注意,可以跟上面的顶部分栏联动的喔,个人觉得很有用,特拿出来分享.~
首先,贴上自定义的HorizontalScrollview
/**
* 翻页效果的水平滑动视图
* @author 心動,即是不理性℃
*
*/
public class PageScrollView extends HorizontalScrollView{
private int childCount = 0;
private ViewGroup ll_container = null;
private int currentPage = 0;
private ArrayList pointList = new ArrayList();
public PageScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public PageScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PageScrollView(Context context) {
super(context);
init();
}
private void init() {
setHorizontalScrollBarEnabled(false);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
retrieveChildInfo();
}
public void retrieveChildInfo() {
ll_container = (ViewGroup) getChildAt(0);
if (ll_container != null) {
childCount = ll_container.getChildCount();
for (int i = 0; i < childCount; i++) {
if ((ll_container.getChildAt(i)).getWidth() > 0) {
pointList.add((ll_container.getChildAt(i)).getLeft());
}
}
}
}
private float downX=-1,downY=-1;
private long downTime=0;
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX=ev.getRawX();
downY=ev.getRawY();
break;
case MotionEvent.ACTION_MOVE:
if(downX==-1||downY==-1||downTime==0){
downX=ev.getRawX();
downY=ev.getRawY();
downTime=System.currentTimeMillis();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
slidePage(judgeSlide(downX, downY,ev.getRawX(),ev.getRawY(),System.currentTimeMillis()-downTime,this.getContext()));
downTime=0;
return true;
}
return super.onTouchEvent(ev);
}
/**
* 关于翻页的判断.
* Last Modifi.. 161123
* @param x1
* @param y1
* @param x2
* @param y2
* @return -1←左,1→右,0-如如不動.
*
*/
private int judgeSlide(float x1,float y1,float x2,float y2,long time,Context context){
int ori=0;
// if(Math.abs(y2-y1)timeGenkai?genkaiNormal:genkaiFast;
if(x2-x1>genkai){
ori= -1;
}else if(x2-x1<-genkai){
ori= 1;
}
android.util.Log.e("", "kyori:"+(x2-x1)+";time:"+time);
// }
return ori;
}
private final int genkaiNormal=Util.getScreenWidth(getContext())/4;
private final long timeGenkai=60;
private final int genkaiFast=16;
/**
* 翻页
* @param ori 方向
*/
private void slidePage(int ori) {
if(ori==0)
smoothScrollTo(pointList.get(currentPage), 0);
else if(ori==1){
if (currentPage < childCount - 1) {
currentPage++;
smoothScrollTo(pointList.get(currentPage), 0);
}
}else if(ori==-1){
if (currentPage > 0) {
currentPage--;
smoothScrollTo(pointList.get(currentPage), 0);
}
}
if(scrollListener!=null)
scrollListener.onPageScrolled(currentPage);
}
/**
* 跳转到指定的页面
*
* @param page
* @return
*/
public boolean gotoPage(int page) {
if (page > -1 && page < childCount) {
smoothScrollTo(pointList.get(page), 0);
currentPage = page;
return true;
}
return false;
}
private OnPageScrolledListener scrollListener;
public interface OnPageScrolledListener{
void onPageScrolled(int currentPage);
}
public void setOnPageScrolledListener(OnPageScrolledListener scrollListener){
this.scrollListener=scrollListener;
}
public void onPageScrolled() {
scrollListener.onPageScrolled(currentPage);
}
}
public class MainActivity extends Activity {
private RadioButton rb1,rb2;
private PageScrollView pageSv;
private ArrayList list=new ArrayList();
private BaseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
rb1=(RadioButton) findViewById(R.id.rb1);
rb2=(RadioButton) findViewById(R.id.rb2);
rb1.setOnClickListener(mClicker);
rb2.setOnClickListener(mClicker);
TextView tv=(TextView) findViewById(R.id.tv);
ListView lv=(ListView) findViewById(R.id.lv);
int screenWidth=Util.getScreenWidth(this);
tv.getLayoutParams().width=screenWidth;
tv.setLayoutParams(tv.getLayoutParams());
lv.getLayoutParams().width=screenWidth;
lv.setLayoutParams(lv.getLayoutParams());
adapter=new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String text=list.get(position);
TextView tv=new TextView(MainActivity.this);
tv.setText(text);
return tv;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
};
lv.setAdapter(adapter);
pageSv=(PageScrollView) findViewById(R.id.psv);
pageSv.setOnPageScrolledListener(new PageScrollView.OnPageScrolledListener() {
@Override
public void onPageScrolled(int currentPage) {
switch (currentPage) {
case 0:
rb1.setChecked(true);
break;
case 1:
rb2.setChecked(true);
break;
default:
break;
}
}
});
initData();
}
private void initData() {
for(int i=0;i<90;i++){
String text="TextView_"+(i+1);
list.add(text);
}
// adapter.notifyDataSetChanged();//注销,否则一开始看到的是第二页.
}
private View.OnClickListener mClicker=new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.rb1:
pageSv.gotoPage(0);
break;
case R.id.rb2:
pageSv.gotoPage(1);
break;
default:
break;
}
}
};
}
最后,好人做到底,贴上项目源码下载链接.
http://download.csdn.net/detail/keanta/9691844
不好意思,要了2分,因为,我需要点分...~~~^_^~~~