Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply click listener on each view #9

Open
deepakcipl opened this issue Dec 9, 2014 · 8 comments
Open

Apply click listener on each view #9

deepakcipl opened this issue Dec 9, 2014 · 8 comments

Comments

@deepakcipl
Copy link

Hi .. I have an issue regarding the library. I have a favorite icon on each frame as an Image view and I want to make it favorite or remove favorite by click on it. I am unable to apply the click listener on that particular view. How can I achieve that ? Please help..
device-2014-12-09-181941

@applm
Copy link
Owner

applm commented Dec 9, 2014

Hi,
Use onItemClickListener.

2014-12-09 13:50 GMT+01:00 deepakcipl [email protected]:

Hi .. I have an issue regarding the library. I have a favorite icon on
each frame as an Image view and I want to make it favorite or remove
favorite by click on it. I am unable to apply the click listener on that
particular view. How can I achieve that ? Please help..
[image: device-2014-12-09-181941]
https://cloud.githubusercontent.com/assets/7940903/5357638/09e63ffe-7fd0-11e4-8f88-4268821b00d3.png


Reply to this email directly or view it on GitHub
#9.

@deepakcipl
Copy link
Author

Sorry.. Not able to do that.. :( onItemClickListener is undefined for image view. You can check my adapter class as follows:-

package app.myspot.adapter;

import java.util.List;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import app.myspot.R;
import app.myspot.dataclass.DataBean;
import app.myspot.tools.AppTypeFace;

public class SampleImageAdapter extends BaseAdapter {
private Context mContext;
private List mBeans ;
/private view holder class/

public SampleImageAdapter(Context c, List<DataBean> mBeans) {
    mContext=c;
    this.mBeans = mBeans;
} 

public int getCount() { 

// Log.e("size:--", ""+mActorName.size());
return mBeans.size();
}

public Object getItem(int position) {
    return position;
} 

public long getItemId(int position) {
    return position;
} 

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    LayoutInflater mInflater = (LayoutInflater) 
            mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.new_row_list_data, null);
        holder = new ViewHolder();
        holder.mPostDataTV = (TextView) convertView.findViewById(R.id.tv_data_container);
        holder.mPostDataPublisherTV = (TextView) convertView.findViewById(R.id.tv_post_publisher_name);
        holder.tv_post_publisher = (TextView) convertView.findViewById(R.id.tv_post_publisher);
        holder.iv_post_favourite = (ImageView) convertView.findViewById(R.id.iv_post_favourite);

        holder.mPostDataTV.setTypeface(AppTypeFace.getInstance(mContext).getRobotoMediumTypeface());
        holder.mPostDataPublisherTV.setTypeface(AppTypeFace.getInstance(mContext).getRobotoMediumTypeface());
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
        holder.tv_post_publisher.setVisibility(TextView.GONE);
        holder.iv_post_favourite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.fav_icon_off));


    }


    holder.mPostDataPublisherTV .setText(mBeans.get(position).getActor_name());
    holder.mPostDataTV.setText(mBeans.get(position).getActor_desc());

    if(mBeans.get(position).isNewPost()){
        holder.tv_post_publisher.setVisibility(TextView.VISIBLE);
    }

    if(mBeans.get(position).isFavourite()){
        holder.iv_post_favourite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.fav_icon));
    }

    holder.iv_post_favourite.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            mBeans.get(position).setFavourite(true);
            if(mBeans.get(position).isFavourite()){
                holder.iv_post_favourite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.fav_icon));

            }

        }
    });




    return convertView;

} 


public static class ViewHolder {
     private TextView mPostDataTV,mPostDataPublisherTV,tv_post_publisher;
     private ImageView iv_post_favourite;

}

}

and set adapter in my activity class as follows:-

adapter=new SampleImageAdapter(MainActivity.this,mBeans);
coverflow.setAdapter(adapter);

Please help me to sort out this problem.

@applm
Copy link
Owner

applm commented Dec 9, 2014

It is on cover flow.

2014-12-09 14:06 GMT+01:00 deepakcipl [email protected]:

