首页 > 代码库 > Activity转换为View和把图片转换为View
Activity转换为View和把图片转换为View
package com.example.viewpager01;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private ViewPager viewPager;
private List<View> viewList;
private View view1, view2, view3; //
//Context context = null;
LocalActivityManager manager = null; //这里要实现把Activity转换为View
private int[] ids = { R.drawable.guide_page_1, R.drawable.guide_page_2,R.drawable.guide_page_3 };//存放图片数组,这里要实现把图片转换为View
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.user_lader);
manager = new LocalActivityManager(this , true);
manager.dispatchCreate(savedInstanceState);
viewList=new ArrayList<View>();
for (int i = 0; i < ids.length; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(ids[i]);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
iv.setLayoutParams(params);
iv.setScaleType(ScaleType.FIT_XY);
viewList.add(iv);
}
Intent intent = new Intent(getApplicationContext(), A.class);
viewList.add(getView("A", intent));
// 1,
viewPager = (ViewPager) findViewById(R.id.view_pager);
// 2,PageAdapter 必须重写的四个函数:
/*
* boolean isViewFromObject(View arg0, Object arg1) int getCount() void
* destroyItem(ViewGroup container, int position,Object object) Object
* instantiateItem(ViewGroup container, int position)
*/
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
if(viewList==null)
{
return 0;
}
else
{
return viewList.size(); //getCount():返回要滑动的VIew的个数
}
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
//return arg0 == arg1; //isViewFromObject():对于这个函数就先不做解说,大家眼下先知道它要这样重写即可了。后面我们会对它进行改写。
//System.out.println("position===isViewFromObject======"+(int)Integer.parseInt(arg1.toString())+(arg0 == viewList.get((int)Integer.parseInt(arg1.toString()))));
return arg0 == viewList.get((int)Integer.parseInt(arg1.toString()));////依据传来的key,找到view,推断与传来的參数View arg0是不是同一个视图
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position)); //做了两件事。第一:将当前视图加入到container中。第二:返回当前View
//return viewList.get(position);
//System.out.println("position===instantiateItem======"+position);
return position; // //把当前新增视图的位置(position)作为Key传过去
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
if(position==1)
{
viewList=null;
}
else
{
//System.out.println("position====destroyItem====="+position);
container.removeView(viewList.get(position)); //从当前container中删除指定位置(position)的View;
}
}
});
}
private View getView(String id, Intent intent) {
return manager.startActivity(id, intent).getDecorView();
}
}
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private ViewPager viewPager;
private List<View> viewList;
private View view1, view2, view3; //
//Context context = null;
LocalActivityManager manager = null; //这里要实现把Activity转换为View
private int[] ids = { R.drawable.guide_page_1, R.drawable.guide_page_2,R.drawable.guide_page_3 };//存放图片数组,这里要实现把图片转换为View
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.user_lader);
manager = new LocalActivityManager(this , true);
manager.dispatchCreate(savedInstanceState);
viewList=new ArrayList<View>();
for (int i = 0; i < ids.length; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(ids[i]);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
iv.setLayoutParams(params);
iv.setScaleType(ScaleType.FIT_XY);
viewList.add(iv);
}
Intent intent = new Intent(getApplicationContext(), A.class);
viewList.add(getView("A", intent));
// 1,
viewPager = (ViewPager) findViewById(R.id.view_pager);
// 2,PageAdapter 必须重写的四个函数:
/*
* boolean isViewFromObject(View arg0, Object arg1) int getCount() void
* destroyItem(ViewGroup container, int position,Object object) Object
* instantiateItem(ViewGroup container, int position)
*/
viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
if(viewList==null)
{
return 0;
}
else
{
return viewList.size(); //getCount():返回要滑动的VIew的个数
}
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
//return arg0 == arg1; //isViewFromObject():对于这个函数就先不做解说,大家眼下先知道它要这样重写即可了。后面我们会对它进行改写。
//System.out.println("position===isViewFromObject======"+(int)Integer.parseInt(arg1.toString())+(arg0 == viewList.get((int)Integer.parseInt(arg1.toString()))));
return arg0 == viewList.get((int)Integer.parseInt(arg1.toString()));////依据传来的key,找到view,推断与传来的參数View arg0是不是同一个视图
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position)); //做了两件事。第一:将当前视图加入到container中。第二:返回当前View
//return viewList.get(position);
//System.out.println("position===instantiateItem======"+position);
return position; // //把当前新增视图的位置(position)作为Key传过去
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
if(position==1)
{
viewList=null;
}
else
{
//System.out.println("position====destroyItem====="+position);
container.removeView(viewList.get(position)); //从当前container中删除指定位置(position)的View;
}
}
});
}
private View getView(String id, Intent intent) {
return manager.startActivity(id, intent).getDecorView();
}
}
Activity转换为View和把图片转换为View
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。