Android實(shí)現(xiàn)類似網(wǎng)易新聞選項(xiàng)卡動(dòng)態(tài)滑動(dòng)效果
本文會(huì)實(shí)現(xiàn)一個(gè)類似網(wǎng)易新聞(不說網(wǎng)易新聞大家可能不知道大概是什么樣子)點(diǎn)擊超多選項(xiàng)卡,選項(xiàng)卡動(dòng)態(tài)滑動(dòng)的效果。
首先來看看布局,就是用HorizontalScrollView控件來實(shí)現(xiàn)滑動(dòng)的效果,里面包含了一個(gè)布局。
接下來我們?cè)趏nCreat方法中加載布局和構(gòu)建我們需要顯示的數(shù)據(jù)
<code class="hljs avrasm"> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tabbar); tv_tabname= (TextView) this.findViewById(R.id.tv_tabname); titleList = new ArrayList<string>(); titleList.add("推薦"); titleList.add("熱點(diǎn)"); titleList.add("北京"); titleList.add("體育"); titleList.add("娛樂"); titleList.add("足球"); titleList.add("巴薩"); titleList.add("汽車"); }</string></code>
加載布局,用RadioGroup動(dòng)態(tài)的加載多個(gè)自定義的RadioButton
<code class="hljs avrasm">hs_activity_tabbar= (HorizontalScrollView) this.findViewById(R.id.hs_activity_tabbar); ll_activity_tabbar_content= (LinearLayout) this.findViewById(R.id.ll_activity_tabbar_content); //選項(xiàng)卡布局 myRadioGroup = new RadioGroup(this); myRadioGroup.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); myRadioGroup.setOrientation(LinearLayout.HORIZONTAL); ll_activity_tabbar_content.addView(myRadioGroup); for (int i = 0; i < titleList.size(); i++) { String channel = titleList.get(i); RadioButton radio = new RadioButton(this); radio.setButtonDrawable(android.R.color.transparent); radio.setBackgroundResource(R.drawable.radiobtn_selector); ColorStateList csl = getResources().getColorStateList(R.color.radiobtn_text_color); radio.setTextColor(csl); LinearLayout.LayoutParams l = new LinearLayout.LayoutParams((int) SizeHelper.dp2px(this, 80), ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER); radio.setLayoutParams(l); radio.setTextSize(15); radio.setGravity(Gravity.CENTER); radio.setText(channel); radio.setTag(channel); myRadioGroup.addView(radio); }</code>
最后也就點(diǎn)擊選項(xiàng)卡的時(shí)候會(huì)有一個(gè)動(dòng)態(tài)滑動(dòng)的效果,其實(shí)就是利用HorizontalScrollView的smoothScrollTo方法來實(shí)現(xiàn)的
<code class="hljs cs"> myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { int radioButtonId = group.getCheckedRadioButtonId(); //根據(jù)ID獲取RadioButton的實(shí)例 RadioButton rb = (RadioButton) findViewById(radioButtonId); channel = (String) rb.getTag(); mCurrentCheckedRadioLeft = rb.getLeft();//更新當(dāng)前按鈕距離左邊的距離 int width=(int) SizeHelper.dp2px(TabbarActivity.this, 140); hs_activity_tabbar.smoothScrollTo((int) mCurrentCheckedRadioLeft - width, 0); tv_tabname.setText(channel); } }); //設(shè)定默認(rèn)被選中的選項(xiàng)卡為第一項(xiàng) if (!titleList.isEmpty()) { myRadioGroup.check(myRadioGroup.getChildAt(0).getId()); }</code>
dp2px方法如下用來將dp轉(zhuǎn)換為px:
<code class="hljs java"> public static float dp2px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (dp * scale); }</code>
全部代碼為:
<code class="hljs avrasm">package com.example.liuwangshu.myslidetabbar; import android.content.res.ColorStateList; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.ViewGroup; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class TabbarActivity extends AppCompatActivity { private HorizontalScrollView hs_activity_tabbar; private RadioGroup myRadioGroup; private List<string> titleList; private LinearLayout ll_activity_tabbar_content; private float mCurrentCheckedRadioLeft;//當(dāng)前被選中的RadioButton距離左側(cè)的距離 private String channel; private TextView tv_tabname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tabbar); tv_tabname= (TextView) this.findViewById(R.id.tv_tabname); titleList = new ArrayList<string>(); titleList.add("推薦"); titleList.add("熱點(diǎn)"); titleList.add("北京"); titleList.add("體育"); titleList.add("娛樂"); titleList.add("足球"); titleList.add("巴薩"); titleList.add("汽車"); initGroup(); } private void initGroup() { hs_activity_tabbar= (HorizontalScrollView) this.findViewById(R.id.hs_activity_tabbar); ll_activity_tabbar_content= (LinearLayout) this.findViewById(R.id.ll_activity_tabbar_content); //選項(xiàng)卡布局 myRadioGroup = new RadioGroup(this); myRadioGroup.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); myRadioGroup.setOrientation(LinearLayout.HORIZONTAL); ll_activity_tabbar_content.addView(myRadioGroup); for (int i = 0; i < titleList.size(); i++) { String channel = titleList.get(i); RadioButton radio = new RadioButton(this); radio.setButtonDrawable(android.R.color.transparent); radio.setBackgroundResource(R.drawable.radiobtn_selector); ColorStateList csl = getResources().getColorStateList(R.color.radiobtn_text_color); radio.setTextColor(csl); LinearLayout.LayoutParams l = new LinearLayout.LayoutParams((int) SizeHelper.dp2px(this, 80), ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER); radio.setLayoutParams(l); radio.setTextSize(15); radio.setGravity(Gravity.CENTER); radio.setText(channel); radio.setTag(channel); myRadioGroup.addView(radio); } myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { int radioButtonId = group.getCheckedRadioButtonId(); //根據(jù)ID獲取RadioButton的實(shí)例 RadioButton rb = (RadioButton) findViewById(radioButtonId); channel = (String) rb.getTag(); mCurrentCheckedRadioLeft = rb.getLeft();//更新當(dāng)前按鈕距離左邊的距離 int width=(int) SizeHelper.dp2px(TabbarActivity.this, 140); hs_activity_tabbar.smoothScrollTo((int) mCurrentCheckedRadioLeft - width, 0); tv_tabname.setText(channel); } }); //設(shè)定默認(rèn)被選中的選項(xiàng)卡為第一項(xiàng) if (!titleList.isEmpty()) { myRadioGroup.check(myRadioGroup.getChildAt(0).getId()); } } } </string></string></code>
來看看效果
以上所述是小編給大家介紹的Android實(shí)現(xiàn)類似網(wǎng)易新聞選項(xiàng)卡動(dòng)態(tài)滑動(dòng)效果,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android項(xiàng)目實(shí)戰(zhàn)之仿網(wǎng)易新聞的頁面(RecyclerView )
- Android實(shí)現(xiàn)仿網(wǎng)易新聞的頂部導(dǎo)航指示器
- Android實(shí)現(xiàn)仿網(wǎng)易新聞主界面設(shè)計(jì)
- Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁效果
- Android 仿網(wǎng)易新聞客戶端分類排序功能
- Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端
- Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(1)
- Android組件DrawerLayout仿網(wǎng)易新聞v4.4側(cè)滑菜單
- Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(2)
- Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼
相關(guān)文章
談?wù)凙ndroid Fragments 詳細(xì)使用
本篇文章主要介紹了Android Fragments 詳細(xì)使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12Android中AutoCompleteTextView自動(dòng)提示
這篇文章主要為大家詳細(xì)介紹了Android中AutoCompleteTextView自動(dòng)提示的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12從零開始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
這篇文章主要介紹了android實(shí)現(xiàn)的計(jì)算器功能示例,可以加減乘除;可以倒退,可以清空文本,大家參考使用吧2014-02-02基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能
這篇文章主要介紹了基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能,介紹了如何錄音,如何播放本地和遠(yuǎn)程音頻文件,以及如何實(shí)現(xiàn)動(dòng)畫,在錄制完音頻文件后如何上傳,這些都是我們平常使用這個(gè)功能會(huì)遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-05-05Android Handler內(nèi)存泄漏詳解及其解決方案
在android開發(fā)過程中,我們可能會(huì)遇到過令人奔潰的OOM異常,這篇文章主要介紹了Android Handler內(nèi)存泄漏詳解及其解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08android Bitmap圓角與倒影的具體實(shí)現(xiàn)代碼
android Bitmap圓角與倒影的具體實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-06-06RecyclerView實(shí)現(xiàn)查看更多及收起
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)查看更多及收起,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01