幻灯片时ViewPager活动更改内容

时间:2016-02-17 19:43:51

标签: android android-viewpager fragment

我一直在四处寻找,但我无法弄清楚如何解决这个问题。 我正在使用@Dave Morrissey的子采样缩放图像视图,它是一个很棒的库,它工作得很完美,但我想做一些改动。 对于用户将要幻灯片的每个图像,我想要显示具体的描述。 所以它将是:

Pic1             |Pic2            |Pic3
DescriptionPic1  |DescriptionPic2 |DescriptionPic3

当我打开它时,我可以看到正确描述下面的图片但是当我向左(或向右)滑动时,我总能看到该项目的描述。 发生这种情况,因为方法getItem()被调用两次以使滑块更加平滑。 问题是我希望在每张图片下面显示正确的内容(描述)。 当用户滑动图片时,如何完美展示内容? 任何帮助都非常感谢。 多谢你们 ViewPagerActivity.java

public class ViewPagerActivity extends FragmentActivity {

private ViewPager page;
private Bitmap bmImg1;
private Bitmap bmImg2;
private Bitmap bmImg3;
private String TAG;
ArrayList<Bitmap> IMAGES =new ArrayList<Bitmap>();
String[] descriptionPhoto;

int numpics=1;
private int position_pic;
private String suggested_aperture;
private String suggested_filter;
private String suggested_iso;
private String suggested_shutter;
private String suggested_shot_level;
private String suggested_lens;
private String[] shot_levelPhoto;
private String[] filterPhoto;
private String[] aperturePhoto;
private String[] shutterPhoto;
private String[] isoPhoto;
private String[] focal_lengthPhoto;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_pager);
    FileInputStream fis;
    FileInputStream fis2;
    FileInputStream fis3;
    Bundle intent=getIntent().getExtras();
    descriptionPhoto = intent.getStringArray("descriptionPhoto");

    numpics = (int) intent.get("numpics");
    position_pic = (int) intent.get("position_pic");

    suggested_aperture= intent.getString("suggested_aperture");
    suggested_filter= intent.getString("suggested_filter");
    suggested_iso=  intent.getString("suggested_iso");
    suggested_shutter= intent.getString("suggested_shutter");
    suggested_shot_level= intent.getString("suggested_shot_level");
    suggested_lens= intent.getString("suggested_lens");

    shot_levelPhoto=intent.getStringArray("shot_levelPhoto");
    filterPhoto =intent.getStringArray("filterPhoto");
    aperturePhoto =intent.getStringArray("aperturePhoto");
    shutterPhoto =intent.getStringArray("shutterPhoto");
    isoPhoto =intent.getStringArray("isoPhoto");
    focal_lengthPhoto=intent.getStringArray("focal_lengthPhoto");


    TextView txtaperture_suggested = (TextView) findViewById(R.id.aperture_suggested);
    TextView txtfilter_suggested = (TextView) findViewById(R.id.filter_suggested);
    TextView txtiso_suggested = (TextView) findViewById(R.id.iso_suggested);
    TextView txtlens_suggested = (TextView) findViewById(R.id.lens_suggested);
    TextView txtshutter_suggested = (TextView) findViewById(R.id.shutter_suggested);
    TextView txtshot_levelsuggested = (TextView) findViewById(R.id.shot_levelsuggested);

    if (suggested_shot_level == "1") {
        txtshot_levelsuggested.setText("Easy");
        /*txtshot_levelPhoto2.setText("Easy");
        txtshot_levelPhoto3.setText("Easy");*/

    } else if (suggested_shot_level == "2") {
        txtshot_levelsuggested.setText("Medium");
       /* txtshot_levelPhoto2.setText("Medium");
        txtshot_levelPhoto3.setText("Medium");*/
    } else if (suggested_shot_level == "3") {
        txtshot_levelsuggested.setText("Difficult");
        /*txtshot_levelPhoto2.setText("Difficult");
        txtshot_levelPhoto3.setText("Difficult");*/
    } else {
        txtshot_levelsuggested.setText("Pro");
       /* txtshot_levelPhoto2.setText("Pro");
        txtshot_levelPhoto3.setText("Pro");*/
    }

    txtaperture_suggested.setText(suggested_aperture);
    txtfilter_suggested.setText(suggested_filter);
    txtiso_suggested.setText(suggested_iso);
    txtshutter_suggested.setText(suggested_shutter);
    txtlens_suggested.setText(suggested_lens);

    TextView txtshutterPhoto = (TextView) findViewById(R.id.shutterPhoto);
    TextView txtshutterPhoto2 = (TextView) findViewById(R.id.shutterPhoto2);
    TextView txtshutterPhoto3 = (TextView) findViewById(R.id.shutterPhoto3);

    TextView txtshot_levelPhoto = (TextView) findViewById(R.id.shot_levelPhoto);
    TextView txtshot_levelPhoto2 = (TextView) findViewById(R.id.shot_levelPhoto2);
    TextView txtshot_levelPhoto3 = (TextView) findViewById(R.id.shot_levelPhoto3);

    TextView txtaperturePhoto = (TextView) findViewById(R.id.aperturePhoto);
    TextView txtaperturePhoto2 = (TextView) findViewById(R.id.aperturePhoto2);
    TextView txtaperturePhoto3 = (TextView) findViewById(R.id.aperturePhoto3);

    TextView txtfilterPhoto = (TextView) findViewById(R.id.filterPhoto);
    TextView txtfilterPhoto2 = (TextView) findViewById(R.id.filterPhoto2);
    TextView txtfilterPhoto3 = (TextView) findViewById(R.id.filterPhoto3);

    TextView txtisoPhoto = (TextView) findViewById(R.id.isoPhoto);
    TextView txtisoPhoto2 = (TextView) findViewById(R.id.isoPhoto2);
    TextView txtisoPhoto3 = (TextView) findViewById(R.id.isoPhoto3);

    TextView txtlensPhoto = (TextView) findViewById(R.id.lensPhoto);
    TextView txtlensPhoto2 = (TextView) findViewById(R.id.lensPhoto2);
    TextView txtlensPhoto3 = (TextView) findViewById(R.id.lensPhoto3);

    /*txtshutterPhoto.setText(shutterPhoto[0]);
    if (shutterPhoto.length > 1) {
        txtshutterPhoto2.setText(shutterPhoto[1]);
        if (shutterPhoto.length > 2) {
            txtshutterPhoto3.setText(shutterPhoto[2]);

        }
    }


    txtisoPhoto.setText(isoPhoto[0]);
    if (isoPhoto.length > 1) {
        txtisoPhoto2.setText(isoPhoto[1]);
        if (isoPhoto.length > 2) {
            txtisoPhoto3.setText(isoPhoto[2]);
        }
    }


    txtfilterPhoto.setText(filterPhoto[0]);
    if (filterPhoto.length > 1) {
        txtfilterPhoto2.setText(filterPhoto[1]);
        if ((filterPhoto.length > 2)) {
            txtfilterPhoto3.setText(filterPhoto[2]);
        }
    }


    txtaperturePhoto.setText(aperturePhoto[0]);
    if (aperturePhoto.length > 1) {
        txtaperturePhoto2.setText(aperturePhoto[1]);
        if (aperturePhoto.length > 2) {
            txtaperturePhoto3.setText(aperturePhoto[2]);

        }
    }*/

    try {
        fis = getApplicationContext().openFileInput("bmImg1");
        bmImg1 = BitmapFactory.decodeStream(fis);
        IMAGES.add(bmImg1);
        fis.close();

        /*
        findViewById(R.id.note1).setVisibility(View.VISIBLE);
        TextView descriptionnote = (TextView)findViewById(R.id.note1);
        descriptionnote.setText(descriptionPhoto[0]);
        */
        if(numpics>1){
            fis2 = getApplicationContext().openFileInput("bmImg2");
            bmImg2 = BitmapFactory.decodeStream(fis2);
            IMAGES.add(bmImg2);
            fis2.close();
            /*
            findViewById(R.id.note1).setVisibility(View.INVISIBLE);
            findViewById(R.id.note2).setVisibility(View.VISIBLE);
            TextView descriptionnote2 = (TextView)findViewById(R.id.note2);
            descriptionnote2.setText(descriptionPhoto[1]);
            */

            if(numpics>2){
                fis3 = getApplicationContext().openFileInput("bmImg3");
                bmImg3 = BitmapFactory.decodeStream(fis3);
                IMAGES.add(bmImg3);
                fis3.close();
            /*
                findViewById(R.id.note2).setVisibility(View.INVISIBLE);
                findViewById(R.id.note3).setVisibility(View.VISIBLE);
                TextView descriptionnote3 = (TextView)findViewById(R.id.note3);
                descriptionnote3.setText(descriptionPhoto[2]);
            */
            }
            }
        }
    catch (FileNotFoundException e) {
        Log.d(TAG, "file not found");
        e.printStackTrace();
    }
    catch (IOException e) {
        Log.d(TAG, "io exception");
        e.printStackTrace();
    }

    PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    page = (ViewPager)findViewById(R.id.pager);
    page.setAdapter(pagerAdapter);
    page.setCurrentItem(position_pic);


    if(page.getCurrentItem()==0 ){
        findViewById(R.id.note1).setVisibility(View.VISIBLE);
        findViewById(R.id.note2).setVisibility(View.INVISIBLE);
        findViewById(R.id.note3).setVisibility(View.INVISIBLE);
        TextView descriptionnote = (TextView) findViewById(R.id.note1);
        descriptionnote.setText(descriptionPhoto[0]);

    }
    if (page.getCurrentItem()==1 ) {
        findViewById(R.id.note1).setVisibility(View.INVISIBLE);
        findViewById(R.id.note2).setVisibility(View.VISIBLE);
        findViewById(R.id.note3).setVisibility(View.INVISIBLE);
        TextView descriptionnote2 = (TextView) findViewById(R.id.note2);
        descriptionnote2.setText(descriptionPhoto[1]);

    }

    if (page.getCurrentItem()==2 ) {
        findViewById(R.id.note1).setVisibility(View.INVISIBLE);
        findViewById(R.id.note2).setVisibility(View.INVISIBLE);
        findViewById(R.id.note3).setVisibility(View.VISIBLE);
        TextView descriptionnote3 = (TextView) findViewById(R.id.note3);
        descriptionnote3.setText(descriptionPhoto[2]);
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    finish();
    return true;
}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }
    public int getItemPosition(Object item){
        return POSITION_NONE;
    }

    @Override
    public Fragment getItem(int position) {
        ViewPagerFragment fragment = new ViewPagerFragment();
        fragment.setAsset(IMAGES.get(position));
        return fragment;
    }

    @Override
    public int getCount() {
        return IMAGES.size();

    }
}}

