android studio设置 onclicklistener 以显示完整的图像

发布时间: 2016/9/16 2:37:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

基本上它是一个容易解决的问题,但只是打不通。我得到了一个网格图像(从 drawable 文件夹) 在 fragment 和每个图像假设要显示全屏幕通过单击。 这是我不能实现的一部分。:(

我已经下载了 Picasso 但我不知道如何使用它根本 ! @_@

请帮我这是我第一次 android application ,它对我意味着很多:D

Fragment包含图像

public class Fragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment2, container, false);
    GridView gridView = (GridView) view.findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

            //Intent intent = new Intent(getActivity(), DetailsActivity.class);
            //startActivity(intent);

        }
    });

    return view;
}
}

图像Adapter

public class ImageAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;

public ImageAdapter(Context context) {
    mInflater = LayoutInflater.from(context);

    mItems.add(new Item(R.drawable.t1));
    mItems.add(new Item(R.drawable.t2));
    mItems.add(new Item(R.drawable.t3));
    mItems.add(new Item(R.drawable.t4));
    mItems.add(new Item(R.drawable.t5));
    mItems.add(new Item(R.drawable.t6));
    mItems.add(new Item(R.drawable.t7));
    mItems.add(new Item(R.drawable.t8));
    mItems.add(new Item(R.drawable.t9));
    mItems.add(new Item(R.drawable.t10));
    mItems.add(new Item(R.drawable.t11));
    mItems.add(new Item(R.drawable.t12));
    mItems.add(new Item(R.drawable.t13));
    mItems.add(new Item(R.drawable.t14));
    mItems.add(new Item(R.drawable.t15));
    mItems.add(new Item(R.drawable.t16));
    mItems.add(new Item(R.drawable.t17));
    mItems.add(new Item(R.drawable.t18));
    mItems.add(new Item(R.drawable.t19));
    mItems.add(new Item(R.drawable.t20));
    mItems.add(new Item(R.drawable.t21));
    mItems.add(new Item(R.drawable.t22));
}

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

@Override
public Item getItem(int i) {
    return mItems.get(i);
}

@Override
public long getItemId(int i) {
    return mItems.get(i).drawableId;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;

    if (v == null) {
        v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
    }

    picture = (ImageView) v.getTag(R.id.picture);

    Item item = getItem(i);

    picture.setImageResource(item.drawableId);

    return v;
}

private static class Item {
    public final int drawableId;

    Item(int drawableId) {
        this.drawableId = drawableId;
    }
}
}

DetailActivity (要包含在全屏幕的图像)

public class DetailsActivity extends AppCompatActivity {

//public static final String EXTRA_IMAGE = "extra_image";

//private ImageView nImageView;

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

}
}

解决方法 1:

我希望我正确理解你的要求...

在你 Fragment :

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override 
    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {

        Intent intent = new Intent(getActivity(), DetailsActivity.class); 
        intent.putExtra(DetailsActivity.EXTRA_IMAGE , position);
        startActivity(intent); 
     } 
}); 

详细信息 Activity :

public class DetailsActivity extends AppCompatActivity { 
    public static final String EXTRA_IMAGE = "extra_image"; 
    private ImageView nImageView; 

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

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            int positionClicked = bundle.getInt(EXTRA_IMAGE);
        } 
        nImageView = (ImageView) findViewById(R.id.yourImageView);

        int imageResId = HERE YOU SHOULD PUT IMAGE RESUORCE BASED ON POSITION

        Picasso.with(this)
                .load(imageResId )
                .into(nImageView );
    }           
}  

====================== UPDATE ============================

//int imageResId = HERE YOU SHOULD PUT IMAGE RESUORCE BASED ON POSITION

int imageResId = 0;

switch(positionClicked) {
    case 0:
        imageResId = R.drawable.t1;
        break;
    case 1:
        imageResId = R.drawable.t2;
        break;

    ...

}
官方微信
官方QQ群
31647020