Accessing Imageview Inside Adapter On Onitemclicklistener
Solution 1:
There are multiple solutions to solve this issue.
You can either get the views you want to hide / show via the view you get in the onClickListener
publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id){
view.findViewById(R.id.optionbackground).setVisibility(View.VISIBLE);
...
}
or you define the onClickListener inside your adapter and keep those views final. So you can set the visibility inside the onClick event.
Viewv= inflater.inflate(R.layout.drawer_list_item, parent, false);
...
final ImageView optionBackground=(ImageView)v.findViewById(R.id.optionbackground);
v.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
optionBackground.setVisibility(View.VISIBLE);
}
});
...
If you plan to update to the RecyclerView. This view does not come with an onItemClickListener anymore, and you will need to add the Listener to the view inside the Adapter.
You can still pass it out to the containing activity or fragment, as i've done it in one of my Open Source projects:
For the additional info on showing it and hiding it onTouch
.setOnTouchListener(newView.OnTouchListener() {
@OverridepublicbooleanonTouch(View v, MotionEvent event) {
switch (event.getAction()) {
caseMotionEvent.ACTION_DOWN:
//pressbreak;
caseMotionEvent.ACTION_UP:
caseMotionEvent.ACTION_CANCEL:
//unpressbreak;
}
// Pass all events through to the host view.returnfalse;
}
}
Solution 2:
dumazy's approach is good one but there is another solution to that as well. You can create onclick method in adapter's getView method. Here is an example
final ImageView optionBackground= (ImageView)v.findViewById(R.id.optionbackground);
v.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub
optionBackground.setVisibility(View.VISIBLE);
}
});
Solution 3:
In the onItemClick
method you have the view parameter. This parameter is the View that contain all the elements you have inflated in the getView
method in your CustomAdapter.
So in order to do what you want to achieve you simply have to retrieve the ImageView with its ID and set its visibility.
mDrawerList.setOnItemClickListener(newAdapterView.OnItemClickListener()
{
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id)
{
view.getViewById(R.id.optionBackground).setVisibility(View.VISIBLE);
}
});
That's it.
Post a Comment for "Accessing Imageview Inside Adapter On Onitemclicklistener"