这是ViewPagerFragment

public class ViewPagerFragment extends Fragment {

private static final String BUNDLE_ASSET = "res";

private Bitmap asset;

public ViewPagerFragment() {
}

public void setAsset(Bitmap asset) {
    this.asset = asset;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.view_pager_page, container, false);

   /* if (savedInstanceState != null) {
        if (asset == null && savedInstanceState.containsKey(BUNDLE_ASSET)) {
            asset = savedInstanceState.getParcelable(BUNDLE_ASSET);
        }
    }*/
    if (asset != null) {
        SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(R.id.imageView);
        imageView.setImage(ImageSource.bitmap(asset));

    }

    return rootView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    View rootView = getView();
    if (rootView != null) {
        outState.putString(BUNDLE_ASSET, String.valueOf(asset));
    }
}}

ViewPager.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#333">

    <GridLayout
        android:id="@+id/suggestedParameter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:visibility="visible"
        android:paddingTop="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:layout_below="@+id/textParameter"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:id="@+id/shot_levellabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/shot_levellabel"
            android:layout_weight="0.10"
            android:gravity="center"

            android:layout_row="0"
            android:layout_column="0"
            android:textColor="#ffffff" />
        <TextView
            android:id="@+id/shot_levelsuggested"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:textColor="#90c683"
            android:layout_weight="0.10"
            android:layout_row="1"
            android:layout_column="0"
            android:textColorHint="#ffffff" />
        <TextView
            android:id="@+id/shot_levelPhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:layout_weight="0.10"
            android:layout_row="2"
            android:layout_column="0"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />
        <!--  <TextView
             android:id="@+id/shot_levelPhoto2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:hint=" None"
             android:gravity="center"
             android:layout_weight="0.10"
             android:layout_row="3"
             android:layout_column="0" />
        <TextView
             android:id="@+id/shot_levelPhoto3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:hint=" None"
             android:gravity="center"
             android:layout_weight="0.10"
             android:layout_row="4"
             android:layout_column="0" />-->




        <TextView
            android:id="@+id/shutterPhotolabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/shutterPhotolabel"
            android:layout_weight="0.10"
            android:layout_row="0"
            android:layout_column="1"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />

        <TextView
            android:id="@+id/shutter_suggested"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:textColor="#90c683"
            android:layout_weight="0.10"
            android:layout_row="1"
            android:layout_column="1"
            android:textColorHint="#ffffff" />

        <TextView
            android:id="@+id/shutterPhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:layout_weight="0.10"
            android:layout_row="2"
            android:layout_column="1"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />
        <!--  <TextView
             android:id="@+id/shutterPhoto2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:hint=" None"
             android:gravity="center"
             android:layout_weight="0.10"
             android:layout_row="3"
             android:layout_column="1" />
         <TextView
             android:id="@+id/shutterPhoto3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:hint=" None"
             android:gravity="center"
             android:layout_weight="0.10"
             android:layout_row="4"
             android:layout_column="1" />-->




        <TextView
            android:id="@+id/aperturePhotolabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/aperturePhotolabel"
            android:layout_weight="0.10"
            android:layout_row="0"
            android:layout_column="2"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/aperture_suggested"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:textColor="#90c683"
            android:layout_weight="0.10"
            android:layout_row="1"
            android:layout_column="2"
            android:textColorHint="#ffffff" />

        <TextView
            android:id="@+id/aperturePhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:layout_weight="0.10"
            android:layout_row="2"
            android:layout_column="2"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />
        <!--   <TextView
              android:id="@+id/aperturePhoto2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:hint=" None"
              android:gravity="center"
              android:layout_weight="0.10"
              android:layout_row="3"
              android:layout_column="2" />
          <TextView
              android:id="@+id/aperturePhoto3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:hint=" None"
              android:gravity="center"
              android:layout_weight="0.10"
              android:layout_row="4"
              android:layout_column="2" />-->



        <TextView
            android:id="@+id/isoPhotolabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/isoPhotolabel"
            android:layout_weight="0.10"
            android:layout_row="0"
            android:layout_column="3"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/iso_suggested"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:textColor="#90c683"
            android:layout_weight="0.10"
            android:layout_row="1"
            android:layout_column="3"
            android:textColorHint="#ffffff" />

        <TextView
            android:id="@+id/isoPhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center|right"
            android:layout_weight="0.10"
            android:paddingRight="10dp"
            android:layout_row="2"
            android:layout_column="3"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />
        <!-- <TextView
            android:id="@+id/isoPhoto2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center|right"
            android:layout_weight="0.10"
            android:paddingRight="10dp"
            android:layout_row="3"
            android:layout_column="3" />
        <TextView
            android:id="@+id/isoPhoto3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center|right"
            android:layout_weight="0.10"
            android:paddingRight="10dp"
            android:layout_row="4"
            android:layout_column="3" />-->




        <TextView
            android:id="@+id/lensPhotolabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lensPhotolabel"
            android:layout_weight="0.10"
            android:layout_row="0"
            android:layout_column="4"
            android:textColor="#ffffff" />
        <TextView
            android:id="@+id/lens_suggested"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center|left"
            android:textColor="#90c683"
            android:layout_weight="0.10"
            android:layout_row="1"
            android:layout_column="4"
            android:textColorHint="#ffffff" />

        <TextView
            android:id="@+id/lensPhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:layout_weight="0.10"
            android:layout_row="2"
            android:layout_column="4"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />
        <!--    <TextView
               android:id="@+id/lensPhoto2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:hint=" None"
               android:gravity="center"
               android:layout_weight="0.10"
               android:layout_row="3"
               android:layout_column="4"
               />
           <TextView
               android:id="@+id/lensPhoto3"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:hint=" None"
               android:gravity="center"
               android:layout_weight="0.10"
               android:layout_row="4"
               android:layout_column="4"
               />-->





        <TextView
            android:id="@+id/filterPhotolabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/filterPhotolabel"
            android:layout_weight="0.10"
            android:layout_row="0"
            android:layout_column="5"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/filter_suggested"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:textColor="#90c683"
            android:layout_row="1"
            android:layout_column="5"
            android:textColorHint="#ffffff" />

        <TextView
            android:id="@+id/filterPhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:layout_row="2"
            android:layout_column="5"
            android:textColor="#ffffff"
            android:textColorHint="#ffffff" />
        <!-- <TextView
            android:id="@+id/filterPhoto2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:layout_row="3"
            android:layout_column="5"
            />
        <TextView
            android:id="@+id/filterPhoto3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=" None"
            android:gravity="center"
            android:layout_row="4"
            android:layout_column="5"
            />-->
    </GridLayout>

    <!--<TextView
        android:id="@+id/parameter_suggested"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:textSize="14sp"
        android:textColor="#FFFFFF"
        android:visibility="visible"
        />
    <TextView
        android:id="@+id/parameter2"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:textSize="14sp"
        android:textColor="#FFFFFF"
        android:visibility="invisible"
        android:text="456"
        android:layout_below="@+id/parameter_suggested"
        />

    <TextView
        android:id="@+id/parameter3"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:textSize="14sp"
        android:textColor="#FFFFFF"
        android:visibility="visible"
        android:text="789"
        android:layout_below="@+id/parameter_suggested"
    />
    -->

