高仿网易新闻顶部滑动条效果实现代码

2016-02-19 11:00 27 1 收藏

有了下面这个高仿网易新闻顶部滑动条效果实现代码教程,不懂高仿网易新闻顶部滑动条效果实现代码的也能装懂了,赶紧get起来装逼一下吧!

【 tulaoshi.com - 编程语言 】


这个是网易新闻的主界面,我们知道底部可以用tabhost实现,这个很容易,我们在其他软件中也会经常用到。
至于顶部的滑动条,个人感觉还是比较漂亮的所以今天也模仿了下,网易顶部滑动条的效果,由于初次模仿这种效果,可能有些地方还不够完美,不过基本已经实现,希望大家能够喜欢。
废话不多说,下面上代码:
首先是布局layout下的main.xml
代码如下:

?xmlversion="1.0"encoding="utf-8"?
RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
LinearLayout
android:id="@+id/layoutBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/big_button_up"
android:orientation="horizontal"
RelativeLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
TextView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="新闻"/
/RelativeLayout
RelativeLayout
android:id="@+id/layout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
TextView
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="体育"/
/RelativeLayout
RelativeLayout
android:id="@+id/layout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
TextView
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="娱乐"/
/RelativeLayout
RelativeLayout
android:id="@+id/layout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
TextView
android:id="@+id/tab4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="更多"/
/RelativeLayout
/LinearLayout
LinearLayout
android:id="@+id/page"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/layoutBar"
android:background="#ffffff"
android:orientation="vertical"

/LinearLayout
/RelativeLayout

下面是核心类,
代码如下:

packagecn.com.karl.slider;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Gravity;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.ViewGroup.LayoutParams;
importandroid.view.animation.TranslateAnimation;
importandroid.widget.LinearLayout;
importandroid.widget.RelativeLayout;
importandroid.widget.TextView;
publicclassSliderBarActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
privateRelativeLayoutlayout;

privateRelativeLayoutlayout1;
privateRelativeLayoutlayout2;
privateRelativeLayoutlayout3;
privateRelativeLayoutlayout4;
privateTextViewtab1;
privateTextViewtab2;
privateTextViewtab3;
privateTextViewtab4;
privateTextViewfirst;
privateintcurrent=1;

privateLinearLayoutpage;

privatebooleanisAdd=false;
privateintselect_width;
privateintselect_height;
privateintfirstLeft;
privateintstartLeft;

@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
privatevoidinit(){
layout=(RelativeLayout)findViewById(R.id.root);

layout1=(RelativeLayout)findViewById(R.id.layout1);
layout2=(RelativeLayout)findViewById(R.id.layout2);
layout3=(RelativeLayout)findViewById(R.id.layout3);
layout4=(RelativeLayout)findViewById(R.id.layout4);

page=(LinearLayout)this.findViewById(R.id.page);

tab1=(TextView)findViewById(R.id.tab1);
tab1.setOnClickListener(onClickListener);
tab2=(TextView)findViewById(R.id.tab2);
tab2.setOnClickListener(onClickListener);
tab3=(TextView)findViewById(R.id.tab3);
tab3.setOnClickListener(onClickListener);
tab4=(TextView)findViewById(R.id.tab4);
tab4.setOnClickListener(onClickListener);



RelativeLayout.LayoutParamsrl=newRelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
first=newTextView(this);
first.setTag("first");
first.setGravity(Gravity.CENTER);
first.setBackgroundResource(R.drawable.slidebar);
first.setText(tab1.getText());
Viewview1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page1,null);
page.addView(view1);

switch(current){
case1:
layout1.addView(first,rl);
current=R.id.tab1;
break;
case2:
layout2.addView(first,rl);
current=R.id.tab2;
break;
case3:
layout3.addView(first,rl);
current=R.id.tab3;
break;
case4:
layout4.addView(first,rl);
current=R.id.tab4;
break;
default:
break;
}

}

privatevoidreplace(){
switch(current){
caseR.id.tab1:
changeTop(layout1);
break;
caseR.id.tab2:
changeTop(layout2);
break;
caseR.id.tab3:
changeTop(layout3);
break;
caseR.id.tab4:
changeTop(layout4);
break;
default:
break;
}
}
privatevoidchangeTop(RelativeLayoutrelativeLayout){
TextViewold=(TextView)relativeLayout.findViewWithTag("first");;
select_width=old.getWidth();
select_height=old.getHeight();

RelativeLayout.LayoutParamsrl=newRelativeLayout.LayoutParams(select_width,select_height);
rl.leftMargin=old.getLeft()+((RelativeLayout)old.getParent()).getLeft();
rl.topMargin=old.getTop()+((RelativeLayout)old.getParent()).getTop();

firstLeft=old.getLeft()+((RelativeLayout)old.getParent()).getLeft();

TextViewtv=newTextView(this);
tv.setTag("move");
tv.setBackgroundResource(R.drawable.slidebar);

layout.addView(tv,rl);
relativeLayout.removeView(old);
}

