ViewPager 首次安装软件加载图片介绍并判断是否第一次,如果第一次展示图片,否则直接进入软件
XML文件为简单的ListView和展示的XML文件为简单的ImageView
package com.example.mothtree_day1_r; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; public class MainActivity extends Activity { ViewPager pager; Button btn; View view1, view2, view3; List<View> list; Boolean flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = (ViewPager) findViewById(R.id.pager); btn = (Button) findViewById(R.id.but); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, MyActivity.class); startActivity(intent); } }); SharedPreferences shared = getSharedPreferences("text", Activity.MODE_PRIVATE); flag = shared.getBoolean("bool", false); guite(); list = new ArrayList<View>(); LayoutInflater lf = LayoutInflater.from(this); view1 = lf.inflate(R.layout.pager1, null); view2 = lf.inflate(R.layout.pager2, null); view3 = lf.inflate(R.layout.pager3, null); list.add(view1); list.add(view2); list.add(view3); pager.setAdapter(new myadapter()); pager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { if (arg0 == 2) { btn.setVisibility(View.VISIBLE); } else { btn.setVisibility(View.INVISIBLE); } } @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 guite() { if (!flag) { SharedPreferences shared = getSharedPreferences("text", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = shared.edit(); editor.putBoolean("bool", true); editor.commit(); pager.setVisibility(View.VISIBLE); } else { pager.setVisibility(View.INVISIBLE); Intent intent = new Intent(MainActivity.this, MyActivity.class); startActivity(intent); } } class myadapter extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(list.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(list.get(position)); return list.get(position); } } }