</RelativeLayout>

<RelativeLayout
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#333">

    <TextView
        android:id="@+id/note1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:textSize="14sp"
        android:textColor="#FFFFFF"
        android:visibility="invisible"
        />
    <TextView
        android:id="@+id/note2"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:textSize="14sp"
        android:textColor="#FFFFFF"
        android:visibility="invisible"
        />
    <TextView
        android:id="@+id/note3"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:textSize="14sp"
        android:textColor="#FFFFFF"
        android:visibility="invisible"
        />

</RelativeLayout>

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_above="@id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/text1" />
</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

你应该将图像添加到Map或Array For Reloading:

    @Override
    public void fill(final Picture picture, int pos, Object... objects) {
        image=(ImageView) view.findViewById(R.id.img_galleryadapter_image);
        caption=(TextView) view.findViewById(R.id.txt_galleryadapter_caption);
        if(bitmapMap.containsKey(picture.imageUrl)){
            image.setImageBitmap(bitmapMap.get(picture.imageUrl));
        }else {
            image.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_hourglass_empty_black_48dp));

            Picasso.with(context).load(picture.imageUrl).memoryPolicy(MemoryPolicy.NO_CACHE).skipMemoryCache().into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
                    image.setImageBitmap(bitmap);
                    bitmapMap.put(picture.imageUrl, bitmap);
                }

                @Override
                public void onBitmapFailed(Drawable drawable) {}
                @Override
                public void onPrepareLoad(Drawable drawable) {}
            });
        }
        caption.setText(picture.caption);
    }