Sorry.. Not able to do that.. :( onItemClickListener is undefined for
image view. You can check my adapter class as follows:-

package app.myspot.adapter;

import java.util.List;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import app.myspot.R;
import app.myspot.dataclass.DataBean;
import app.myspot.tools.AppTypeFace;

public class SampleImageAdapter extends BaseAdapter {
private Context mContext;
private List mBeans ;
/private view holder class/

public SampleImageAdapter(Context c, List mBeans) {
mContext=c;
this.mBeans = mBeans;
}

public int getCount() {

// Log.e("size:--", ""+mActorName.size());
return mBeans.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public View getView(final int position, View convertView, ViewGroup parent) {

final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater)
        mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
    convertView = mInflater.inflate(R.layout.new_row_list_data, null);
    holder = new ViewHolder();
    holder.mPostDataTV = (TextView) convertView.findViewById(R.id.tv_data_container);
    holder.mPostDataPublisherTV = (TextView) convertView.findViewById(R.id.tv_post_publisher_name);
    holder.tv_post_publisher = (TextView) convertView.findViewById(R.id.tv_post_publisher);
    holder.iv_post_favourite = (ImageView) convertView.findViewById(R.id.iv_post_favourite);

    holder.mPostDataTV.setTypeface(AppTypeFace.getInstance(mContext).getRobotoMediumTypeface());
    holder.mPostDataPublisherTV.setTypeface(AppTypeFace.getInstance(mContext).getRobotoMediumTypeface());
    convertView.setTag(holder);
}
else {
    holder = (ViewHolder) convertView.getTag();
    holder.tv_post_publisher.setVisibility(TextView.GONE);
    holder.iv_post_favourite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.fav_icon_off));


}


holder.mPostDataPublisherTV .setText(mBeans.get(position).getActor_name());
holder.mPostDataTV.setText(mBeans.get(position).getActor_desc());

if(mBeans.get(position).isNewPost()){
    holder.tv_post_publisher.setVisibility(TextView.VISIBLE);
}

if(mBeans.get(position).isFavourite()){
    holder.iv_post_favourite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.fav_icon));
}

holder.iv_post_favourite.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        mBeans.get(position).setFavourite(true);
        if(mBeans.get(position).isFavourite()){
            holder.iv_post_favourite.setImageDrawable(mContext.getResources().getDrawable(R.drawable.fav_icon));

        }

    }
});




return convertView;

}

public static class ViewHolder {
private TextView mPostDataTV,mPostDataPublisherTV,tv_post_publisher;
private ImageView iv_post_favourite;

}

}

and set adapter in my activity class as follows:-

adapter=new SampleImageAdapter(MainActivity.this,mBeans);
coverflow.setAdapter(adapter);

Please help me to sort out this problem.


Reply to this email directly or view it on GitHub
#9 (comment).

@deepakcipl
Copy link
Author

Okay.. If I apply setOnItemClickListener on coverflow so in my opinion the listener should work on whole view but as per above snap which I have posted ,I just only want to apply listener on favorite icon image. So how can I achieve that?

@applm
Copy link
Owner

applm commented Dec 9, 2014

Item click listener gives you view which was clicked. You can ignore all
other views but the one you want.

2014-12-09 14:26 GMT+01:00 deepakcipl [email protected]:

Okay.. If I apply setOnItemClickListener on coverflow so in my opinion the
listener should work on whole view but as per above snap which I have
posted ,I just only want to apply listener on favorite icon image. So how
can I achieve that?


Reply to this email directly or view it on GitHub
#9 (comment).

@deepakcipl
Copy link
Author

How Can I achieve that? Can you please provide me a small code to achieve that ?

@applm
Copy link
Owner

applm commented Dec 9, 2014

I dont have time to teach you how Android works. This listener can be found
on any adapter view. Use google.

2014-12-09 15:09 GMT+01:00 deepakcipl [email protected]:

How Can I achieve that? Can you please provide me a small code to achieve
that ?


Reply to this email directly or view it on GitHub
#9 (comment).

@Nesh-Brindha
Copy link

Can you briefly explain how we can add click listener to every view in listview???? i have spend two days to clear this issue but no luck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants