文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android编程实现ListView头部ViewPager广告轮询图效果

2022-06-06 09:38

关注

本文实例讲述了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程序设计有所帮助。

您可能感兴趣的文章:Android实现自动轮询的RecycleView


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