zhangkaiyue zhangkaiyue - 29 days ago 13
Android Question

How to add click listener to custom relativeLayout

I need a custom relativeLayout because I will use it many times in the different listview. And whichever listview it is, the click listener of the custom relativeLayout is same intent that is to start one activity. So I write click listener in this class, rather than getView() in adapter.
I want to add click listener to custom relativeLayout, and I add the custom relativeLayout as a item to a listview, but when I click the item, listener don't work.
What should I do? Please help.

Custom relativeLayout:

public class SearchCourseItem extends RelativeLayout {

@Bind(R.id.iv_search_course)
ImageView mIvSearchCourse;
@Bind(R.id.tv_search_course_title)
TextView mTvSearchCourseTitle;
@Bind(R.id.tv_search_course_detail)
TextView mTvSearchCourseDetail;

private SearchCourseEntity.DataEntity datas;

public SearchCourseItem(Context context) {
super(context);
init(context);
}

public void init(Context context) {
setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
LayoutInflater.from(context).inflate(R.layout.item_search_course, this);
ButterKnife.bind(this);
}

public void setData(SearchCourseEntity.DataEntity datas) {
this.datas = datas;
mTvSearchCourseDetail.setText(datas.getDescription());
mTvSearchCourseTitle.setText(datas.getName());
ImageLoader.getInstance().displayImage(datas.getPicture(), mIvSearchCourse, UILHelper.getMediumPlaceHolderBaseBuilder().build());
}

@Override
public void setOnClickListener(OnClickListener l) {
Intent intent = new Intent();
if (datas.getType().equals("workout")) {
intent.setClass(getContext(), DailyTrainActivity.class);
intent.putExtra("workoutid", datas.getId());
} else {
intent.setClass(getContext(), PlanActivity.class);
intent.putExtra("planid", datas.getId());
}
getContext().startActivity(intent);
}
}


Adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchCourseItem searchCourseItem;
if (convertView != null && convertView instanceof SearchCourseItem) {
searchCourseItem = (SearchCourseItem) convertView;
} else {
searchCourseItem = new SearchCourseItem(mContext);
}
searchCourseItem.setData(mDataEntities.get(position));
return searchCourseItem;
}

Answer

Don't override

public void setOnClickListener(OnClickListener l)

Move the code to an onClickListener and set it in your getView().

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    SearchCourseItem searchCourseItem;
    if (convertView != null && convertView instanceof SearchCourseItem) {
        searchCourseItem = (SearchCourseItem) convertView;
    } else {
        searchCourseItem = new SearchCourseItem(mContext);
    }
    searchCourseItem.setData(mDataEntities.get(position));
searchCourseItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            if (searchCourseItem.getData().getType().equals("workout")) { //Don't know, if that is what you want!!!
                intent.setClass(getContext(), DailyTrainActivity.class);
                intent.putExtra("workoutid", datas.getId());
            } else {
                intent.setClass(getContext(), PlanActivity.class);
                intent.putExtra("planid", datas.getId());
            }
            getContext().startActivity(intent);   
        }
    });

    return searchCourseItem;
}

EDIT: Alternative you could set an onClickListener in the constructor. Still no need to override setOnClickListener.

public SearchCourseItem(Context context) {
    super(context);
    init(context);
    super.setOnClickListener(...);
}
Comments