package com.uitest.administrator.shoulderby.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.uitest.administrator.shoulderby.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* 主页
* Created by Administrator on 2017/12/6 0006.
*/
public class HomeFragment extends Fragment {
@Bind(R.id.tv_common_title)
TextView tvCommonTitle;
@Bind(R.id.vp_home)
ViewPager vpHome;
@Bind(R.id.group)
RadioGroup group;
@Bind(R.id.iv_home_icon1)
ImageView ivHomeIcon1;
@Bind(R.id.tv_home_icon1)
TextView tvHomeIcon1;
@Bind(R.id.iv_home_icon2)
ImageView ivHomeIcon2;
@Bind(R.id.tv_home_icon2)
TextView tvHomeIcon2;
@Bind(R.id.iv_home_icon3)
ImageView ivHomeIcon3;
@Bind(R.id.tv_home_icon3)
TextView tvHomeIcon3;
@Bind(R.id.iv_home_icon4)
ImageView ivHomeIcon4;
@Bind(R.id.tv_home_icon4)
TextView tvHomeIcon4;
@Bind(R.id.iv_home_bigshow)
ImageView ivHomeBigshow;
@Bind(R.id.tv_home_hot)
TextView tvHomeHot;
@Bind(R.id.iv_home_hot1)
ImageView ivHomeHot1;
@Bind(R.id.iv_home_hot2)
ImageView ivHomeHot2;
@Bind(R.id.iv_home_hot3)
ImageView ivHomeHot3;
//图片资源
private int[] imageIds = {R.drawable.pic_home_longterm, R.drawable.pic_home_quickly, R.drawable.pic_home_shortterm};
//存放图片的数组
private List<ImageView> mList;
//当前索引位置以及上一个索引位置
private int index = 0, preIndex = 0;
//是否需要轮播标志
private boolean isContinue = true;
//定时器,用于实现轮播
private Timer timer;
protected Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (getActivity() != null) {
switch (msg.what) {
case 1:
index++;
System.out.println("==========index: " + index);
vpHome.setCurrentItem(index);
}
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment_home, null);
ButterKnife.bind(this, view);
initTitle();
mList = new ArrayList<>();
vpHome.setAdapter(pagerAdapter);
vpHome.addOnPageChangeListener(onPageChangeListener);
vpHome.setOnTouchListener(onTouchListener);
initRadioButton(imageIds.length);
intiTimer(); //初始化timer 定时器
return view;
}
private void intiTimer() {
timer = new Timer();//创建Timer对象
if (getActivity() == null) {
timer.cancel();
} else {
//执行定时任务
timer.schedule(new TimerTask() {
@Override
public void run() {
//首先判断是否需要轮播,是的话我们才发消息
if (isContinue) {
mHandler.sendEmptyMessage(1);
}
}
}, 3000, 3000);//延迟2秒,每隔2秒发一次消息
}
}
public void initTitle() {
tvCommonTitle.setText("首页");
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
/**
* 根据图片个数初始化按钮
*
* @param length
*/
private void initRadioButton(int length) {
for (int i = 0; i < length; i++) {
ImageView imageview = new ImageView(getActivity());
imageview.setImageResource(R.drawable.rg_selector);//设置背景选择器
imageview.setPadding(20, 0, 0, 0);//设置每个按钮之间的间距
//将按钮依次添加到RadioGroup中
group.addView(imageview, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//默认选中第一个按钮,因为默认显示第一张图片
group.getChildAt(0).setEnabled(false);
}
}
/**
* 根据当前触摸事件判断是否要轮播
*/
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;
}
};
/**
* 根据当前选中的页面设置按钮的选中
*/
ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
index = position;//当前位置赋值给索引
setCurrentDot(index % imageIds.length);
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
/**
* 设置对应位置按钮的状态
*
* @param i 当前位置
*/
private void setCurrentDot(int i) {
if (group.getChildAt(i) != null) {
group.getChildAt(i).setEnabled(false);//当前按钮选中
}
if (group.getChildAt(preIndex) != null) {
group.getChildAt(preIndex).setEnabled(true);//上一个取消选中
preIndex = i;//当前位置变为上一个,继续下次轮播
}
}
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;
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) {
container.removeView(mList.get(position));
}
};
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
由andriod Studio开发的一款大学生兼职招聘App (114个子文件)
.gitignore 7B
build.gradle 1KB
HomeFragment.java 7KB
MainActivity.java 7KB
DBHelper.java 3KB
MeFragment.java 3KB
MsgAdapter.java 3KB
ChatFragment.java 2KB
JobFragment.java 2KB
ChatViewActivity.java 2KB
ChatAdapter.java 2KB
JobAdapter.java 2KB
WelcomeActivity.java 2KB
LoginActivity.java 1KB
job.java 1KB
Chat_item.java 1KB
ExampleInstrumentedTest.java 771B
BaseFragmeny.java 718B
Msg.java 533B
ExampleUnitTest.java 413B
SettingActivity.java 412B
background.JPG 389KB
head1.jpg 11KB
head2.jpg 10KB
me_photo.png 765KB
me_info_background.png 163KB
icon_launcher-web.png 150KB
icon-web.png 114KB
icon_foreground.png 63KB
head4.png 51KB
pic_home_shortterm.png 48KB
start_background.png 46KB
pic_home_quickly.png 44KB
pic_home_longterm.png 40KB
icon_foreground.png 38KB
pic_home_suggest.png 31KB
job_item_pic.png 31KB
icon_launcher.png 29KB
icon_round.png 29KB
head3.png 22KB
icon.png 22KB
icon_foreground.png 19KB
pic_home_gg1.png 19KB
pic_home_gg2.png 19KB
pic_home_gg3.png 18KB
icon_round.png 18KB
icon_launcher.png 18KB
ic_launcher_round.png 15KB
icon.png 13KB
icon_foreground.png 11KB
ic_launcher_round.png 10KB
icon_round.png 10KB
icon_launcher.png 9KB
ic_launcher.png 9KB
ic_launcher_round.png 7KB
icon.png 7KB
ic_launcher.png 6KB
icon_round.png 6KB
icon_launcher.png 6KB
icon_foreground.png 6KB
ic_launcher_round.png 5KB
ic_launcher.png 4KB
icon.png 4KB
icon_round.png 3KB
icon_launcher.png 3KB
ic_launcher.png 3KB
ic_launcher_round.png 3KB
icon.png 3KB
icon_home_bt1.png 2KB
left_messages.9.png 2KB
ic_launcher.png 2KB
right_messages.9.png 2KB
bottom03.png 2KB
icon_home_bt4.png 2KB
bottom05.png 2KB
bottom04.png 1KB
bottom06.png 1KB
bottom01.png 948B
icon_home_bt3.png 704B
bottom02.png 642B
me_item_background.png 483B
bottom07.png 323B
bottom08.png 249B
icon_home_bt2.png 236B
proguard-rules.pro 751B
fragment_home.xml 9KB
fragment_me.xml 8KB
ic_launcher_background.xml 5KB
icon_background.xml 5KB
icon.xml 5KB
main_bottom.xml 4KB
activity_login.xml 2KB
fragment_chat.xml 2KB
msg_item.xml 2KB
ic_launcher_foreground.xml 2KB
activity_welcome.xml 2KB
item_job.xml 2KB
item_chat_chat.xml 1KB
activity_chat_view.xml 1KB
AndroidManifest.xml 980B
共 114 条
- 1
- 2
Kingmeo
- 粉丝: 12
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页