如何在寻呼机中实现图像视图和按钮

Posted

技术标签:

【中文标题】如何在寻呼机中实现图像视图和按钮【英文标题】:How to implement imageview and button in pager 【发布时间】:2013-12-06 09:57:41 【问题描述】:

我想在ViewPager 中实现ImageViewButton。但是当我尝试实现它时,我的应用程序崩溃了,我无法实现它,请帮助我,因为我是 android 新手。

public class FullScreenImageActivity extends Activity 
private static int NUM_VIEWS = 5;

private MyPagerAdapter adapter;
private ViewPager pager;
int gotbiscuit;
public String TAG = "hello";

@Override
protected void onCreate(Bundle savedInstanceState) 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fullscreen_view);

    Intent i = getIntent();
    int gotbiscuit = i.getExtras().getInt("position");

    adapter = new MyPagerAdapter(this);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);



public class MyPagerAdapter extends PagerAdapter 
    public Integer[] images1 =  R.drawable.i_3, R.drawable.i_4,
            R.drawable.i_6 ;
    public Integer[] images2 =  R.drawable.i_8, R.drawable.i_9,
            R.drawable.i_10 ;

    public Context mContext;

    public MyPagerAdapter(Context context) 
        super();
        this.mContext = context;
        // TODO Auto-generated constructor stub
    

    @Override
    public int getCount() 
        // TODO Auto-generated method stub
        return images1.length;
    

    @Override
    public boolean isViewFromObject(View view, Object object) 
        // TODO Auto-generated method stub
        return view == ((ImageView) object);
    

    @Override
    public void destroyItem(ViewGroup container, int position, Object view) 
        // TODO Auto-generated method stub
        ((ViewPager) container).removeView((ImageView) view);
    

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    public Object instantiateItem(ViewGroup container, int position) 
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(container
                .getContext());
        View view1 = inflater.inflate(R.layout.fullscreen_view, null);
        ImageView view = (ImageView) findViewById(R.id.imgDisplay);
        Button btn = (Button) findViewById(R.id.wallb);
        // view1.setScaleType(ScaleType.CENTER_CROP);

        switch (gotbiscuit) 

        case 0:
            view.setImageResource(images1[position]);
            break;
        case 1:
            view.setImageResource(images2[position]);
            break;
        
        view.setAdjustViewBounds(true);

        ((ViewPager) container).addView(view1, 0);

        return view1;

    



这是我用于 pagerview 的 Pager xml 文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_
        android:layout_ />

</LinearLayout>

【问题讨论】:

【参考方案1】:

这个

int gotbiscuit = i.getExtras().getInt("position"); // local to onCreate

应该

gotbiscuit = i.getExtras().getInt("position"); // already declared

你也有

setContentView(R.layout.fullscreen_view);

并膨胀相同的布局

 View view1 = inflater.inflate(R.layout.fullscreen_view, null);

应该是

setContentView(R.layout.pager_view); // pager xml

根据您的要求修改以下内容

public class MainActivity extends Activity
public void onCreate(Bundle savedInstanceState) 
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager)findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);



class MyPagerAdapter extends PagerAdapter 

public int getCount() 
return 3;


public Object instantiateItem(View collection, int position) 
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;

view = inflater.inflate(R.layout.pagerview, null);
((ViewPager) collection).addView(view, 0);
Button btn = (Button)view.findViewById(R.id.button1);
ImageView iv = (ImageView)view.findViewById(R.id.imageView1);
btn.setOnClickListener(new View.OnClickListener() 

    @Override
    public void onClick(View v) 
        Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
    

);

return view;

public void destroyItem(View arg0, int arg1, Object arg2) 
((ViewPager) arg0).removeView((View) arg2);

public boolean isViewFromObject(View arg0, Object arg1) 
return arg0 == ((View) arg1);



activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity" >

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_
android:layout_/>
</RelativeLayout>

pagerview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_
    android:layout_
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_
    android:layout_
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="176dp"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

捕捉

【讨论】:

【参考方案2】:

是的,它可以通过这样的方式来完成......

我做了一个xml布局文件,命名为settings_merge.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/set_settings"
    android:layout_
    android:layout_
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:src="@drawable/share" />

<Button
    android:id="@+id/facebook_share"
    android:layout_
    android:layout_
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/facebook" />

现在在我的 viewpager 活动布局中,我使用了include 标签来扩展上述布局

<FrameLayout
    android:id="@+id/transparentlayout"
    android:layout_
    android:layout_ >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_
        android:layout_ />

    <include layout="@layout/settings_merge" />
</FrameLayout>

最后在你的PagerActivity

    ImageView set_settings;
Button facebook_share;
    set_settings=(ImageView)findViewById(R.id.set_settings);

          //the onclick listener
        set_settings.setOnClickListener(new OnClickListener() 

            @Override
            public void onClick(View v) 
                // TODO Auto-generated method stub
                //Do whatever you want to do 
            
        );

你就完成了..:)

【讨论】:

以上是关于如何在寻呼机中实现图像视图和按钮的主要内容,如果未能解决你的问题,请参考以下文章

从头开始查看寻呼机图像(片段)滑块

如何在android中实现图像视图的缩放效果?

如何使用按钮从集合视图(网格视图)切换到表格视图(列表视图)?

如何在android中实现视图滑动

如何将按钮放置在图像视图中的特定坐标处

使用视图寻呼机 android 的 Cover Flow 功能