我正在尝试创建一个可以按以下方式运行的Android应用:
我有一个装有10张图片的ViewPager项目:这不是问题。
现在,当我点击ViewPager中显示的图像时,我必须在ViewPager下面的ImageView字段中显示它。
如何实现将图像从ViewPager复制到ImageView的方法?
这是我的代码:
SecondActivity.java
package com.example.logger;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.view.ViewGroup;
public class SecondActivity extends Activity {
private static final int NUM_PAGES = 10;
private ViewPager mpager;
private MyPagerAdapter mPagerAdapter;
private int [] pics = {R.drawable.android_interfaz_layout_estructura_final, R.drawable.baixa, R.drawable.baixada, R.drawable.greenprogressbar,
R.drawable.layout_keyboard, R.drawable.linerlay, R.drawable.merge1, R.drawable.o2zds, R.drawable.regbd,
R.drawable.s7qrs};
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mPagerAdapter = new MyPagerAdapter();
mpager = (ViewPager) findViewById(R.id.viewer);
mpager.setAdapter (mPagerAdapter);
}
private class MyPagerAdapter extends PagerAdapter
{
@Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return false;
}
@Override
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView (SecondActivity.this);
view.setImageResource (pics[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
public void onClick (View v)
{
// TODO Auto-generated method stub
//aquí anirà la funció per traslladar la image de la gallery a la pantalla
mpager.setVisibility(View.VISIBLE);
mpager.setCurrentItem(v.getId());
}
}
结束.java
这也是我的布局:
activity_second.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<android.support.v4.view.ViewPager
android:padding="16dp"
android:id="@+id/viewer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.70"
/>
<ImageView
android:id="@+id/big_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="viewer"
android:layout_weight="0.30"
/>
</RelativeLayout>
结束.xml
如果你能帮助我,我将非常感激。
您诚挚的,
莫罗。
答案 0 :(得分:1)
您可以在活动中使用名为showImage的函数,如下所示:
public void showImage (int position,View view){
//View is your view that you returned from instantiateItem and position is it's postion in array you can get the image resource id using this position ans set it to the ImageView
}
在适配器中:
private class MyPagerAdapter extends PagerAdapter {
<YourActivityClass> activity;
public MyPagerAdapter(<YourActivityClass> activity){
this.activity=activity;
}
public Object instantiateItem(View collection, int position) {
//return a view for view pager
}
@Override
public void setPrimaryItem(ViewGroup container, int position,
Object object) {
// TODO Auto-generated method stub
super.setPrimaryItem(container, position, object);
activity.showImage(position,(View)object);
}
}
答案 1 :(得分:1)
您可以使用标记在视图中添加其他信息。
@Override
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView (SecondActivity.this);
view.setImageResource (pics[position]);
((ViewPager) collection).addView(view, 0);
//I use Integer, but you can use any object.
view.setTag (Integer.valueOf(pics[position]));
view.setOnClickListener(mPageClickListener);
return view;
}
//ViewPaged don't have itemClickListener.
//You should use own OnClickListener for every created View.
private OnClickListener mPageClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
//Getting tag from view.
Integer picId = (Integer) v.getTag();
// ...
}
};
答案 2 :(得分:0)
您可以保留对instantiateItem
中设置的当前drawable的引用,并在单击该项目时将其设置为您的大图像。