0

我制作了一个简单的应用程序,其中使用了 3 个选项卡。每个选项卡都有单独的classeslayouts。第一个选项卡显示地图。第二个选项卡将显示用户可以从中拍照的相机,第三个选项卡关闭 图片在GridView. 所有选项卡都运行良好。现在我想要的是ImageView当用户点击任何图像时显示完整。

对于所有这一切,我正在使用本教程。所以我遵循了它的每一步,事情进展顺利。但是完整ImageView的代码不起作用。

下面是我的图片Fragment

图片.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的屏幕和图像,如下所示

全屏.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>

全屏图像.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,没有任何警告,也没有任何应用程序崩溃。

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

我被困住了,不知道该怎么办

任何帮助将不胜感激。

4

3 回答 3

2

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

此答案取自您遵循的同一个教程人员,该教程与您的代码具有相同的模式。注意图像数组是硬编码的。

看一看,特别注意AndroidGridLayoutActivity

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


grid_layout.xml >

<?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>

全图像.xml >

<?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

首页活动>

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 >(查看这里的评论

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

图像适配器>

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

}

作为一个单独的项目,这将起作用。如果您无法解决问题,即插即用。只是作为帮助发布!

输出>

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

于 2017-02-06T11:39:28.733 回答
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 中删除了单击侦听器,否则会产生冲突。

于 2017-02-10T06:59:43.557 回答
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);
    }
});
于 2017-02-13T05:44:01.867 回答