添加到地图:

public static Map<String,Bitmap> bitmapMap=new LinkedHashMap<>();

答案 1 :(得分:0)

在xml文件中修改你的布局,然后使用imagetextview放在RelativeLayout下面,或者将{1}}放在其下面,然后将其描述与你的java代码对齐

答案 2 :(得分:0)

经过努力和阅读这么多不同的解决方案后,我自己解决了这个问题。 我希望与您分享我的解决方案也许可以帮助别人。 随意问我关于它的一切。

这是FragmentActivity:

public class ViewPagerActivity extends FragmentActivity {

private boolean again=false;
private ViewPager page;
private Bitmap bmImg1;
private Bitmap bmImg2;
private Bitmap bmImg3;
private String TAG;
ArrayList<Bitmap> IMAGES =new ArrayList<>();
ArrayList<String> DESCRIPTIONS =new ArrayList<>();
ArrayList<String> SHOTLEVEL=new ArrayList<>();
ArrayList<String> FILTERPHOTO=new ArrayList<>();
ArrayList<String> APERTUREPHOTO=new ArrayList<>();
ArrayList<String> SHUTTERPHOTO=new ArrayList<>();
ArrayList<String> ISOPHOTO=new ArrayList<>();
ArrayList<String> LENSPHOTO=new ArrayList<>();

String[] descriptionPhoto;
private String shot_levelPhoto;
private String shot_levelPhoto1;
private String shot_levelPhoto2;

private String filterPhoto;
private String filterPhoto1;
private String filterPhoto2;

private String aperturePhoto;
private String aperturePhoto1;
private String aperturePhoto2;

private String shutterPhoto;
private String shutterPhoto1;
private String shutterPhoto2;

private String isoPhoto;
private String isoPhoto1;
private String isoPhoto2;

private String focal_lengthPhoto;
private String focal_lengthPhoto1;
private String focal_lengthPhoto2;

private String[] shot_levelPhotoArray= new String[3];
private String[] filterPhotoArray= new String[3];
private String[] aperturePhotoArray=new String[3];
private String[] shutterPhotoArray=new String[3];
private String[] isoPhotoArray=new String[3];
private String[] focal_lengthPhotoArray=new String[3];



int numpics=1;
private int position_pic;
private String suggested_aperture;
private String suggested_filter;
private String suggested_iso;
private String suggested_shutter;
private String suggested_shot_level;
private String suggested_lens;

private LayoutInflater inflater;
private boolean firsttime;
private boolean secondtime;
private int oldposition=0;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_pager);
    FileInputStream fis;
    FileInputStream fis2;
    FileInputStream fis3;
    Bundle intent=getIntent().getExtras();
    descriptionPhoto = intent.getStringArray("descriptionPhoto");

    numpics = (int) intent.get("numpics");
    position_pic = (int) intent.get("position_pic");

    suggested_aperture= intent.getString("suggested_aperture");
    suggested_filter= intent.getString("suggested_filter");
    suggested_iso=  intent.getString("suggested_iso");
    suggested_shutter= intent.getString("suggested_shutter");
    suggested_shot_level= intent.getString("suggested_shot_level");
    suggested_lens= intent.getString("suggested_lens");

    shot_levelPhoto=intent.getString("shot_levelPhoto");
    shot_levelPhoto1=intent.getString("shot_levelPhoto1");
    shot_levelPhoto2=intent.getString("shot_levelPhoto2");

    shot_levelPhotoArray[0] = shot_levelPhoto;
    if(!shot_levelPhoto1.equals("null")){
        shot_levelPhotoArray[1]=shot_levelPhoto1;
        if(!shot_levelPhoto2.equals("null")){
            shot_levelPhotoArray[2]=shot_levelPhoto2;
        }
    }

    filterPhoto =intent.getString("filterPhoto");
    filterPhoto1 =intent.getString("filterPhoto1");
    filterPhoto2 =intent.getString("filterPhoto2");
    filterPhotoArray[0]= filterPhoto;
    if(!filterPhoto1.equals("null")){
        filterPhotoArray[1]=filterPhoto1;
        if(!filterPhoto2.equals("null")){
            filterPhotoArray[2]=filterPhoto2;
        }
    }

    aperturePhoto =intent.getString("aperturePhoto");
    aperturePhoto1 =intent.getString("aperturePhoto1");
    aperturePhoto2 =intent.getString("aperturePhoto2");
    aperturePhotoArray[0]= aperturePhoto;
    if(!aperturePhoto1.equals("null")){
        aperturePhotoArray[1]=aperturePhoto1;
        if(!aperturePhoto2.equals("null")){
            aperturePhotoArray[2]=aperturePhoto2;
        }
    }

    shutterPhoto =intent.getString("shutterPhoto");
    shutterPhoto1 =intent.getString("shutterPhoto1");
    shutterPhoto2 =intent.getString("shutterPhoto2");
    shutterPhotoArray[0]= shutterPhoto;
    if(!shutterPhoto1.equals("null")){
        shutterPhotoArray[1]=shutterPhoto1;
        if(!shutterPhoto2.equals("null")){
            shutterPhotoArray[2]=shutterPhoto2;
        }
    }

    isoPhoto =intent.getString("isoPhoto");
    isoPhoto1 =intent.getString("isoPhoto1");
    isoPhoto2 =intent.getString("isoPhoto2");
    isoPhotoArray[0]= isoPhoto;
    if(!isoPhoto1.equals("null")){
        isoPhotoArray[1]=isoPhoto1;
        if(!isoPhoto2.equals("null")){
            isoPhotoArray[2]=isoPhoto2;
        }
    }

    focal_lengthPhoto=intent.getString("focal_lengthPhoto");
    focal_lengthPhoto1=intent.getString("focal_lengthPhoto1");
    focal_lengthPhoto2=intent.getString("focal_lengthPhoto2");
    focal_lengthPhotoArray[0]= focal_lengthPhoto;
    if(!focal_lengthPhoto1.equals("null")){
        focal_lengthPhotoArray[1]=focal_lengthPhoto1;
        if(!focal_lengthPhoto2.equals("null")){
            focal_lengthPhotoArray[2]=focal_lengthPhoto2;
        }
    }


    TextView txtaperture_suggested = (TextView) findViewById(R.id.aperture_suggested);
    TextView txtfilter_suggested = (TextView) findViewById(R.id.filter_suggested);
    TextView txtiso_suggested = (TextView) findViewById(R.id.iso_suggested);
    TextView txtlens_suggested = (TextView) findViewById(R.id.lens_suggested);
    TextView txtshutter_suggested = (TextView) findViewById(R.id.shutter_suggested);
    TextView txtshot_levelsuggested = (TextView) findViewById(R.id.shot_levelsuggested);

    if (suggested_shot_level == "1") {
        txtshot_levelsuggested.setText("Easy");

    } else if (suggested_shot_level =="2") {
        txtshot_levelsuggested.setText("Medium");

    } else if (suggested_shot_level =="3") {
        txtshot_levelsuggested.setText("Difficult");

    } else if (suggested_shot_level =="4"){
        txtshot_levelsuggested.setText("Pro");

    }

    txtaperture_suggested.setText(suggested_aperture);
    txtfilter_suggested.setText(suggested_filter);
    txtiso_suggested.setText(suggested_iso);
    txtshutter_suggested.setText(suggested_shutter);
    txtlens_suggested.setText(suggested_lens);

   /* TextView txtshutterPhoto = (TextView) findViewById(R.id.shutterPhoto);
    TextView txtshutterPhoto2 = (TextView) findViewById(R.id.shutterPhoto2);
    TextView txtshutterPhoto3 = (TextView) findViewById(R.id.shutterPhoto3);

    TextView txtshot_levelPhoto = (TextView) findViewById(R.id.shot_levelPhoto);
    TextView txtshot_levelPhoto2 = (TextView) findViewById(R.id.shot_levelPhoto2);
    TextView txtshot_levelPhoto3 = (TextView) findViewById(R.id.shot_levelPhoto3);

    TextView txtaperturePhoto = (TextView) findViewById(R.id.aperturePhoto);
    TextView txtaperturePhoto2 = (TextView) findViewById(R.id.aperturePhoto2);
    TextView txtaperturePhoto3 = (TextView) findViewById(R.id.aperturePhoto3);

    TextView txtfilterPhoto = (TextView) findViewById(R.id.filterPhoto);
    TextView txtfilterPhoto2 = (TextView) findViewById(R.id.filterPhoto2);
    TextView txtfilterPhoto3 = (TextView) findViewById(R.id.filterPhoto3);

    TextView txtisoPhoto = (TextView) findViewById(R.id.isoPhoto);
    TextView txtisoPhoto2 = (TextView) findViewById(R.id.isoPhoto2);
    TextView txtisoPhoto3 = (TextView) findViewById(R.id.isoPhoto3);

    TextView txtfocal_lengthPhoto = (TextView) findViewById(R.id.lensPhoto);
    TextView txtfocal_lengthPhoto2 = (TextView) findViewById(R.id.lensPhoto2);
    TextView txtfocal_lengthPhoto3 = (TextView) findViewById(R.id.lensPhoto3);*/

   /* if (shot_levelPhotoArray[0] == "1") {
        txtshot_levelPhoto.setText("Easy");
        shot_levelPhotoArray[0] = "Easy";
        *//*txtshot_levelPhoto2.setText("Easy");
        txtshot_levelPhoto3.setText("Easy");*//*

    } else if (shot_levelPhotoArray[0] == "2") {
        txtshot_levelPhoto.setText("Medium");
        shot_levelPhotoArray[0] = "Medium";
       *//* txtshot_levelPhoto2.setText("Medium");
        txtshot_levelPhoto3.setText("Medium");*//*
    } else if (shot_levelPhotoArray[0] == "3") {
        txtshot_levelPhoto.setText("Difficult");
        shot_levelPhotoArray[0] = "Difficult";
        *//*txtshot_levelPhoto2.setText("Difficult");
        txtshot_levelPhoto3.setText("Difficult");*//*
    } else {
        txtshot_levelPhoto.setText("Pro");
        shot_levelPhotoArray[0] = "Pro";
       *//* txtshot_levelPhoto2.setText("Pro");
        txtshot_levelPhoto3.setText("Pro");*//*
    }


    txtshutterPhoto.setText(shutterPhoto);
    txtisoPhoto.setText(isoPhoto);
    txtfilterPhoto.setText(filterPhoto);
    txtaperturePhoto.setText(aperturePhoto);
    txtfocal_lengthPhoto.setText(focal_lengthPhoto);*/




    /*txtshutterPhoto.setText(shutterPhoto[0]);
    if (shutterPhoto.length > 1) {
        txtshutterPhoto2.setText(shutterPhoto[1]);
        if (shutterPhoto.length > 2) {
            txtshutterPhoto3.setText(shutterPhoto[2]);

        }
    }


    txtisoPhoto.setText(isoPhoto[0]);
    if (isoPhoto.length > 1) {
        txtisoPhoto2.setText(isoPhoto[1]);
        if (isoPhoto.length > 2) {
            txtisoPhoto3.setText(isoPhoto[2]);
        }
    }


    txtfilterPhoto.setText(filterPhoto[0]);
    if (filterPhoto.length > 1) {
        txtfilterPhoto2.setText(filterPhoto[1]);
        if ((filterPhoto.length > 2)) {
            txtfilterPhoto3.setText(filterPhoto[2]);
        }
    }


    txtaperturePhoto.setText(aperturePhoto[0]);
    if (aperturePhoto.length > 1) {
        txtaperturePhoto2.setText(aperturePhoto[1]);
        if (aperturePhoto.length > 2) {
            txtaperturePhoto3.setText(aperturePhoto[2]);

        }
    }*/

    try {
        fis = getApplicationContext().openFileInput("bmImg1");
        bmImg1 = BitmapFactory.decodeStream(fis);
        IMAGES.add(bmImg1);
        DESCRIPTIONS.add(descriptionPhoto[0]);
        SHOTLEVEL.add(shot_levelPhotoArray[0]);
        FILTERPHOTO.add(filterPhotoArray[0]);
        APERTUREPHOTO.add(aperturePhotoArray[0]);
        SHUTTERPHOTO.add(shutterPhotoArray[0]);
        ISOPHOTO.add(isoPhotoArray[0]);
        LENSPHOTO.add(focal_lengthPhotoArray[0]);
        fis.close();

        if(numpics>1){
            fis2 = getApplicationContext().openFileInput("bmImg2");
            bmImg2 = BitmapFactory.decodeStream(fis2);
            IMAGES.add(bmImg2);
            DESCRIPTIONS.add(descriptionPhoto[1]);
            SHOTLEVEL.add(shot_levelPhotoArray[1]);
            FILTERPHOTO.add(filterPhotoArray[1]);
            APERTUREPHOTO.add(aperturePhotoArray[1]);
            SHUTTERPHOTO.add(shutterPhotoArray[1]);
            ISOPHOTO.add(isoPhotoArray[1]);
            LENSPHOTO.add(focal_lengthPhotoArray[1]);
            fis2.close();


            if(numpics>2){
                fis3 = getApplicationContext().openFileInput("bmImg3");
                bmImg3 = BitmapFactory.decodeStream(fis3);
                IMAGES.add(bmImg3);
                DESCRIPTIONS.add(descriptionPhoto[2]);
                SHOTLEVEL.add(shot_levelPhotoArray[2]);
                FILTERPHOTO.add(filterPhotoArray[2]);
                APERTUREPHOTO.add(aperturePhotoArray[2]);
                SHUTTERPHOTO.add(shutterPhotoArray[2]);
                ISOPHOTO.add(isoPhotoArray[2]);
                LENSPHOTO.add(focal_lengthPhotoArray[2]);
                fis3.close();
                }
            }
        }
    catch (FileNotFoundException e) {
        Log.d(TAG, "file not found");
        e.printStackTrace();
    }
    catch (IOException e) {
        Log.d(TAG, "io exception");
        e.printStackTrace();
    }

    PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), descriptionPhoto[position_pic]);
    page = (ViewPager)findViewById(R.id.pager);
    page.setAdapter(pagerAdapter);
    page.setCurrentItem(position_pic);


}