privateOnClickListeneronClickListener=newOnClickListener(){
publicvoidonClick(Viewv){
if(!isAdd){
replace();
isAdd=true;
}

TextViewtop_select=(TextView)layout.findViewWithTag("move");
top_select.setGravity(Gravity.CENTER);
top_select.setText(tab1.getText());
inttabLeft;
intendLeft=0;

booleanrun=false;
switch(v.getId()){
caseR.id.tab1:
if(current!=R.id.tab1){
page.removeAllViews();
tabLeft=((RelativeLayout)tab1.getParent()).getLeft()+tab1.getLeft()+tab1.getWidth()/2;
endLeft=tabLeft-select_width/2;
current=R.id.tab1;
top_select.setText(tab1.getText());
run=true;
Viewview1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page1,null);
page.addView(view1);
}
break;
caseR.id.tab2:
if(current!=R.id.tab2){
page.removeAllViews();
tabLeft=((RelativeLayout)tab2.getParent()).getLeft()+tab2.getLeft()+tab2.getWidth()/2;
endLeft=tabLeft-select_width/2;
current=R.id.tab2;
top_select.setText(tab2.getText());
run=true;
Viewview2=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page2,null);
page.addView(view2);
}
break;
caseR.id.tab3:
if(current!=R.id.tab3){
page.removeAllViews();
tabLeft=((RelativeLayout)tab3.getParent()).getLeft()+tab3.getLeft()+tab3.getWidth()/2;
endLeft=tabLeft-select_width/2;
current=R.id.tab3;
top_select.setText(tab3.getText());
run=true;
Viewview3=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page3,null);
page.addView(view3);
}
break;
caseR.id.tab4:
if(current!=R.id.tab4){
page.removeAllViews();
tabLeft=((RelativeLayout)tab4.getParent()).getLeft()+tab3.getLeft()+tab4.getWidth()/2;
endLeft=tabLeft-select_width/2;
current=R.id.tab4;
top_select.setText(tab4.getText());
run=true;
Viewview4=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page4,null);
page.addView(view4);
}
break;
default:
break;
}

if(run){
TranslateAnimationanimation=newTranslateAnimation(startLeft,endLeft-firstLeft,0,0);
startLeft=endLeft-firstLeft;
animation.setDuration(100);
animation.setFillAfter(true);
top_select.bringToFront();
top_select.startAnimation(animation);

}

}
};
}

由于时间比较紧,我没有做注释,有时间再做注释啊。
看一下效果是不是一样啊!

   

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

 
效果还请大家自行体验并改进,由于时间仓促,代码并未做注释,希望大家能够原谅

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1596306.html

延伸阅读
微信聊天窗口的信息效果类似iphone上的短信效果,以气泡的形式展现,在Android上,实现这种效果主要用到ListView和BaseAdapter,配合布局以及相关素材,就可以自己做出这个效果,素材可以下一个微信的APK,然后把后缀名改成zip,直接解压,就可以得到微信里面的所有素材了。首先看一下我实现的效果: 以下是工程目录结构 : 接下来就是如何...
标签: Web开发
上图展示了传统 Wordpress 模板在浏览器中的载入顺序,Loading 条的出现和消失分布于头尾。 如果我们在页面的不同位置放置多个 JS ,每个 JS 用于逐步增加 Loading 条的宽度,那么这个 Loading 条无疑会更具实用价值。它在一定程度上缓解了访客等待载入的枯燥感,同时还能客观反映页面载入的进度。若再配以 jQuery 内建的动画效果,其完...
标签: Web开发
其实我本来的计划是做网页设计师的,可是没有人认为我设计的好,哥到现在还没有工作,发泄一下,不多说了。 效果图 需要用到的图片: 背景图片: 进度显示图片: 网页结构: 代码如下: div div/div divdiv/div/div /div css代码: 代码 代码如下: #center{ margin:50px auto; width:400px; } #loading{ wid...
前面一篇高仿launcher和墨迹左右拖动效果获得了很多朋友的好评,上一篇文章主要是通过自定义ViewGroup实现的,有点麻烦。今天用ViewPager这个类实现了同样的效果,这样代码更少,但是效果是一样的。ViewPager是实现左右两个屏幕平滑地切换的一个类,它是Google提供的。 使用ViewPager首先需要引入android-support-v4.jar这个jar包。具体ViewPa...
标签: Web开发
代码如下: 1.  script language="javascript"  !--  location.replace("www.goldtimes.net");  --/script  2.  meta http-equiv="refresh" content="0;url=http://www.goldtimes.net"   3.  script language="javascript"  !--  self.location="";&nbs...

经验教程

97

收藏

14
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部