安卓开发之viewpager学习(头条显示)
activity_luancher.xml代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FCF2E4" > <android.support.v4.view.ViewPager
android:id="@+id/viewpager_launcher"
android:layout_width="match_parent"
android:layout_height="match_parent"/> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <LinearLayout
android:id="@+id/viewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" />
</RelativeLayout> </RelativeLayout>
android.support.v4.view.ViewPager是在xml加入一个viewpager控件。
activity_luancher_pager_item.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/tutorial_1"/> <TextView
android:id="@+id/tv_start_headlines"
android:layout_gravity="bottom|center_horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginBottom="60dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:background="@drawable/start_headlines_bg"
android:textColor="@color/launcher_item_select"
android:textSize="16sp"
android:visibility="invisible"
android:text="开启我的头条"/> </FrameLayout>
这是每个viewpager页中的内容。
viewpager的Adapter提供图片等信息:
LauncherPagerAdapter.java代码:
package com.ansen.developerheadlines.adapter; import java.util.ArrayList;
import java.util.List; import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView; import com.ansen.developerheadlines.R;
import com.ansen.developerheadlines.iview.ILauncherView; /**
* ViewPager适配器
* @author ansen
* @create time 2016-04-15
*/
public class LauncherPagerAdapter extends PagerAdapter implements OnClickListener{
private ILauncherView launcherView; private List<View> views;
//每页显示的图片
private int[] images=new int[]{R.drawable.tutorial_1,R.drawable.tutorial_2,R.drawable.tutorial_3,R.drawable.tutorial_4}; public LauncherPagerAdapter(Context context,ILauncherView launcherView){
views=new ArrayList<View>();
this.launcherView=launcherView;
//初始化每页显示的View
for(int i=0;i<images.length;i++){
View item=LayoutInflater.from(context).inflate(R.layout.activity_luancher_pager_item, null);
ImageView imageview=(ImageView) item.findViewById(R.id.imageview);
imageview.setImageResource(images[i]);
item.findViewById(R.id.tv_start_headlines).setOnClickListener(this);
views.add(item);
}
} public List<View> getViews() {
return views;
} @Override
public int getCount() {
return views == null ? 0 : views.size();
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
} @Override
public void destroyItem(ViewGroup container, int position, Object object){
((ViewPager) container).removeView(views.get(position));
} @Override
public Object instantiateItem(ViewGroup container, int position) {
((ViewPager) container).addView(views.get(position), 0);
return views.get(position);
} @Override
public void onClick(View v) {
launcherView.gotoMain();
} }
先将图片存入list<view>动态数组中(当然图片已经绑定在了各个view中)。
item.findViewById(R.id.tv_start_headlines).setOnClickListener(this);实现最后的页面的"开启头条"跳转,在前几个页面设置的invisible。 viewpager的adapter在切换view时,会先调用destroyitem函数,销毁显示的对象(view),然后调用instantiateItem新建一个需要显示的对象(view). LauncherActivity.java代码:
package com.ansen.developerheadlines.activity; import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; import com.ansen.developerheadlines.R;
import com.ansen.developerheadlines.adapter.LauncherPagerAdapter;
import com.ansen.developerheadlines.iview.ILauncherView; /**
* 第一次启动页面
*
* @author Ansen
* @create time 2016-04-15
*/
@SuppressLint("ResourceAsColor")
public class LauncherActivity extends FragmentActivity implements ILauncherView {
private ViewPager viewpagerLauncher;
private LauncherPagerAdapter adapter; private ImageView[] tips; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_luancher); // if(!isFirst()){
// gotoMain();
// }
viewpagerLauncher = (ViewPager) findViewById(R.id.viewpager_launcher);
adapter = new LauncherPagerAdapter(this, this);
viewpagerLauncher.setOffscreenPageLimit(2);
viewpagerLauncher.setCurrentItem(0);
viewpagerLauncher.setAdapter(adapter);
viewpagerLauncher.setOnPageChangeListener(changeListener);
ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);// 初始化底部显示控件
tips = new ImageView[4];
for (int i = 0; i < tips.length; i++){
ImageView imageView = new ImageView(this);
if (i == 0) {
imageView.setBackgroundResource(R.drawable.page_indicator_focused);
} else {
imageView.setBackgroundResource(R.drawable.page_indicator_unfocused);
}
tips[i] = imageView;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layoutParams.leftMargin = 10;// 设置点点点view的左边距
layoutParams.rightMargin = 10;// 设置点点点view的右边距
group.addView(imageView, layoutParams);
}
} private OnPageChangeListener changeListener = new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageSelected(int index) {
setImageBackground(index);// 改变点点点的切换效果 TextView tvStartHeadlines = (TextView) adapter.getViews().get(index).findViewById(R.id.tv_start_headlines);
if (index == tips.length - 1) {// 最后一个
tvStartHeadlines.setVisibility(View.VISIBLE);
} else {
tvStartHeadlines.setVisibility(View.INVISIBLE);
}
}
}; /**
* 改变点点点的切换效果
* @param selectItems
*/
private void setImageBackground(int selectItems) {
for (int i = 0; i < tips.length; i++) {
if (i == selectItems) {
tips[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
tips[i].setBackgroundResource(R.drawable.page_indicator_unfocused);
}
}
} @Override
public void gotoMain() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} private boolean isFirst() {
SharedPreferences setting = getSharedPreferences("headlines", 0);
Boolean user_first = setting.getBoolean("FIRST", true);
if (user_first) {// 第一次
setting.edit().putBoolean("FIRST", false).commit();
return true;
} else {
return false;
}
}
}
ImageView[] tips是图片下面的转动条,设置事件随图片的变动而变动。
效果图:![]()
安卓开发之viewpager学习(头条显示)的更多相关文章
- 安卓开发之Java学习
Java之素数(这里附上王智超大佬的博客地址)https://blog.csdn.net/weixin_43862765/article/details/103311286
- Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab
今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...
- 安卓开发之ListAdapter(二)
今天我们来学习一下ArrayAdapter: ArrayAdapter是安卓中最简单的适配器.使用ArrayAdapter(数组适配器),需要把数据源存 放至数组里面来显示. •构造函数: publi ...
- Android开发之ViewPager
什么是ViewPager? ViewPager是安卓3.0之后提供的新特性,继承自ViewGroup,专门用以实现左右滑动切换View的效果. 如果想向下兼容就必须要android-support-v ...
- 安卓开发之Toolbar
根据官网的教程,发现实现与预期不一致,查看相关资料自己整理了一下(官网开发文档:https://developer.android.com/training/appbar/setting-up.htm ...
- IOS开发之XCode学习011:UISwitch控件
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 1.定义UIswitch控件,添加UIswitc ...
- IOS开发之XCode学习009:UIViewController使用
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 通过点击屏幕事件,调用ViewController ...
- IOS开发之XCode学习008:UIViewController基础
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 红色框选部分用A代替,AppDelegate类在程序框架启动时,如果在i ...
- IOS开发之XCode学习007:UIWindow对象
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm #import "AppDelegate.h" @i ...
随机推荐
- bootstrap真是个好东西
之前就知道有bootstrap这么个东东,但是因为本身不做web,也就没有仔细了解.这次一个项目合作方使用django和bootstrap做的,有机会接触了一些,感觉确实非常好! 今天下午利用一个下午 ...
- (转载)Mac和iOS开发资源汇总—更新于2013-07-19
(转载)http://beyondvincent.com/2013/07/18/resources-for-mac-and-ios-developers/ 小引 本文主要汇集一些苹果开发的资源,会经常 ...
- 数据结构(分块):[HZOI 2015]easy seq
[题目描述] 给定一个序列,下标从0开始,分别为a0,a1,a2...an−1,有m个询问,每次给出l和r,求满足ai=aj且l<=i<=j<=r时j−i的最大值 本题强制在线,l和 ...
- 使用Struts2开发Java Web应用程序(目录)
连接地址 http://blog.csdn.net/struts2/article/details/1721752
- STM32F072B-DISCO 深入研究 中断系统
STM32F072B-DISCO 是我认为性价比最高的一款CPU的demo系统,以前一直在用PIC的CPU但最近几年ST异军突起,几次课题查找芯片无一例外都是ST,像USB,CAN,ZIGBEE等,S ...
- c++学习笔记(2)类的声名与实现的分离及内联函数
一.类的声名与实现的分离: 和c函数声明与实现分离类似 有.h : 类的声明 .cpp : 类的实现 在在一个类的cpp中应该包含本类的.h文件 在cpp中类的使用:例: //Circle类 //Ci ...
- spring中解析xml
解析xml有SAX,Stax,dom等方式,那么spring中是如何解析xml文件的呢? Document doc = this.documentLoader.loadDocument( inputS ...
- python数据类型和3个重要函数
Python中所有变量都是值的引用,也就说变量通过绑定的方式指向其值. 而这里说的不可变指的是值的不可变. 对于不可变类型的变量,如果要更改变量,则会创建一个新值,把变量绑定到新值上,而旧值如果没有被 ...
- Tomcat8 配置NIO
conf/server.xml 打开:<Executor name="tomcatThreadPool" namePrefix="catalina-exec-&qu ...
- postgresql的/d命令
ostgreSQL-psql常用命令 文章索引 [隐藏] \d命令 \d命令 1 2 3 格式: \d [ pattern ] \d [ pattern ] + 该命令将显示每个匹配关系(表,视图 ...