本文实例讲述了Android编程实现ListView头部ViewPager广告轮询图效果。分享给大家供大家参考,具体如下:
之前看了别人的一些软件,发现其广告图轮询的时候,那个广告感觉和ViewPager的效果不太一样,后来也查了一下,是因为时间问题,找了一些资料,自己也实践一下。
1、为了解决ListView头部加ViewPager滑动冲突问题,必须自定义ListView,重写里面的onInterceptTouchEvent方法,ListView代码如下:
package com.example.testa; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ListView; public class DefinedListView extends ListView { public DefinedListView(Context context) { super(context); } public DefinedListView(Context context,AttributeSet attrs) { super(context,attrs); } public DefinedListView(Context context,AttributeSet attrs,int defStyle) { super(context,attrs,defStyle); } private float xdistance,ydistance,xLast,yLast; @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: xdistance = ydistance = 0f; xLast = ev.getX(); yLast = ev.getY(); break; case MotionEvent.ACTION_MOVE: final float curX = ev.getX(); final float curY = ev.getY(); xdistance += Math.abs(curX - xLast); ydistance += Math.abs(curY - yLast); xLast = curX; yLast = curY; if (xdistance > ydistance) { return false; } } return super.onInterceptTouchEvent(ev); } }
2、为了让广告轮询的速度减慢,必须重新设置ViewPager的Scroller,自定义的Scroller如下:
package com.example.testa; import java.lang.reflect.Field; import android.annotation.SuppressLint; import android.content.Context; import android.os.Handler; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.animation.Interpolator; import android.widget.Scroller; public class DefinedScroller extends Scroller { private int DURATION_TIME = 1000; private boolean shouldAutoScroll = true; private ViewPager viewPager = null; @SuppressLint("HandlerLeak") private final Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if (shouldAutoScroll) { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); handler.sendEmptyMessageDelayed(0,2000); } }; }; public DefinedScroller(Context context) { super(context); } public DefinedScroller(Context context,Interpolator interpolator) { super(context,interpolator); } private View initViewPager(Context context) { View view = LayoutInflater.from(context).inflate(R.layout.view_pager_layout,null); this.viewPager = (ViewPager) view.findViewById(R.id.viewpager); this.viewPager.setoffscreenPageLimit(2); this.viewPager.setAdapter(new MyFragmentPagerAdapter(((FragmentActivity) context).getSupportFragmentManager())); return view; } public ViewPager getViewPager() { return viewPager; } public View initView(Context context) { View view = initViewPager(context); this.viewPager.setonTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v,MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { shouldAutoScroll = true; handler.sendEmptyMessageDelayed(0,2000); } else { handler.removeMessages(0); shouldAutoScroll = false; } return false; } }); try { Field mScroller = ViewPager.class.getDeclaredField("mScroller"); mScroller.setAccessible(true); mScroller.set(viewPager,this); } catch (NoSuchFieldException e) { } catch (IllegalArgumentException e) { } catch (illegalaccessexception e) { } handler.sendEmptyMessageDelayed(0,2000); return view; } public void setShouldAutoScroll() { handler.removeMessages(0); if (viewPager.isShown()) { shouldAutoScroll = true; handler.sendEmptyMessageDelayed(0,2000); } else { shouldAutoScroll = false; } } @Override public void startScroll(int startX,int startY,int dx,int dy,int duration) { super.startScroll(startX,startY,dx,dy,shouldAutoScroll ? DURATION_TIME : duration); } }
关键是startScroll方法啦
3、其他测试类,
(1)TestFragment:
package com.example.testa; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; public class TestFragment extends Fragment { public int imageResource = 0; public static TestFragment newInstance(int imageResource) { TestFragment newFragment = new TestFragment(); newFragment.imageResource = imageResource; return newFragment; } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.pager_layout,null); ImageView textView = (ImageView) view.findViewById(R.id.image); textView.setimageResource(imageResource); return view; } }
(2)ViewPager的Adapter类:
package com.example.testa; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter { public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public Fragment getItem(int arg0) { switch (arg0 % 4) { case 0: return TestFragment.newInstance(R.drawable.a1); case 1: return TestFragment.newInstance(R.drawable.default_wh); case 2: return TestFragment.newInstance(R.drawable.default_ww); case 3: return TestFragment.newInstance(R.drawable.test); default: return TestFragment.newInstance(R.drawable.a1); } } }
(3)测试的Activity:
package com.example.testa; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.TextView; public class MainActivity extends FragmentActivity { private DefinedListView listView = null; private List<String> list = null; private DefinedScroller definedScroller = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (DefinedListView) findViewById(R.id.listview); initListData(); initViewPager(); listView.setAdapter(new listadapter()); listView.setonItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent,View view,int position,long id) { definedScroller.getViewPager().setCurrentItem(definedScroller.getViewPager().getCurrentItem() + 1); } }); listView.setonScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view,int scrollState) { if (scrollState == SCROLL_STATE_IDLE) { definedScroller.setShouldAutoScroll(); } } @Override public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount) { } }); } private void initListData() { list = new ArrayList<String>(); for (int i = 1; i <= 20; ++i) { list.add("test" + i); } } private void initViewPager() { definedScroller = new DefinedScroller(this); listView.addHeaderView(definedScroller.initView(this)); } private class listadapter extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position,View convertView,ViewGroup parent) { TextView textView = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.text_view_layout,null); textView.setPadding(20,20,20); textView.setText(list.get(position)); return textView; } } }
4、布局文件就算了吧
上面有些是在别的地方看到的,很久了,忘记在哪里看到的了,就写不上源地址了,敬请见谅。
希望本文所述对大家Android程序设计有所帮助。