第一步:XML佈局文件java
activity_main.xmlandroid
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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="com.example.daohangye.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/liner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:gravity="center" android:orientation="horizontal"></LinearLayout> <!--android:visibility="gone"按鈕隱藏--> <Button android:id="@+id/btn" android:text="歡迎" android:visibility="gone" android:layout_gravity="bottom|center" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>
在drawable數據庫
設置兩個佈局:製做導航頁底部圓點app
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <corners android:radius="10dp"></corners> <solid android:color="#ffffff"></solid> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <corners android:radius="10dp"></corners> <solid android:color="#000000"></solid> </shape>
第二部:適配器ide
public class Adapter extends PagerAdapter { private List<ImageView> list; public Adapter(List<ImageView> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position)); } }
第三步:代碼實現佈局
package com.example.daohangye; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ViewPager vp; private LinearLayout liner; private Button btn; private List<ImageView> list; private List<ImageView> yuandian; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取數據庫 SharedPreferences name = getSharedPreferences("name", 0); //獲取數據庫內容 String key = name.getString("key", ""); //判斷是否存在相同內容,存在直接跳轉 if (key.equals("value")){ Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } initView(); initData(); initAdapter(); initListener(); } private void initListener() { //viewpager的滑動監聽 vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for (int i = 0; i < 3; i++) { if (position==i){ yuandian.get(i).setBackgroundResource(R.drawable.shape2); }else { yuandian.get(i).setBackgroundResource(R.drawable.shape); } } if (position==list.size()-1){ //設置顯示 btn.setVisibility(View.VISIBLE); //設置隱藏 liner.setVisibility(View.INVISIBLE); } if (position<list.size()-1){ btn.setVisibility(View.INVISIBLE); liner.setVisibility(View.VISIBLE); } } @Override public void onPageScrollStateChanged(int state) { } }); } private void initAdapter() { Adapter adapter = new Adapter(list); vp.setAdapter(adapter); vp.setCurrentItem(0); } private void initData() { list = new ArrayList<>(); for (int i = 0; i < 3; i++) { ImageView imageView = new ImageView(this); imageView.setImageResource(R.mipmap.ic_launcher); //圖片自適應 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); list.add(imageView); } yuandian = new ArrayList<>(); for (int i = 0; i < 3; i++) { ImageView imageView = new ImageView(this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); //設置圓點屬性 lp.height=10; lp.width=10; lp.leftMargin=18; lp.rightMargin=18; //判斷當前ViewPager索引,而後設置底部圓點樣式 if (i==0){ imageView.setBackgroundResource(R.drawable.shape2); }else { imageView.setBackgroundResource(R.drawable.shape); } imageView.setLayoutParams(lp); liner.addView(imageView); yuandian.add(imageView); } } private void initView() { vp = (ViewPager) findViewById(R.id.vp); liner = (LinearLayout) findViewById(R.id.liner); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn: //設置小型本地數據庫 SharedPreferences sharedPreferences = getSharedPreferences("name",0); //獲取一個操做對象 SharedPreferences.Editor edit = sharedPreferences.edit(); edit.putString("key","value"); edit.commit(); Intent intent = new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); finish(); break; } } }