首页 > 代码库 > setOnClickListener
setOnClickListener
package com.yuchongxia.internetradio.widget;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;import com.yuchongxia.internetradio.R;public class MessageView extends LinearLayout { private OnClickListener ml; public MessageView(Context context) { super(context); LayoutInflater.from(getContext()).inflate(R.layout.message_view, this); findViewById(R.id.button_refresh).setOnClickListener(onClickListener); } public MessageView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(getContext()).inflate(R.layout.message_view, this); findViewById(R.id.button_refresh).setOnClickListener(onClickListener); } OnClickListener onClickListener = new OnClickListener() { @Override public void onClick(View v) { if (ml != null) { ml.onClick(v); } } }; @Override public void setOnClickListener(OnClickListener l) { ml = l; super.setOnClickListener(ml); }}
package com.yuchongxia.internetradio.widget;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Locale;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.content.Context;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.RelativeLayout;import com.yuchongxia.internetradio.R;import com.yuchongxia.internetradio.constant.Constant;import com.yuchongxia.internetradio.pojo.ContentCategory;import com.yuchongxia.internetradio.util.HttpClientUtil;import com.yuchongxia.internetradio.util.Util;/** * 分类导航组件 * * @author Andy * @date 2013-9-3 */public class NavigationBar extends RelativeLayout { private Context context; private RelativeLayout.LayoutParams params; private RelativeLayout container; // 焦点所在位置 private int focusPosition = 1; private OnMyClickListener onMyClickListener; private OnDataPreparedListener onDataPreparedListener; private List<ContentCategory> list; // private ContentCategoryDao contentCategoryDao; public NavigationBar(Context context, AttributeSet attrs) throws IOException, JSONException { super(context, attrs); this.context = context; LayoutInflater.from(getContext()).inflate(R.layout.navigation_bar, this); container = (RelativeLayout) findViewById(R.id.layout_navigationBar); list = new ArrayList<ContentCategory>(); // contentCategoryDao = new ContentCategoryDao(context); if (Util.isNetworkAvailable(context)) { getRingCategory(); } } // public void writeDB() { // new Thread() { // @Override // public void run() { // super.run(); // // contentCategoryDao.insert(list); // } // }.start(); // } public void parseJson(final String data) { new Thread() { @Override public void run() { try { JSONArray jsonArray = new JSONArray(data); if (jsonArray == null || jsonArray.length() == 0) { handler.sendEmptyMessage(0); return; } Log.i(Constant.TAG, "开始解析网络获取的数据"); for (int i = 0; i < jsonArray.length(); i++) { ContentCategory contentCategory = new ContentCategory(); JSONObject object = (JSONObject) jsonArray.getJSONObject(i); int categoryId = object.getInt("categoryId"); String categoryName = object.getString("categoryName"); int sort = object.getInt("order"); int publishTime = object.getInt("publishTime"); contentCategory.setCategoryId(categoryId); contentCategory.setCategoryName(categoryName); contentCategory.setSort(sort); contentCategory.setPublishTime(publishTime); list.add(contentCategory); } Log.i(Constant.TAG, "解析后数据 jsList size:" + list.size()); // 全部 ContentCategory temp = new ContentCategory(); temp.setCategoryId(-1); temp.setCategoryName(context.getString(R.string.all)); list.add(0, temp); temp = new ContentCategory(); temp.setCategoryId(-2); temp.setCategoryName(context.getString(R.string.collect)); list.add(0, temp); temp = new ContentCategory(); temp.setCategoryId(-3); temp.setCategoryName(context.getString(R.string.local_radio)); list.add(temp); temp = new ContentCategory(); temp.setCategoryId(-4); temp.setCategoryName(context.getString(R.string.more)); list.add(temp); handler.sendEmptyMessage(1); } catch (JSONException e) { Log.i(Constant.TAG, "解析数据时出错"); e.printStackTrace(); } } }.start(); } public void getRingCategory() { new Thread() { @Override public void run() { String data = HttpClientUtil.get(Constant.CONTENT_CATEGOARY_URL); parseJson(data); } }.start(); } public void layout() { container.removeAllViews(); NavigationButton btn; int cnt = list.size(); if (onDataPreparedListener != null) { Log.i(Constant.TAG, "prepared"); onDataPreparedListener.onPrepared(list); } for (int i = 0; i < cnt; i++) { params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); btn = new NavigationButton(context); btn.setId(i + 1); btn.setText(list.get(i).getCategoryName()); btn.setOnClickListener(clickListener); if (i == 0) { btn.setChoosed(true); } String language = Locale.getDefault().getLanguage(); String country = Locale.getDefault().getCountry(); // zh CN // zh TW // en ** if (language.equals("zh") && country.equals("CN")) { } else if (language.equals("zh") && country.equals("TW")) { } else { } if (i > 0) { params.addRule(RelativeLayout.BELOW, i); } container.addView(btn, params); } } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 0) { Log.i(Constant.TAG, "无法获取数据"); } else if (msg.what == 1) { layout(); } } }; /** * 刷新NavigationBar */ public void refresh() { getRingCategory(); // 恢复当前位置标示 focusPosition = 1; } /** * 取 NavigationBar 第一项的分类id * * @return 第一项的分类id */ // public String getFirst() { // if (list.size() != 0) { // return list.get(0).getCategoryId(); // } else { // return ""; // } // } /** * 焦点是否在第一项 */ public boolean isFocusOnFirst() { return focusPosition == 1 ? true : false; } public void setChoosed(int id) { NavigationButton btn = (NavigationButton) findViewById(focusPosition); btn.setChoosed(false); btn = (NavigationButton) findViewById(id); btn.setChoosed(true); // 标记当前选中项 focusPosition = id; } OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { int id = v.getId(); // 取消选中 NavigationButton btn = (NavigationButton) findViewById(focusPosition); btn.setChoosed(false); focusPosition = id; // 重新选中 btn = (NavigationButton) findViewById(id); btn.setChoosed(true); if (onMyClickListener != null && list.size() != 0) { onMyClickListener.onClick(id - 1); } } }; /** * 回调接口 * */ public interface OnDataPreparedListener { public void onPrepared(List<ContentCategory> list); } public interface OnMyClickListener { public void onClick(int buttonId); } /** * 设置一个监听,供外部调用的方法 */ public void setOnMyClickListener(OnMyClickListener listener) { this.onMyClickListener = listener; } /** * 设置一个监听,供外部调用的方法 */ public void setOnDataPreparedListener(OnDataPreparedListener onDataPreparedListener) { this.onDataPreparedListener = onDataPreparedListener; }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。