如何在寻呼机中实现imageview和按钮

时间:2013-12-06 09:57:41

标签: android android-viewpager pager

我想在ImageView中实施ButtonViewPager。但是当我试图实现它时,只是崩溃我的应用程序而我无法实现它,请帮助我,因为我是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的寻呼机xml文件

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

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

</LinearLayout>

2 个答案:

答案 0 :(得分: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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

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

pagerview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

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

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

</RelativeLayout>

对齐

enter image description here

答案 1 :(得分:0)

是的,可以通过类似的方式来完成......

我制作了一个xml布局文件,并将其命名为settings_merge.xml

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

<ImageView
    android:id="@+id/set_settings"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:src="@drawable/share" />

<Button
    android:id="@+id/facebook_share"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/facebook" />

现在,在我的viewpager活动布局中,我使用include标记来扩充上述布局

<FrameLayout
    android:id="@+id/transparentlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

    <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 
            }
        });

你完成了......:)