Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android編程實現ListView頭部ViewPager廣告輪詢圖效果

Android編程實現ListView頭部ViewPager廣告輪詢圖效果

編輯:Android開發實例

本文實例講述了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, 20); 
   textView.setText(list.get(position)); 
   return textView; 
  } 
 } 
}

4、布局文件就算了吧

上面有些是在別的地方看到的,很久了,忘記在哪裡看到的了,就寫不上源地址了,敬請見諒。

希望本文所述對大家Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved