On A Listview Item's Child Click
I have a ListView where each of its items is composed from some ImageViews and TextViews, i want that when i click on a specefic ImageView, some code will be executed, where should
Solution 1:
Lets say your getView
has a textView
and a imageView
@Overridepublic View getView(finalint position, View convertView, final ViewGroup parent) {
Viewview= LayoutInflater.from(parent.getContext()).inflate(R.layout.xml, parent, false);
// setup view bindingsTextViewtextView= view.findViewById(R.id.textView);
ImageViewimageView= view.findViewById(R.id.imageView);
setClickListener(textView, position, parent);
setClickListener(imageView, position, parent);
}
privatevoidsetClickListener(View view, finalint position, final ViewGroup parent){
view.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// this part is important, it lets ListView handle the clicks
((ListView) parent).performItemClick(v, position, 0);
}
});
}
Now in Activity
or Fragment
ListViewlistView= findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
longviewId= view.getId();
if (viewId == R.id.textView) {
Toast.makeText(Activity.this, "TextView Clicked", Toast.LENGTH_SHORT).show();
} elseif (viewId == R.id.imageView) {
Toast.makeText(Activity.this, "ImageView Clicked", Toast.LENGTH_SHORT).show();
}
}
});
Solution 2:
In the UploadedAdapter
in getView()
you must be creating the object of the ImageViews
.
Just set the onClickListener
to the objects of that ImageViews
and handle the click event in the onClick
on the basis of the id of each ImageView
.
Solution 3:
You put this code in
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id)
{
}
or you can write something like that
listview.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, final View view,
int position, long id) {
finalStringitem= (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(newRunnable() {
@Overridepublicvoidrun() {
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
Post a Comment for "On A Listview Item's Child Click"