先上效果图,选择不同的模块,滑动会通过动画形式滑过去,这样的适合新闻client多种栏目的展示:
这么写Layout:
代码中在string.xml中增加数据://这个能够把子栏目都加到column_title_layout中
- 科技
- 財经
- 体育
- 本地
- 最新
- 百家
- 娱乐
private void initTab() { String[] resource = this.getResources().getStringArray(R.array.all_choice); for (int j = 0; j < resource.length; j++) { String name = resource[j]; array.add(name); } this.columnTitleLayout.removeAllViews(); int j = this.array.size(); if (j <= 5) { this.scrollToRight.setVisibility(View.INVISIBLE); this.scrollToLeft.setVisibility(View.INVISIBLE); } currTabIndex = 0; int i = 0; animImage.setBackgroundResource(R.drawable.slidebar); for (i = 0; i < array.size(); i++) { String str = array.get(i); TextView ColumnTextView = new TextView(this); ColumnTextView.setText(str); ColumnTextView.setTag(i); ColumnTextView.setPadding(18, 2, 15, 4); ColumnTextView.setOnClickListener(this); ColumnTextView.setTextAppearance(this, R.style.column_tx_style); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); columnTitleLayout.addView(ColumnTextView, params); } TextView MoreColumnTextView = new TextView(this); MoreColumnTextView.setTag(i); CharSequence localCharSequence = getResources().getText(R.string.more_column); MoreColumnTextView.setText(localCharSequence); MoreColumnTextView.setPadding(18, 2, 15, 4); MoreColumnTextView.setTextAppearance(this, R.style.column_tx_style); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); columnTitleLayout.addView(MoreColumnTextView, params); }
在点击子栏目的时候启动动画:
@Override public void onClick(View v) { int k = (Integer)v.getTag(); lastTabIndex = currTabIndex; currTabIndex = k; String text = ((TextView) v).getText().toString(); if (lastTabIndex != currTabIndex) { if (currTabIndex == array.size()) { return; } showAnimation(); } }动画採用TranslateAnimation animation.setFillAfter(true);
private void showAnimation() { if (lastTabIndex == currTabIndex) { return; } ((TextView) columnTitleLayout.getChildAt(lastTabIndex)).setTextColor(R.drawable.white); int widgetItemWidth = ((TextView) columnTitleLayout.getChildAt(lastTabIndex)).getWidth(); int fromX = lastTabIndex * widgetItemWidth; int toX = currTabIndex * widgetItemWidth; Log.v("test", "widgetItemWidth" + widgetItemWidth + "fromX:" + fromX + " toX:" + toX); TranslateAnimation animation = new TranslateAnimation(fromX, toX, 0, 0); animation.setDuration(500); animation.setFillAfter(true); animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { ((TextView) columnTitleLayout.getChildAt(lastTabIndex)).setTextColor(MainActivity.this.getResources().getColor(R.drawable.gray2)); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { ((TextView) columnTitleLayout.getChildAt(currTabIndex)).setTextColor(MainActivity.this.getResources().getColor(R.drawable.white)); lastTabIndex = currTabIndex; } }); animImage.startAnimation(animation); }代码能够在 下载