@Override
public void onBackPressed() {
    super.onBackPressed();


}
public void clear() {
    IMAGES.clear();
    DESCRIPTIONS.clear();
    SHOTLEVEL.clear();
    FILTERPHOTO.clear();
    APERTUREPHOTO.clear();
    SHUTTERPHOTO.clear();
    ISOPHOTO.clear();
    LENSPHOTO.clear();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    finish();
    return true;
}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    String description;

    public ScreenSlidePagerAdapter(FragmentManager fm, String description) {
        super(fm);
        this.description=description;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    //this is called when notifyDataSetChanged() is called
    @Override
    public int getItemPosition(Object object) {
        // refresh all fragments when data set changed
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        if(object != null){
            return ((Fragment)object).getView() == view;
        }else{
            return false;

        }
    }

    /*@Override
    public Fragment getItem(int position) {
        ViewPagerFragment fragment = new ViewPagerFragment();
        fragment.setAsset(IMAGES.get(position));
        return fragment;
    }*/

    @Override
    public Fragment getItem(int index) {
        ViewPagerFragment fragment = new ViewPagerFragment();
        //fragment.setAsset(IMAGES.get(index));
        fragment.setAsset(IMAGES.get(index), DESCRIPTIONS.get(index));
        ViewPagerFragment.newInstance(index,
                SHOTLEVEL.get(index),
                FILTERPHOTO.get(index),
                APERTUREPHOTO.get(index),
                SHUTTERPHOTO.get(index),
                ISOPHOTO.get(index),
                LENSPHOTO.get(index));  // Pages is an array of Strings
        return fragment;

    }

    @Override
    public int getCount() {
        return IMAGES.size();
    }

}
}

