在ImageAdapter中获取listview的位置扩展了PagerAdapter

时间:2015-08-04 05:10:27

标签: android android-listview android-pageradapter

我有一个包含历史记录,地图,图库的列表视图。当我点击图库时,我正在使用查看寻呼机向左右移动图像。我想要的是获得listview的位置。所以在ImageAdapter.java中我想得到图库的位置,以便我可以使用不同的图像来处理不同的事情。

page4.java
package com.example.namrathasrinivas.karnatakatemples;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.AdapterView;
import android.support.v7.app.ActionBarActivity;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.os.Handler;
import java.util.Timer;
import java.util.TimerTask;
/**
 * Created by namrathasrinivas on 23/07/15.
 */
public class page4 extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page4);

        Intent intent = getIntent();
        String list1 = intent.getStringExtra("list1");
        //int list2=Integer.parseInt(list1);

        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImageAdapter adapter = new ImageAdapter(this);
        viewPager.setAdapter(adapter);






    }


}

ImageAdapter.java
package com.example.namrathasrinivas.karnatakatemples;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.os.Handler;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by namrathasrinivas on 31/07/15.
 */
public class ImageAdapter extends PagerAdapter {

    Context context;
    private int[] GalImages = new int[] {
            R.drawable.chokkanatha1,
            R.drawable.chokkanatha2,
            R.drawable.chokkanatha3,
            R.drawable.chokkanatha4,
            R.drawable.chokkanatha5,
            R.drawable.chokkanatha6
    };

    ImageAdapter(Context context){


        this.context=context;
    }

//    private int position;
//    ImageAdapter(Context context, int position) {
//        this.context = context;
//        this.position = position;
//
//        Log.d("message", "test");
//    }




    @Override
    public int getCount() {
        return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {


       // Log.d("message", "position");



        ImageView imageView = new ImageView(context);
        //imageView.getItem(myViewPager.getCurrentItem());

        // int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
        // imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setImageResource(GalImages[position]);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

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

0 个答案:

没有答案