編輯:關於android開發
ViewPager是安卓App很常用的工具類,通常是用來設置界面導航,比如微信,QQ都是使用這種,還有就是新聞類App,很常見
比如:
這就是一個典型的例子。
復習一下這個知識點。
大致步驟和之前的ListView差不多。
1.構造適配器
2.數據源
3.加載適配器
activity_main.xml
ViewPager使用v4的兼容包
view1-view4.xml
自定義適配器
MyPagerAdapter.java
package com.xieth.as.againviewpagerdemo;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by YR on 2016/04/05.
*/
public class MyPagerAdapter extends PagerAdapter{
private List viewList = null;
public MyPagerAdapter(List viewList) {
this.viewList = viewList;
}
// 實例化一個頁卡
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
// 銷毀一個頁卡
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
MainActivity.java
package com.xieth.as.againviewpagerdemo;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager pager = null;
private MyPagerAdapter adapter = null;
private View[] views = new View[4];
private int[] viewId = {R.layout.view1, R.layout.view2, R.layout.view3, R.layout.view4};
//數據源
private List viewList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
adapter = new MyPagerAdapter(viewList);
pager.setAdapter(adapter);
}
private void initViews() {
pager = (ViewPager) findViewById(R.id.id_viewpager);
viewList = new ArrayList<>();
for (int i = 0; i < viewId.length; i++) {
views[i] = View.inflate(this, viewId[i], null);
viewList.add(views[i]);
}
}
}
運行: MainActivity.java 運行: 可以把下面長的短線去掉: 運行: 除了這樣加載,還可以使用Fragment作為載體。 自定義適配器 MainActivity.java 運行: 記得設置事件: 運行: 完整代碼: OK。
這是一個簡單的例子,然後可以在上面添加標題欄。
使用android.support.v4.view.PagerTabStrip<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjxiciAvPg0KPHN0cm9uZz5hY3Rpdml0eV9tYWluLnhtbDwvc3Ryb25nPjwvcD4NCjxwcmUgY2xhc3M9"brush:java;">
package com.xieth.as.againviewpagerdemo;
import android.os.Bundle;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager pager = null;
private MyPagerAdapter adapter = null;
private View[] views = new View[4];
private int[] viewId = {R.layout.view1, R.layout.view2, R.layout.view3, R.layout.view4};
private String[] titleStr = {"第一頁", "第二頁", "第三頁", "第四頁"};
//數據源
private List
tab.setDrawFullUnderline(false);
fragment1-fragment4.java
id改一下即可
package com.xieth.as.againviewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by YR on 2016/04/05.
*/
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.view1, container, false);
}
}
MyFragmentPagerAdapter.java
package com.xieth.as.againviewpagerdemo;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by YR on 2016/04/05.
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
//數據源
private List
package com.xieth.as.againviewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager pager = null;
private MyPagerAdapter adapter = null;
private View[] views = new View[4];
private int[] viewId = {R.layout.view1, R.layout.view2, R.layout.view3, R.layout.view4};
private String[] titleStr = {"第一頁", "第二頁", "第三頁", "第四頁"};
//數據源
private List
基本上是一致的:
然後可以打印一下,獲取當前頁面是第幾個頁面:
需要實現ViewPager.OnPageChangeListener接口
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Toast.makeText(this, "這是第" + (position + 1) + "個頁面", Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrollStateChanged(int state) {
}
pager.setOnPageChangeListener(this);
package com.xieth.as.againviewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
private ViewPager pager = null;
private MyPagerAdapter adapter = null;
private View[] views = new View[4];
private int[] viewId = {R.layout.view1, R.layout.view2, R.layout.view3, R.layout.view4};
private String[] titleStr = {"第一頁", "第二頁", "第三頁", "第四頁"};
//數據源
private List
記錄一下。
[android] 練習viewpagerindicator的使用(二),viewpagerindicator主要還是想實現滑動的tab切換效果 MainActivit
android WebView控件顯示網頁,androidwebview有時需要app裡面顯示網頁,而不調用其他浏覽器浏覽網頁,那麼這時就需要WebView控件。這個控件
Volley 源碼解析,volley源碼解析1. 功能介紹 1.1. Volley Volley 是 Google 推出的 Android 異步網絡請求框架和圖片加載框架
Intent(三)向下一個活動傳遞數據,intent傳遞 向下傳遞活動很簡單,可以我采用putExtra()方法的重載,把
使用Eclipse開發,Java Compiler中Annotation