網站首頁 編程語言 正文
本文實例為大家分享了ViewPager實現圖片切換效果的具體代碼,供大家參考,具體內容如下
xml布局:
<RelativeLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="260dp"> ? ? ? ? <android.support.v4.view.ViewPager ? ? ? ? ? ? android:id="@+id/view_pager" ? ? ? ? ? ? android:background="#000" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" /> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:id="@+id/layout_point" ? ? ? ? ? ? android:background="#00000000" ? ? ? ? ? ? android:gravity="center" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="20dp" ? ? ? ? ? ? android:orientation="horizontal" ? ? ? ? ? ? android:layout_marginBottom="40dp" ? ? ? ? ? ? android:layout_alignParentBottom="true"/> </RelativeLayout>
activity代碼:
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.example.uoso.myapplicationdemo.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
?* ViewPager顯示圖片
?*/
public class Fragment2 extends Fragment{
? ? private ViewPager viewPager;
? ? private LinearLayout layoutPoint;
? ? private String[] imgs = {"1圖片url"
? ? ? ? ? ? ,"2圖片url",
? ? ? ? ? ? "3圖片url"};
? ? @Nullable
? ? @Override
? ? public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
? ? ? ? View view = inflater.inflate(R.layout.fragment2, null);
? ? ? ? viewPager = (ViewPager) view.findViewById(R.id.view_pager);
? ? ? ? layoutPoint = (LinearLayout) view.findViewById(R.id.layout_point); ? ? ??
? ? ? ? initView();
? ? ? ? return view;
? ? }
? ? private void initView(){ ? ? ? ?
? ? ? ? //ViewPager相關
? ? ? ? ViewPagerAdater myAdater = new ViewPagerAdater(getContext());
? ? ? ? viewPager.setAdapter(myAdater);
? ? ? ? viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onPageSelected(int position) {
? ? ? ? ? ? ? ? //在滑動完成后向布局中添加小圓點
? ? ? ? ? ? ? ? setLayoutPoint(position);
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onPageScrollStateChanged(int state) {
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? setLayoutPoint(0);
? ? }
? ? /**
? ? ?* 設置小圓點布局
? ? ?* @param position
? ? ?*/
? ? private void setLayoutPoint(int position){
? ? ? ? layoutPoint.removeAllViews();
? ? ? ? for (int i = 0; i <3 ; i++) {
? ? ? ? ? ? ImageView imageView = new ImageView(getContext());
? ? ? ? ? ? //設置ImageView
? ? ? ? ? ? LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
? ? ? ? ? ? params.setMargins(10,0,10,0);
? ? ? ? ? ? imageView.setLayoutParams(params);
? ? ? ? ? ? //設置小圓點樣式
? ? ? ? ? ? if (position==i){
? ? ? ? ? ? ? ? imageView.setImageDrawable(getResources().getDrawable(R.drawable.point_white));
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? imageView.setImageDrawable(getResources().getDrawable(R.drawable.point_gray));
? ? ? ? ? ? }
? ? ? ? ? ? layoutPoint.addView(imageView);
? ? ? ? }
? ? } ?
? ? public class ViewPagerAdater extends PagerAdapter {
? ? ? ? private Context context;
? ? ? ? public ViewPagerAdater(Context context) {
? ? ? ? ? ? this.context = context;
? ? ? ? }
? ? ? ? @Override
? ? ? ? public int getCount() {
? ? ? ? ? ? return imgs.length;
? ? ? ? }
? ? ? ? @Override
? ? ? ? public Object instantiateItem(ViewGroup container, int position) {
? ? ? ? ? ? ImageView imageView = new ImageView(context);
? ? ? ? ? ? imageView.setScaleType(ImageView.ScaleType.FIT_XY);
? ? ? ? ? ? Glide.with(context).load(imgs[position]).into(imageView);
? ? ? ? ? ? container.addView(imageView);
? ? ? ? ? ? return imageView;
? ? ? ? }
? ? ? ? @Override
? ? ? ? public void destroyItem(ViewGroup container, int position, Object object) {
? ? ? ? ? ? container.removeView((ImageView) object);
? ? ? ? }
? ? ? ? @Override
? ? ? ? public boolean isViewFromObject(View view, Object object) {
? ? ? ? ? ? return view == object;
? ? ? ? }
? ? }
}
小圓點樣式:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <solid android:color="#fddc04"/> ? ? <corners android:radius="5dp"/> </shape>
Glide依賴:
compile 'com.github.bumptech.glide:glide:3.7.0'
原文鏈接:https://blog.csdn.net/An_nAl/article/details/80324509
相關推薦
- 2022-04-04 webpack-插件: plugin的使用(clean-webpack-plugin)
- 2022-02-13 QT 控件 QListWidget 設置所有事件都激發編輯(雙擊, 選擇, 選項變化)
- 2022-06-08 Python使用PyYAML庫讀寫yaml文件的方法_python
- 2023-03-26 C#?cefSharep控件的使用詳情_C#教程
- 2024-04-02 Centos無法獲取IP報No suitable device found for this con
- 2024-02-27 Go 讀取控制臺輸入
- 2022-09-14 python?特殊屬性及方法詳細解析_python
- 2022-05-12 Android textAppearance 使用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支