Android gallery scrolling issue (custom images) - android

I am new to android. I am using a custom gallery to show images and check boxes over images. Both have their own click listeners in adapter class getView() method. When I scroll the gallery, it doesn't scroll from images and check boxes. Instead it scrolls from free spaces between the image thumbs.

i have solved this issue in my scenario i have an image and a check box i have to implement click listener on image and check box i use "checkedChangedlistener" instead of Click listener on check box in get view method of adapter class
public View getView(int position, View convertView, ViewGroup parent)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.icon, null);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.gCheckBox);
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
}
else if(!isChecked)
{
}
}
});
}
and after this i need Click listener for my image thumb so i use on item click listener of gallery in my activity.
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Intent i = new Intent(Conditions.this, TestExampleGUIActivity.class);
startActivity(i);
}
});
when item click event fires i perform my action which i want to perform on my image thumb. its working fine and gallery behavior is also fine
i am sorry! i am new to this forum so i have many issues to explain my problems i will improve this in future thanks to all of you

Related

Android Listview changes icon when scrolled

I have a ListView on my Android App. Each row has a text and an image. When the user clicks a row, the row's image is changed (this works fine). The problem is, when the user scrolls the ListView, at the moment the row crosses the top part of the ListView (when you stop seeing that row), the image changes back to the default one automatically. How can I prevent this? If the user clicks the row and the image is changed, I need that new image to stay there (since the new image is the indicator for the user that he already clicked the row).
Thank you for your help!
Here's my code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i;
ImageView imageView;
switch (position){
case 0:
imageView = (ImageView) view.findViewById(R.id.check_alimentacion);
if (frutas.equals("NO")){
imageView.setImageResource(R.drawable.checkbox);
frutas = "SI";
}else {
imageView.setImageResource(R.drawable.checkbox_inactivo);
frutas = "NO";
}
break;
case 1:
imageView = (ImageView) view.findViewById(R.id.check_alimentacion);
if (verdura.equals("NO")){
imageView.setImageResource(R.drawable.checkbox);
verdura = "SI";
}else {
imageView.setImageResource(R.drawable.checkbox_inactivo);
verdura = "NO";
}
break;
case 2:
imageView = (ImageView) view.findViewById(R.id.check_alimentacion);
if (carne.equals("NO")){
imageView.setImageResource(R.drawable.checkbox);
carne = "SI";
}else {
imageView.setImageResource(R.drawable.checkbox_inactivo);
carne = "NO";
}
break;
}
}
});
Well firstly, this is an interesting question.
The images are being replaced because when the list-item view exits the screen it is destroyed, and whenever it reappears it is newly created again. Entirely from scratch. A drawback of listview. A better way to do is to use a RecyclerView. It will create objects for each row using a ViewHolder and later instead of creating everything from scratch, it refers the already created ViewHolder objects.
However, the user selection for a row still needs to be handled all by ourselves here. I would suggest as follows.
Create a global boolean array of same length as your listview.
boolean[] selected=new boolean[listViewLength];
Now, inside onItemClickListener()
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//change value of boolean to true, to depict as selected
selected[position]=true; //default value of boolean is false.
//update the image
imageView = (ImageView) view.findViewById(R.id.check_alimentacion);
imageView.setImageResource(R.drawable.checkedDrawable);
}
});
And finally in your getView() check for the selection which will look something like this,
public View getView (int position, View convertView, ViewGroup parent){
//Initialize the item-view
LayoutInflater inflater=(LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.listItemLayout,null,false);
imageView = (ImageView) view.findViewById(R.id.check_alimentacion);
if(selected[position]){
imageView.setImageResource(R.drawable.checkedDrawable);
}
else{
imageView.setImageResource(R.drawable.defaultDrawable);
}
return convertView;
}
Views inside listview are recycled.
You should store each view's state in some kind of array;
For example in List<Integer> itemsImageRes, where itemsImageRes.get(i) will be cached imageResId of listView element at position i
And you should set this image resource to imageView on your adapters getView(int position, View convertView, ViewGroup parent)
And good idea is to use Picasso instead of imageView.setImageResource(R.drawable.drawableid);

Add multiple Images in ListView and get each click event

I want to design my UI like the following Instagram's user activity screen.
With the same features (get profile picture, user Name, liked photos click event event).
How to add multiple Images in a list Item and get the click event for the added images?
You will have to create the ImageViews when creating your Row. Which means you will have to do this during getView(). When Creating a new ImageView, make it clickable using iv.setClickable(true) and attach a new clicklistener onto the image, which will handle the opening of the image once you clicked it. If every Image does the same, you can implement the onClickListener in the adapter and add it to every single image. This might be a smart choice if all images do the same, like opening the image you have just clicked.
within the onClick Callback you will have the view the click originated from, so you can access its id or its Tag, which will tell you what image to show.
If you are using custom imageview for imagesthen make the imageview clickable by using this property...
<ImageView
android:clickable="true"
android:onClick="Click"
android:src="#drawable/img">
</ImageView>
I think this will help you...
You have to use adapter to show multiple image in list view and for click
do something like this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (convertView == null) {
convertView = inf.inflate(R.layout.image, parent, false);
holder = new Holder();
holder.im1 = (ImageView) convertView.findViewById(R.id.imageView1);
holder.im2 = (ImageView) convertView.findViewById(R.id.imageView2);
holder.im1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("i is clicked");
}
});
holder.im2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("2 is clicked");
}
});
convertView.setTag(holder);
} else {
convertView.getTag();
}
return convertView;
}
public class Holder {
ImageView im1, im2;
}

