package com.example.shop.ui.fragment;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Toast;
import com.example.mylibrary.net.BaseNetForJSON;
import com.example.mylibrary.net.NetOverListener;
import com.example.mylibrary.net.http.HttpNetForJSONFactory;
import com.example.mylibrary.util.AnimatorString;
import com.example.shop.Constant;
import com.example.shop.R;
import com.example.shop.base.SCBaseFragment;
import com.example.shop.entity.HomeEntity;
import com.example.shop.ui.activity.LimitBuyActivity;
import com.example.shop.ui.activity.TopicActivity;
import com.example.shop.ui.adapter.AdapterVpHomeFragBanner;
import com.example.shop.ui.adapter.ItemLvHomeFragClasslistAdapter;
import java.util.Timer;
import java.util.TimerTask;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends SCBaseFragment implements ViewPager.OnPageChangeListener, View.OnClickListener, AdapterView.OnItemClickListener {
private LinearLayout mLayoutHomeFragSearch;
private EditText mEdtHomeFragSearch;
private Button mBtHomeFragSearch;
private ViewPager mVpHomeFragBanner;
private ListView mLvHomeFragClasslist;
private Button mBtHomeFragCall;
private boolean mVPCanPlay = true;
private RadioGroup mRgHomeFragBanner;
private int mBannerCount = -1;
private Timer mTimer;
private ScrollView mScrollViewHomeFrag;
// private Handler mHandler = new Handler() {
// @Override
// public void handleMessage(Message msg) {
// super.handleMessage(msg);
// switch (msg.what) {
// case 1:
// mVpHomeFragBanner.setCurrentItem(mVpHomeFragBanner.getCurrentItem()+1);
mHandler.sendEmptyMessageDelayed(1,1500);
// break;
// default:
// break;
// }
// }
// };
@Override
public int setRootView() {
return R.layout.fragment_home;
}
@Override
public void initViews() {
this.setTitleCenter("505特约商城,专卖***");
mLayoutHomeFragSearch = (LinearLayout) mRootView.findViewById(R.id.layout_home_frag_search);
mEdtHomeFragSearch = (EditText) mRootView.findViewById(R.id.edt_home_frag_search);
mBtHomeFragSearch = (Button) mRootView.findViewById(R.id.bt_home_frag_search);
mVpHomeFragBanner = (ViewPager) mRootView.findViewById(R.id.vp_home_frag_banner);
mLvHomeFragClasslist = (ListView) mRootView.findViewById(R.id.lv_home_frag_classlist);
mBtHomeFragCall = (Button) mRootView.findViewById(R.id.bt_home_frag_call);
mRgHomeFragBanner = (RadioGroup) mRootView.findViewById(R.id.rg_home_frag_banner);
mVpHomeFragBanner.addOnPageChangeListener(this);
mScrollViewHomeFrag = (ScrollView) mRootView.findViewById(R.id.scrollView_home_frag);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mBtHomeFragCall, AnimatorString.translationX, -10, 10);
objectAnimator1.setRepeatCount(-1);
objectAnimator1.setRepeatMode(ObjectAnimator.REVERSE);
objectAnimator1.setDuration(500);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mBtHomeFragCall, AnimatorString.scaleY, 1f, 0.6f);
objectAnimator2.setRepeatCount(-1);
objectAnimator2.setRepeatMode(ObjectAnimator.RESTART);
objectAnimator2.setDuration(500);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator1, objectAnimator2);
// animatorSet.start();
mBtHomeFragCall.setOnClickListener(this);
}
@Override
public void initDatas() {
BaseNetForJSON baseNetForJSON = HttpNetForJSONFactory.getNetUtil(Constant.COMMON_NET_METHOD, Constant.HOME_URL, new HomeNetOverLis(), HomeEntity.class);
baseNetForJSON.doNet();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
startActvity(LimitBuyActivity.class);
break;
case 1:
startActvity(TopicActivity.class);
break;
default:
break;
}
}
private class HomeNetOverLis implements NetOverListener<HomeEntity> {
@Override
public void onSuccess(HomeEntity entity) {
initVPBanner(entity);
initLVClassList(entity);
}
@Override
public void onError() {
Toast.makeText(mActivity, "当前网络连接不可用", Toast.LENGTH_SHORT).show();
}
}
private void initLVClassList(HomeEntity entity) {
ItemLvHomeFragClasslistAdapter itemLvHomeFragClasslistAdapter = new ItemLvHomeFragClasslistAdapter(mActivity, entity.getHome_classlist());
mLvHomeFragClasslist.setAdapter(itemLvHomeFragClasslistAdapter);
mLvHomeFragClasslist.setOnItemClickListener(this);
}
/**
* @param entity
*/
private void initVPBanner(HomeEntity entity) {
mBannerCount = entity.getHome_banner().size();
mVpHomeFragBanner.setAdapter(new AdapterVpHomeFragBanner(entity.getHome_banner(), mActivity));
mVpHomeFragBanner.setCurrentItem(25000);
if (mTimer == null) {
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
// mHandler.sendEmptyMessage(1);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mVPCanPlay) {
mVpHomeFragBanner.setCurrentItem(mVpHomeFragBanner.getCurrentItem() + 1);
}
}
});
}
}, 3000, 1500);
}
}
@Override
public void onDestroy() {
super.onDestroy();
mTimer.cancel();
}
@Override
public void onClick(View v) {
try {
String url = "mqqwpa://im/chat?chat_type=wpa&uin=1650983316";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(mActivity, "没哟QQ", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (mBannerCount != -1) {
int realPos = position % mBannerCount;
RadioButton childAt = (RadioButton) mRgHomeFragBanner.getChildAt(realPos);
childAt.setChecked(true);
}
}
@Override
public void onPageScrollStateChanged(int state) {
mVPCanPlay = state == ViewPager.SCROLL_STATE_IDLE;
}
}
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView_home_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:scrollbars="none"
tools:context="com.example.shop.ui.fragment.HomeFragment"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layout_home_frag_search"
android:background="@drawable/home_search2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="45dp">
<EditText
android:layout_marginLeft="35dp"
android:id="@+id/edt_home_frag_search"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#00000000"
android:layout_marginRight="5dp"
android:layout_height="match_parent"/>
<Button
android:id="@+id/bt_home_frag_search"
android:background="#0000"
android:layout_width="70dp"
android:layout_height="match_parent"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:layout_below="@+id/layout_home_frag_search"
android:background="#f00"
android:id="@+id/vp_home_frag_banner"
android:layout_width="match_parent"
android:layout_height="170dp"></android.support.v4.view.ViewPager>
<RadioGroup
android:orientation="horizontal"
android:id="@+id/rg_home_frag_banner"
android:layout_width="100dp"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/vp_home_frag_banner"
android:layout_height="15dp">
<RadioButton
android:layout_width="0dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:clickable="false"
android:background="@drawable/slide_adv_selector"
android:button="@null"
android:layout_height="match_parent"/>
<RadioButton
android:layout_width="0dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:clickable="false"
android:background="@drawable/slide_adv_selector"
android:button="@null"
android:layout_height="match_parent"/>
<RadioButton
android:layout_width="0dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:clickable="false"
android:background="@drawable/slide_adv_selector"
android:button="@null"
android:layout_height="match_parent"/>
<RadioButton
android:layout_width="0dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:clickable="false"
android:background="@drawable/slide_adv_selector"
android:button="@null"
android:layout_height="match_parent"/>
<RadioButton
android:layout_width="0dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:clickable="false"
android:background="@drawable/slide_adv_selector"
android:button="@null"
android:layout_height="match_parent"/>
</RadioGroup>
<com.example.shop.ui.views.MesureListView
android:id="@+id/lv_home_frag_classlist"
android:layout_below="@+id/vp_home_frag_banner"
android:layout_width="match_parent"
android:layout_height="0dp"></com.example.shop.ui.views.MesureListView>
<Button
android:id="@+id/bt_home_frag_call"
android:layout_width="match_parent"
android:text="订购热线,厕所旁边"
android:textColor="#fff"
android:background="@drawable/button_long_selector"
android:layout_below="@+id/lv_home_frag_classlist"
android:layout_margin="10dp"
android:layout_height="50dp"/>
</RelativeLayout>
</ScrollView>
package com.example.shop.ui.adapter;
import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.shop.R;
import com.example.shop.entity.HomeEntity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import java.util.List;
/**
* Created by Administrator on 2016/5/17.
*/
public class AdapterVpHomeFragBanner extends PagerAdapter {
private DisplayImageOptions mOptions;
private List<HomeEntity.HomeBannerEntity> mHomeBannerEntities;
private Activity mActivity;
private LayoutInflater mLayoutInflater;
public AdapterVpHomeFragBanner(List<HomeEntity.HomeBannerEntity> homeBannerEntities, Activity activity) {
mHomeBannerEntities = homeBannerEntities;
mActivity = activity;
mLayoutInflater=mActivity.getLayoutInflater();
mOptions = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.product_loading)
.showImageOnFail(R.drawable.product_loading)
.showImageOnLoading(R.drawable.product_loading)
.resetViewBeforeLoading(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.considerExifParams(false)
.displayer(new FadeInBitmapDisplayer(300))
.build();
}
@Override
public int getCount() {
return 50000;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView= (ImageView) mLayoutInflater.inflate(R.layout.item_vp_home_frag_banner,container,false);
container.addView(imageView);
ImageLoader.getInstance().displayImage(mHomeBannerEntities.get(position%mHomeBannerEntities.size()).getPic(),imageView,mOptions);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
}
|