public class FristFragment extends BaseFragment {
private RelativeLayout shequ_layout, fankui_layout;
private GridView gd_view;
private TextView tv_shequ;
private ImageView iv_chose;
private LinearLayout.LayoutParams params;
// 记录小圆点个数的容器
private List<ImageView> dots = new ArrayList<ImageView>();
private ViewPager iv_lunbo;
//存放图片的数组
private List<ImageView> mList;
//当前索引位置以及上一个索引位置
private static int index = 0, preIndex = 0;
//是否需要轮播标志
private boolean isContinue = true;
//定时器,用于实现轮播
private Timer timer = new Timer();
private MyHandler mHandler;
private LinearLayout ll_dat;
private String[] names_grade = new String[]{"生鲜果蔬", "干果干货", "米面粮油", "酒水饮料", "居家日用","外卖送餐"};
private int[] ids_grade = new int[]{R.drawable.sxgs,R.drawable.gggh,R.drawable.mmly,R.drawable.jsyl,R.drawable.jjry,R.drawable.wmsc};
//图片资源,实际项目需要从网络获取
private int[] imageIds = {R.drawable.ytf_loading,R.drawable.ytf_loading, R.drawable.ytf_loading, R.drawable.ytf_loading};
public static final int RESULTCode=1;
public FristFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//首页标题栏左右两个布局的点击控件
View view=inflater.inflate(R.layout.fragment_frist, container, false);
iv_chose= (ImageView) view.findViewById(R.id.iv_chose);
fankui_layout= (RelativeLayout) view.findViewById(R.id.fankui_layout);
tv_shequ= (TextView) view.findViewById(R.id.tv_shequ);
//首页GradView
gd_view= (GridView) view.findViewById(R.id.gd_view);
iv_lunbo= (ViewPager) view.findViewById(R.id.iv_lunbo);
ll_dat= (LinearLayout) view.findViewById(R.id.ll_dat);
shequ_layout= (RelativeLayout) view.findViewById(R.id.shequ_layout);
shequ_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(),YtfSQChose.class);
startActivity(intent);
}
});
fankui_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(),PingTaiLiuyanActivity.class);
startActivity(intent);
}
});
// Inflate the layout for this fragment
initGrideView();
initData();
addListener();
//让当前图片位于中间某个位置,目的就是为了开始能够左滑
iv_lunbo.setCurrentItem(imageIds.length * 100);
initRadioButton(imageIds.length);
//注意这句和上面那句顺序不能写反,否则会出现第一个圆点无法显示选中状态
startSwitch();
return view;
}
@Override
public void onResume() {
super.onResume();
tv_shequ.setText( YtfSQChose.SetMsg.message);
}
/**
* 构建GradeView的数据源
*/
private void initGrideView(){
List<Map<String,Object> > list=new ArrayList<>();
for (int i = 0; i < names_grade.length; i++) {
Map<String,Object> map=new HashMap<>();
map.put("name",names_grade[i]);
map.put("pig",ids_grade[i]);
list.add(map);
}
String[] from_grade=new String[]{"name","pig"};
int[] to_grade=new int[]{R.id.tv_grade_item, R.id.iv_grade_item};
SimpleAdapter adapter=new SimpleAdapter(getActivity(),list,R.layout.grade_chose_layout,from_grade,to_grade);
gd_view.setAdapter(adapter);
//gradeview点击跳转详情页
gd_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(getActivity(), YtfGoodActivity.class);
// intent.putExtra("url",)
startActivity(intent);
}
});
}
/**
* 初始化数据
*/
public void initData() {
mList = new ArrayList<>();
iv_lunbo.setAdapter(pagerAdapter);
mHandler = new MyHandler(FristFragment.this);
}
public class MyHandler extends Handler {
private WeakReference<FristFragment> weakReference;
public MyHandler(FristFragment fragment) {
weakReference = new WeakReference<>(fragment);
}
@Override
public void handleMessage(Message msg) {
if (weakReference.get() != null) {
index++;
iv_lunbo.setCurrentItem(index);
}
super.handleMessage(msg);
}
}
PagerAdapter pagerAdapter=new PagerAdapter() {
@Override
public int getCount() {
//返回一个比较大的值,目的是为了实现无限轮播
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % imageIds.length;//因为position非常大,而我们需要的position不能大于图片集合长度
//所以在此取余
ImageView imageView = new ImageView(getActivity());
imageView.setImageResource(imageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
mList.add(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
};
@Override
public void onDestroy() {
super.onDestroy();
//页面销毁的时候取消定时器
if (timer != null) {
preIndex = 0;
timer.cancel();
}
}
/**
* 根据图片个数初始化按钮
* @param length 图片所在集合长度
*/
private void initRadioButton(int length) {
for (int i = 0; i < length; i++) {
ImageView imageview = new ImageView(getActivity());
if(i == 0){
imageview.setImageResource(R.drawable.dat_focus);
}else {
imageview.setImageResource(R.drawable.dat_normal);
}
//设置滚动小圆点的大小
params = new LinearLayout.LayoutParams(10, 10);
params.setMargins(15,0,15,0);
ll_dat.addView(imageview,params);
dots.add(imageview);
}
}
/**
* 进行图片轮播
*/
public void startSwitch() {
//执行定时任务
timer.schedule(new TimerTask() {
@Override
public void run() {
//首先判断是否需要轮播,是的话我们才发消息
if (isContinue) {
mHandler.sendEmptyMessage(1);
}
}
}, 3000, 2000);//延迟3秒,每隔2秒发一次消息;
}
/**
* 添加监听
*/
public void addListener() {
iv_lunbo.addOnPageChangeListener(onPageChangeListener);
iv_lunbo.setOnTouchListener(onTouchListener);
}
/**
* 根据当前触摸事件判断是否要轮播
*/
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
//手指按下和划动的时候停止图片的轮播
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
isContinue = false;
break;
default:
isContinue = true;
}
return false;//这里只能返回false,如果返回true,Dwon就会消费掉事件,MOVE无法获得事件,
// 导致图片无法滑动
}
};
/**
* 根据当前选中的页面设置按钮的选中
*/
ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
index = position;//当前位置赋值给索引
for (int i = 0; i < dots.size(); i++) {
ImageView img = dots.get(i);
if (i ==(position % dots.size())) {
img.setImageResource(R.drawable.dat_focus);
LinearLayout.LayoutParams params1=new LinearLayout.LayoutParams(15,15);
params1.setMargins(15,0,15,0);
img.setLayoutParams(params1);
} else {
img.setImageResource(R.drawable.dat_normal);
params.setMargins(15,0,15,0);
img.setLayoutParams(params);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
ViewPager中带自动缩放小圆点的图片轮播
最新推荐文章于 2023-12-17 20:26:26 发布