摘要:網易新聞客戶端博主要實現的效果代碼看了文件相信所有人都明朗了吧,其實每個選項卡都是的組合標題欄操作設置監聽事件這里可以進行點擊相應選項卡后進行的觸發事件,一般為顯示新聞頁面每次點擊新的選項卡都調用此方法將所有選項卡都置換為未點擊狀態,
網易新聞客戶端:
博主要實現的效果:
代碼:
view_header.xml:
activity_news.xml:
看了xml文件相信所有人都明朗了吧,其實每個選項卡都是TextView+ImageView的組合
News.java:
public class News extends Activity { private LinearLayout ll_top_left; private TextView tv_title; private TextView btn_headline, btn_internation, btn_inland, btn_video, btn_appraisal, btn_complaint; private ImageView view_headline, view_internation, view_inland, view_video, v iew_appraisal, view_complaint; private int type = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news); initHeader(); initTab(); } private void initHeader() { //標題欄操作 ll_top_left = (LinearLayout) findViewById(R.id.ll_header_left); tv_title = (TextView) findViewById(R.id.tv_header_title); tv_title.setText(getString(R.string.head_title_news)); ll_top_left.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub finish(); } });
}
private void initTab() { btn_headline = (TextView) findViewById(R.id.tv_headline); btn_internation = (TextView) findViewById(R.id.tv_international); btn_inland = (TextView) findViewById(R.id.tv_inland); btn_video = (TextView) findViewById(R.id.tv_video); btn_appraisal = (TextView) findViewById(R.id.tv_appraisal); btn_complaint = (TextView) findViewById(R.id.tv_complaint); view_headline = (ImageView) findViewById(R.id.view_news_headline); view_internation = (ImageView) findViewById(R.id.view_news_internation); view_inland = (ImageView) findViewById(R.id.view_news_inland); view_video = (ImageView) findViewById(R.id.view_news_video); view_appraisal = (ImageView) findViewById(R.id.view_news_appraisal); view_complaint = (ImageView) findViewById(R.id.view_news_complaint); //設置監聽事件 btn_headline.setOnClickListener(new MyTabClickListener()); btn_internation.setOnClickListener(new MyTabClickListener()); btn_inland.setOnClickListener(new MyTabClickListener()); btn_video.setOnClickListener(new MyTabClickListener()); btn_appraisal.setOnClickListener(new MyTabClickListener()); btn_complaint.setOnClickListener(new MyTabClickListener()); onType(type);
}
private class MyTabClickListener implements OnClickListener { @Override public void onClick(View v) { onFocused(v.getId()); switch (v.getId()) { case R.id.tv_headline: type = 1; break; case R.id.tv_international: type = 2; break; case R.id.tv_inland: type = 3; break; case R.id.tv_video: type = 4; break; case R.id.tv_appraisal: type = 5; break; case R.id.tv_complaint: type = 6; break; default: break; } //這里可以進行點擊相應選項卡后進行的觸發事件,一般為顯示新聞頁面 }
}
private void initTabState(){ 每次點擊新的選項卡都調用此方法將所有選項卡都置換為未點擊狀態,這么做是有點麻煩,但是我還沒想到更加優雅的方法囧... view_headline.setVisibility(View.INVISIBLE); view_internation.setVisibility(View.INVISIBLE); view_inland.setVisibility(View.INVISIBLE); view_video.setVisibility(View.INVISIBLE); view_appraisal.setVisibility(View.INVISIBLE); view_complaint.setVisibility(View.INVISIBLE); btn_headline.setTextColor(getResources().getColor(R.color.color_news_title_unchecked)); btn_internation.setTextColor(getResources().getColor(R.color.color_news_title_unchecked)); btn_inland.setTextColor(getResources().getColor(R.color.color_news_title_unchecked)); btn_video.setTextColor(getResources().getColor(R.color.color_news_title_unchecked)); btn_appraisal.setTextColor(getResources().getColor(R.color.color_news_title_unchecked)); btn_complaint.setTextColor(getResources().getColor(R.color.color_news_title_unchecked));
}
private void onFocused(int fid) { 點擊選項卡時調用的方法 initTabState(); switch (fid) { case R.id.tv_headline: view_headline.setVisibility(View.VISIBLE); btn_headline.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case R.id.tv_international: view_internation.setVisibility(View.VISIBLE); btn_internation.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case R.id.tv_inland: view_inland.setVisibility(View.VISIBLE); btn_inland.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case R.id.tv_video: view_video.setVisibility(View.VISIBLE); btn_video.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case R.id.tv_appraisal: view_appraisal.setVisibility(View.VISIBLE); btn_appraisal.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case R.id.tv_complaint: view_complaint.setVisibility(View.VISIBLE); btn_complaint.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; }
}
private void onType(int type) { 觸發監聽事件后調用 initTabState(); switch (type) { case 1: view_headline.setVisibility(View.VISIBLE); btn_headline.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case 2: view_internation.setVisibility(View.VISIBLE); btn_internation.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case 3: view_inland.setVisibility(View.VISIBLE); btn_inland.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case 4: view_video.setVisibility(View.VISIBLE); btn_video.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case 5: view_appraisal.setVisibility(View.VISIBLE); btn_appraisal.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; case 6: view_complaint.setVisibility(View.VISIBLE); btn_complaint.setTextColor(getResources().getColor(R.color.color_news_title_checked)); break; }
}
}
關于GONE和INVISIBLE的區別有一篇中文博客講的比較好:http://blog.csdn.net/chindroid/article/details/8000713
當控件visibility屬性為INVISIBLE時,界面保留了view控件所占有的空間;而控件屬性為GONE時,界面則不保留view控件所占有的空間。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/64130.html
閱讀 1988·2021-11-19 09:40
閱讀 1931·2021-09-28 09:36
閱讀 2279·2021-09-22 10:02
閱讀 2724·2019-08-30 14:00
閱讀 1948·2019-08-29 15:31
閱讀 2893·2019-08-29 15:11
閱讀 2905·2019-08-29 13:04
閱讀 1080·2019-08-27 10:55