IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> banner轮播 -> 正文阅读

[移动开发]banner轮播

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;
    }
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-02-09 20:49:02  更:2022-02-09 20:49:25 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 14:31:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码