Create a clickable image in a GridView in Android

前端 未结 3 821
忘了有多久
忘了有多久 2020-12-09 12:06

I have images displayed in a GridView as in this tutorial. I want to be able to click on a single image and do other events and I need to know what image was clicked.

3条回答
  •  没有蜡笔的小新
    2020-12-09 12:27

    i tried the above mentioned method getView(final int position . . .) realized that the position got "reset" after 28 items and the position when back to 0 after the 28th item in the gridview.

    i suspected the final keyword is creating problem and after removing it i was able to get the positions as expected.

    Below is a sample code with the click event being called in the activity that is showcasing the gridview.

    public class MainActivity extends Activity {
    
    ArrayList item_ids = new ArrayList();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        item_ids = //get your item ids method
    
        GridView gridview = (GridView) findViewById(R.id.grid_featured);
        gridview.setAdapter(new ImageAdapter(this));
        gridview.setOnItemClickListener(itemClickListener);
    
        footer = (Footer)findViewById(R.id.layoutFooter);
        footer.setActivity(this);
    }
    
    private OnItemClickListener itemClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
        Log.d(TAG,"Position Clicked ["+position+"] with item id ["+item_ids.get(position)+"]");
        }
    };
    

    }

提交回复
热议问题