一、定义四的个Fragment
只是测试,不需要太多数据 ,所以简单一些
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"
tools:context=".MainActivity"
android:background="@drawable/new_comer_guide_fg_1">
</RelativeLayout>
选了四张图片以大众点评为例子:
二、主布局文件也不需要太多,只需要一个viewPager,当然为了,增强用户的体现底部增加了四个圆点
<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"
tools:context=".GuideActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_guide_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/cpi_guide_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
/>
</RelativeLayout>
第一个控件,也可以使用LinearLayout,代码中添加四个圆点
三、布局文件还是非常简单的,接下来就是Java代码里的东西了,不用麻烦也是很简单的 ,当然使用上面这种框架也有缺点,就是下面圆点点击事件无法添加
public class GuideActivity extends FragmentActivity {
ViewPager viewPager;
MyPagerAdapter adapter;
CirclePageIndicator cpi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
viewPager = (ViewPager) findViewById(R.id.vp_guide_viewpager);
cpi = (CirclePageIndicator) findViewById(R.id.cpi_guide_indicator);
adapter = new MyPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
//CirclePageIndicator与ViewPager绑定的代码
//一定要写在ViewPager绑定适配器之后
cpi.setViewPager(viewPager);
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
cpi.setRadius(px);//px
cpi.setFillColor(Color.parseColor("#ffff6633"));
cpi.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
if(arg0==3){
cpi.setVisibility(View.INVISIBLE);
}else{
cpi.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageScrollStateChanged(int arg0) {}
});
}
}
四、还有第二种 显示 方式
布局文件
fragment布局文件都 一样,就是主布局文件有点不一样
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="16dp"
android:layout_marginTop="-54dp"
android:orientation="horizontal"
android:gravity="center" >
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_marginTop="-54dp"
android:visibility="gone"
android:layout_gravity="center_horizontal"
android:text="下一页" />
</LinearLayout>
四(二)java代码稍微麻烦一点,但是增强了用户体现,使用范围也广
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends FragmentActivity {
ViewPager viewPager;
List<Fragment> allFragment ;
LinearLayout layout ;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return allFragment.size();
}
@Override
public Fragment getItem(int arg0) {
return allFragment.get(arg0);
}
};
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
int befor;
@Override
public void onPageSelected(int arg0) {
layout.getChildAt(arg0).setEnabled(false);
layout.getChildAt(befor).setEnabled(true);
befor = arg0;
if(arg0 == 3){
layout.setVisibility(View.GONE);
button.setVisibility(View.VISIBLE);
}else{
layout.setVisibility(View.VISIBLE);
button.setVisibility(View.GONE);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void initView() {
viewPager = (ViewPager) findViewById(R.id.viewpager);
allFragment = new ArrayList<Fragment>();
allFragment.add(new OneFragment());
allFragment.add(new TwoFragment());
allFragment.add(new ThreeFragment());
allFragment.add(new FourthFragment());
button = (Button) findViewById(R.id.button);
layout = (LinearLayout) findViewById(R.id.layout);
for(int i=0;i<allFragment.size();i++){
final ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.drawable.selector_iamgeview);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.rightMargin = 10;
params.width = 9;
params.height = 9;
imageView.setLayoutParams(params);
imageView.setTag(i);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem((Integer)imageView.getTag());
}
});
layout.addView(imageView);
}
layout.getChildAt(0).setEnabled(false);
}
}
显示效果也比较好
如果大家有兴趣,请继续关注,以后会有更多多