这是片段

public class ViewPagerFragment extends Fragment {

private static final String BUNDLE_ASSET = "res";

private Bitmap asset;

String descriptionPhoto;
private TextView tv;

HashMap<Bitmap, String > map;
private static Bundle bundle;


public ViewPagerFragment() {
}

/*public int setAsset(Bitmap asset) {
    this.asset = asset;

    return 0;
}*/

public HashMap<Bitmap, String> setAsset(Bitmap asset, String description){
    map= new HashMap<>();
    this.asset = asset;
    this.descriptionPhoto=description;

    map.put(this.asset, this.descriptionPhoto);
    return  map;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.view_pager_page, container, false);

    if (savedInstanceState != null) {
        if (asset == null && savedInstanceState.containsKey(BUNDLE_ASSET)) {
            asset = savedInstanceState.getParcelable(BUNDLE_ASSET);
        }
    }
    if (asset != null && descriptionPhoto!=null) {
        TextView descriptionnote = (TextView) rootView.findViewById(R.id.note1);
        if(descriptionPhoto.equals("null")){
            descriptionPhoto="No description for this picture";
        }
        descriptionnote.setText(descriptionPhoto);
        SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)rootView.findViewById(R.id.imageView);
        imageView.setImage(ImageSource.bitmap(asset));
        TextView txtshot_level = (TextView) rootView.findViewById(R.id.shot_levelPhoto);
        TextView txtshutterPhoto = (TextView) rootView.findViewById(R.id.shutterPhoto);
        TextView txtaperturePhoto = (TextView) rootView.findViewById(R.id.aperturePhoto);
        TextView txtfilterPhoto = (TextView) rootView.findViewById(R.id.filterPhoto);
        TextView txtisoPhoto = (TextView) rootView.findViewById(R.id.isoPhoto);
        TextView txtfocal_lengthPhoto = (TextView) rootView.findViewById(R.id.lensPhoto);

