国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

簡(jiǎn)單實(shí)現(xiàn)Android滾動(dòng)公告欄

2019-12-12 03:54:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

實(shí)現(xiàn)的效果,是一個(gè)滾動(dòng)的公告欄,是這樣的:

可以看到這個(gè)公告欄一方面是滾動(dòng),另外一方面是可點(diǎn)擊。

實(shí)現(xiàn)的思路:

1.textView放在ViewFlipper中實(shí)現(xiàn)滑動(dòng)效果(可設(shè)置左右、或者上下滾動(dòng)),很明顯這應(yīng)該是自定義view;

2.利用textView的點(diǎn)擊事件即可實(shí)現(xiàn)點(diǎn)擊;

OK,先看看自定義view的代碼:

public class MarqueeTextView extends LinearLayout {   private Context mContext;  private ViewFlipper viewFlipper;  private View marqueeTextView;  private String[] textArrays;  private MarqueeTextViewClickListener marqueeTextViewClickListener;   public MarqueeTextView(Context context) {  super(context);  mContext = context;  initBasicView();  }    public MarqueeTextView(Context context, AttributeSet attrs) {  super(context, attrs);  mContext = context;  initBasicView();  }   public void setTextArraysAndClickListener(String[] textArrays, MarqueeTextViewClickListener marqueeTextViewClickListener) {//1.設(shè)置數(shù)據(jù)源;2.設(shè)置監(jiān)聽(tīng)回調(diào)(將textView點(diǎn)擊事件傳遞到目標(biāo)界面進(jìn)行操作)  this.textArrays = textArrays;  this.marqueeTextViewClickListener = marqueeTextViewClickListener;  initMarqueeTextView(textArrays, marqueeTextViewClickListener);  }   public void initBasicView() {//加載布局,初始化ViewFlipper組件及效果  marqueeTextView = LayoutInflater.from(mContext).inflate(R.layout.marquee_textview_layout, null);  LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);  addView(marqueeTextView, layoutParams);  viewFlipper = (ViewFlipper) marqueeTextView.findViewById(R.id.viewFlipper);  viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));//設(shè)置上下的動(dòng)畫(huà)效果(自定義動(dòng)畫(huà),所以改左右也很簡(jiǎn)單)  viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top));  viewFlipper.startFlipping();  }   public void initMarqueeTextView(String[] textArrays, MarqueeTextViewClickListener marqueeTextViewClickListener) {  if (textArrays.length == 0) {   return;  }   int i = 0;  viewFlipper.removeAllViews();  while (i < textArrays.length) {   TextView textView = new TextView(mContext);   textView.setText(textArrays[i]);   textView.setOnClickListener(marqueeTextViewClickListener);   LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);   viewFlipper.addView(textView, lp);   i++;  }  }   public void releaseResources() {  if (marqueeTextView != null) {   if (viewFlipper != null) {   viewFlipper.stopFlipping();   viewFlipper.removeAllViews();   viewFlipper = null;   }   marqueeTextView = null;  }  }  } 

然后,主Activity異常簡(jiǎn)單(還是封裝得好):

public class MainActivity extends AppCompatActivity {  private MarqueeTextView marqueeTv;  private String [] textArrays = new String[]{"this is content No.1","this is content No.2","this is content No.3"};   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  marqueeTv = (MarqueeTextView) findViewById(R.id.marqueeTv);   marqueeTv.setTextArraysAndClickListener(textArrays, new MarqueeTextViewClickListener() {   @Override   public void onClick(View view) {   startActivity(new Intent(MainActivity.this,AnotherActivity.class));   }  });  }   @Override  protected void onDestroy() {  marqueeTv.releaseResources();  super.onDestroy();  } } 

Git地址>>https://github.com/ganshenml/MarqueeTextViewApp

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宾川县| 冕宁县| 亳州市| 乡宁县| 平定县| 中超| 顺平县| 佳木斯市| 平定县| 阳西县| 三门峡市| 溧阳市| 富宁县| 松原市| 滕州市| 陇西县| 丰原市| 四平市| 乌拉特后旗| 余庆县| 阜南县| 广水市| 遂川县| 开化县| 东港市| 石河子市| 深水埗区| 苍梧县| 大丰市| 右玉县| 自治县| 华容县| 石渠县| 巴彦淖尔市| 九台市| 阿拉善右旗| 梓潼县| 利川市| 灵寿县| 隆林| 中宁县|