第一次写博客,就写下自己程序的进度吧,目前两个页面
起始页面:package com.example.welcome;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnPageChangeListener {
int[] s={R.drawable.f1,R.drawable.f2,R.drawable.f3,R.drawable.f4};
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button) findViewById(R.id.btn);
setIndicator();
//获得ViewPager
ViewPager vp=(ViewPager)findViewById(R.id.pagerId);
//构建ViewPager
InnerPagerAdapter adapter=new InnerPagerAdapter();
//关联adapter
vp.setAdapter(adapter);
//监听器
vp.setOnPageChangeListener(this);
}
LinearLayout layout;
public void setIndicator(){
//获得
layout=(LinearLayout) findViewById(R.id.pagerIndicator);
for(int i=0;i
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(10, 10);
v.setLayoutParams(params);
v.setBackgroundResource(R.drawable.indicator_selector_1);
layout.addView(v);
}
layout.getChildAt(0).setEnabled(false);
}
//skip跳过
public void onClick(View v){
Intent intnet=new Intent(this,FirstMainActivity.class);
startActivity(intnet);
}
public void onskip(View v){
Intent intnet=new Intent(this,FirstMainActivity.class);
startActivity(intnet);
}
//重写方法
class InnerPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return s.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
//构建item
ImageView v=(ImageView)View.inflate(MainActivity.this, R.layout.v, null);
//set item data to item view
v.setImageResource(s[position]);
v.setScaleType(ScaleType.FIT_XY);
container.addView(v);
return v;
}
//删除指定的item
public void destroyItem(ViewGroup container, int position,Object object) {
container.removeView(
(View)object);
}
}
//页面指示器
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
private int prepos;
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position==s.length-1){
btn.setVisibility(View.VISIBLE);
}else{
btn.setVisibility(View.GONE);
}
layout.getChildAt(position).setEnabled(false);
layout.getChildAt(prepos).setEnabled(true);
prepos=position;
}
}
首页:
package com.example.welcome;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class FirstMainActivity extends Activity {
private List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity_main);
loadData();
setGridView();
}
private void setGridView(){
//1.gridview
GridView gv=(GridView)
findViewById(R.id.gridId);
//2.构建适配器
GridAdapter adapter=new GridAdapter(this, R.layout.f_bg_1,items);
//3.关联adapter
gv.setAdapter(adapter);
}
private void loadData(){
items= new ArrayList
items.add(new GridItem(R.drawable.s_1));
items.add(new GridItem(R.drawable.s_2));
items.add(new GridItem(R.drawable.s_3));
items.add(new GridItem(R.drawable.s_4));
items.add(new GridItem(R.drawable.s_5));
items.add(new GridItem(R.drawable.s_6));
}
class GridAdapter extends ArrayAdapter
public GridAdapter(Context context, int textViewResourceId,List
super(context, textViewResourceId,objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v=View.inflate(getContext(),R.layout.f_bg_1,null);
GridItem item=getItem(position);
ImageView iv=(ImageView) v.findViewById(R.id.logoId);
iv.setImageResource(item.getLogo());
return v;
}
}
class GridItem{
private int logo;
public GridItem(int logo) {
super();
this.logo = logo;
}
public int getLogo() {
return logo;
}
public void setLogo(int logo) {
this.logo = logo;
}
}
}