        txtshot_level.setText(bundle.getString("shot_levelcontent"));
        txtshutterPhoto.setText(bundle.getString("shuttercontent"));
        txtisoPhoto.setText(bundle.getString("isocontent"));
        txtfilterPhoto.setText(bundle.getString("filtercontent"));
        txtaperturePhoto.setText(bundle.getString("aperturecontent"));
        txtfocal_lengthPhoto.setText(bundle.getString("lenscontent"));

    }
    return rootView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    View rootView = getView();
    if (rootView != null) {
        outState.putString(BUNDLE_ASSET, String.valueOf(asset));
    }
}


public static Fragment newInstance(int position,
                                   String shot_level,
                                   String filter,
                                   String aperture,
                                   String shutter,
                                   String iso,
                                   String lens) {
    ViewPagerFragment swipeFragment = new ViewPagerFragment();
    bundle = new Bundle();
    if (shot_level.equals("1")) {
        bundle.putString("shot_levelcontent", "Easy");

    } else if (shot_level.equals("2")) {
        bundle.putString("shot_levelcontent", "Medium");

    } else if (shot_level.equals("3")) {
        bundle.putString("shot_levelcontent", "Difficult");
    } else {
        bundle.putString("shot_levelcontent", "Pro");
    }
    bundle.putString("filtercontent", filter);
    bundle.putString("aperturecontent", aperture);
    bundle.putString("shuttercontent", shutter);
    bundle.putString("isocontent", iso);
    bundle.putString("lenscontent", lens);
    swipeFragment.setArguments(bundle);
    return swipeFragment;

}
}