How can I handle onListItemClick(...) based on what view inside the row was clicked?

Question
I have a listView inside a DialogFragment and I want to fire certain callbacks only when certain particular items inside a row are fired. How can I do that?
Basically, I want to do something like this
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final int viewId = view.getId();
if ((viewId == R.id.textView1) || (viewId == R.id.textView2)) {
// do something...
}
which I can't. Read further if you don't know why.
What I tried
I tried to look into the documentation, but the OnItemClickListener callback doesn't offer as a parameter the exact clicked view (the View you can see in the signature is the whole row).
Also, I tried to set a simple onClick callback on the single view in the adapter, but this overrides the listSelector and other behavior a list should have. Reading in the documentation, I found it's explicitly written that we should set callbacks via the onListItemClick(...) method (not via onClick(...)), so I'm looking for a way to do that, using this method, not to override any default list behavior.
I was trying to get this done by working on the xml. To my surprise, I found that if I set a view android:clickable property to true, the onListItemClick callback won't fire (I thought it was the opposite),
so a partial solution would be to set to android:clickable=true every view in the row apart from the one I want to fire the callback, but that is not a solution because if the user clicks where there is padding or white space, the callback will fire. Also, I found that if I set the parent of the row's view to android:clickable=true and the child views I want to handle with the callback to android:clickable=false, this won't work, because apparently the property is not overwritten.
EDIT Sorry for the really bad title this question had before, I didn't even noticed I submitted the question.
new Answer, hope I understood now :)
In your adapters getView, attach an OnClickListener to any view in your layout you want to fire. (more pseudocode)
public class Adapter extends ArrayAdapter<XYZ> {
private int resource;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) convertView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(this.resource, parent, false);
((Button)convertView.findViewById(R.id.YOUR_BUTTON_IN_LAYOUT)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
DOSTUFF();
}
});
return convertView ;
}
}
old Answer:
The position indicates where you are in the list (pseudocode).
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int position,long arg3) {
YOUR_ITEM_BACKED_BY_ADAPTER item = listView.getItemAtPosition(position);
if(item==THE_FIRST_ITEM_IN_LIST) doSomething();
else if(item == THE_LAST_ITEM_IN_LIST) doSomethingElse();
}
});
You can set listeners for other views inside the adapter's getView
public class MyAdapter extends ArrayAdapter<MyItem> implements View.OnClickListener {
public View getView(int position, View convertView, ViewGroup parent) {
// setup the converView inflating it, for simplicity I've removed that code
MyItem item = getItem(position);
text1 = (TextView)convertView.findViewById(R.id.text1);
text2 = (TextView)convertView.findViewById(R.id.text2);
text1.setOnClickListener(this);
// pass the item to use when clicked
text1.setTag(item);
text2.setOnClickListener(this);
text2.setTag(item);
}
public void onClick(View v) {
MyItem item = v.getTag();
switch (v.getId()) {
case R.id.text1:
download(item);
break;
case R.id.text2:
upload(item);
break;
}
}
}
Instead of hardcoding action (eg download) inside the adapter you can pass to it an interface and for example the calling activity can implement that interface

ListView: when move up the finger after long clicking an imageview in each row, the onItemClick function was called

I have a problem about ListView. First of all, this problem only occurs on my real Android device, it works ok on the simulator.
1 I have a ListView: listVew. In each row of it, there's an imageView: picView. I set a long click listener to the image view, in the customized Adapter:
picView.setOnLongClickListener(new View.OnLongClickListener()
{
public boolean onLongClick(View v)
{
...
}
}
When I long click the imageView, it'll move.
2 In my Activity class, I set OnItemClick Listener on every item of the listView.
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
...
}
}
The problem is, in the real device, when I long click the imageView and move up my finger, the OnItemClick method was activate. In the simulator, this never occurred.
Does anyone know why this happened? Thanks a lot!
Add:
I test in emulator for more times, now I think the question can be easy described like this now:
Is there a way to disable other TouchEvent listener when device detect a longclicklistener?
I think you need to intercept the touch event of listview. So try to set OnClickListener in Adapter.getView()
View getView(View convertView, int position, ViewGroup parent){
convertView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
onItemClick(parent, convertView, position, 0);
}
});
return convertView;
}

How to get another item then click on item in gridview?

I want to know how to get different item from selected item in gridview. I have gridview game of matching pictures. When i click on one image then that will flip and then when click on second image first will flip back. How to perform this animation? I want to get imageview of first selected image.
You can do that by saving the last view in a variable
View lastView = null;
// .....
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
if(lastView != null) {
// do something with the last view
}
// the rest of your code
lastView = view;
}
});
Not tested but should work.

Categories

Resources