触摸图片视图不起作用

时间:2017-02-03 14:20:28

标签: android android-viewpager android-adapter baseadapter android-touch-event

我制作了一个简单的应用,其中我使用了3个标签。每个标签都有单独的classeslayouts。第一个标签显示地图。第二个选项卡将显示用户可以拍摄照片的相机和第三个选项卡已关闭在GridView中查看拍摄图像的照片。所有标签都运行良好。现在我希望当用户点按任何图片时显示完整ImageView

对于所有这一切,我使用此tutorial。所以我遵循了每一步,事情进展顺利。但是对于完整的ImageView,代码无效。

以下是我的照片Fragment

Pictures.java

public class Pictures extends Fragment implements MainActivity.Updateable {

private Utils utils;
private ArrayList<String> imagePaths = new ArrayList<String>();
private GridViewImageAdapter adapter;
private GridView gridView;
private int columnWidth;


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

    gridView = (GridView) rootView.findViewById(R.id.grid_view);
    utils = new Utils(getContext());

    // Initilizing Grid View
    InitilizeGridLayout();

    // loading all image paths from SD card
    imagePaths = utils.getFilePaths();

    // Gridview adapter

    adapter = new GridViewImageAdapter(getActivity(), imagePaths, columnWidth);
    /*adapter = new GridViewImageAdapter(Pictures.this, imagePaths,
            columnWidth);*/

    // setting grid view adapter
    gridView.setAdapter(adapter);

    //((BaseAdapter) adapter).notifyDataSetChanged();
    //adapter.notifyDataSetChanged();


    return rootView;
}

private void InitilizeGridLayout() {

    Resources r = getResources();
    float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            AppConstant.GRID_PADDING, r.getDisplayMetrics());

    columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

    gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS);
    gridView.setColumnWidth(columnWidth);
    gridView.setStretchMode(GridView.NO_STRETCH);
    gridView.setPadding((int) padding, (int) padding, (int) padding,
            (int) padding);
    gridView.setHorizontalSpacing((int) padding);
    gridView.setVerticalSpacing((int) padding);
}


@Override
public void update() {
    if(adapter !=null)
    {
        adapter.notifyDataSetChanged();

    }
    else
    {
        Log.d(getTag(),"null");
    }
}}

现在,对于全屏视图,我为屏幕制作了一个新的Activity两个Layouts个屏幕,另一个用于图片

fullscreen.xml

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

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

fullscreenImage.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
    android:id="@+id/imgDisplay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitCenter" />

<Button
    android:id="@+id/btnClose"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:paddingTop="2dp"
    android:paddingBottom="2dp"
    android:background="@drawable/button_background"
    android:textColor="#ffffff"
    android:text="Close" /></RelativeLayout>

现在我创建了一个Adapter

FullScreenImageAdapter.java

public class FullScreenImageAdapter extends PagerAdapter {

private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;

// constructor
public FullScreenImageAdapter(Activity activity,
                              ArrayList<String> imagePaths) {
    this._activity = activity;
    this._imagePaths = imagePaths;
}

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

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

@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imgDisplay;
    Button btnClose;

    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
            false);

    imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);
    btnClose = (Button) viewLayout.findViewById(R.id.btnClose);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
    imgDisplay.setImageBitmap(bitmap);

    // close button click event
    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            _activity.finish();
        }
    });

    ((ViewPager) container).addView(viewLayout);

    return viewLayout;
}

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

}}

现在全屏Activity

FullScreenViewActivity.java

public class FullScreenViewActivity extends Activity {

private Utils utils;
private FullScreenImageAdapter adapter;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fullscreen_view);

    viewPager = (ViewPager)findViewById(R.id.pager);

    utils = new Utils(getApplicationContext());

    Intent i = getIntent();

    int position = i.getIntExtra("position",0);

    adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
            utils.getFilePaths());

    viewPager.setAdapter(adapter);

    // displaying selected image first
    viewPager.setCurrentItem(position);

}}

现在当我点击任何图像时没有任何反应,logcat没有任何错误,没有任何警告或任何应用程序崩溃。

我已经调试了应用程序,但它没有达到那一点。

我坚持下去,不知道该怎么做

任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分:3)

这可能不是您正在寻找的答案!但是,如果你的代码不起作用,这将有助于你的解决方案。如果你知道整个过程,你可以插拔这个。

这个答案取自您所跟随的tutorial人,它与您的代码具有相同的模式。注意图像阵列是硬编码的。

看看,在 AndroidGridLayoutActivity

上特别注意

启动器活动:HomeActivity您的清单中需要的其他活动:FullImageActivity

grid_layout.xml &gt;

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"
    android:horizontalSpacing="5dp"
    android:verticalSpacing="5dp"
    android:gravity="center">
</GridView>

full_image.xml &gt;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView android:id="@+id/full_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout

HomeActivity &gt;

public class HomeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        GridView gridView = (GridView) findViewById(R.id.grid_view);
        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));
    }
}

AndroidGridLayoutActivity &gt; (检查此中的注释)

public class AndroidGridLayoutActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);

        GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //----------------------Seems In your code you are missing this Part----------------------
                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }
}

ImageAdapter &gt;

 public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array - I have hard coded this
    public Integer[] mThumbIds = {
            R.drawable.casper, R.drawable.casper,
            R.drawable.monkey, R.drawable.monkey,
            R.drawable.cub, R.drawable.cub,
            R.drawable.mouse, R.drawable.mouse,
            R.drawable.casper, R.drawable.casper,
            R.drawable.monkey, R.drawable.monkey,
            R.drawable.cub, R.drawable.cub,
            R.drawable.mouse
    };

    public ImageAdapter(Context c){
        mContext = c;
    }

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

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(150, 150));  /// IF YOU WANT TO CHANGE IMAGE SIZE CHANGE HERE
        return imageView;
    }

}

作为一个单独的项目,这将工作。如果你不能解决你的问题插件并发挥此作用。只是发布作为帮助!

输出&gt;

![3TVetv](http://i.makeagif.com/media/2-06-2017/3TVetv.gif)

答案 1 :(得分:0)

您可以尝试在网格视图上使用OnItemClickListener,如下所示

 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Get your item here
                adapterView.getItemAtPosition(i);
               //Do your code here 
            }
        });

请确保您从getView中删除了您的点击侦听器,否则会发生冲突。

答案 2 :(得分:0)

请确保您必须使用单个imageview类

将位置ID发送到imageview类我使用下面的代码进行全屏图像视图

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i = new Intent(mContext, Singleimageview.class);
        i.putExtra("pos", (Integer) itemView.getTag() + "");

        mContext.startActivity(i);
        overridePendingTransition(R.anim.pull_in_left, R.anim.pull_out_right);
    }
});