您的位置:首页 > 其它

viewpager滑动fragment点击textview切换页面线条滑动效果

2016-10-18 21:23 447 查看
public class WeActivity extends FragmentActivity implements OnClickListener {
private List<Fragment> flist = new ArrayList<Fragment>();
private ViewPager tabsvp;
private RedCursorView cursor;
private TabAdapter adapter;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private LinearLayout rel;
private LinearLayout rel2;
private TextView shouye;
private TextView yong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_we);
cursor=(com.example.view.RedCursorView)findViewById(R.id.cursor);
tabsvp=(ViewPager) findViewById(R.id.tabsvp);
tv1 = (TextView) findViewById(R.id.text1);
tv2 = (TextView) findViewById(R.id.text2);
tv3 = (TextView) findViewById(R.id.text3);
rel=(LinearLayout) findViewById(R.id.rel);
rel2=(LinearLayout) findViewById(R.id.rel2);
shouye=(TextView) findViewById(R.id.shouye);
yong=(TextView) findViewById(R.id.yong);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
rel.setOnClickListener(this);
rel2.setOnClickListener(this);
shouye.setOnClickListener(this);
yong.setOnClickListener(this);
View v = findViewById(R.id.lin);//找到你要设透明背景的layout 的id
v.getBackground().setAlpha(200);//0~255透明度值 ,0为完全
tabsvp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
tv1.setTextColor(Color.BLACK);
tv2.setTextColor(Color.BLACK);
tv3.setTextColor(Color.BLACK);
switch (arg0) {
case 0:
tv1.setTextColor(Color.RED);
break;
case 1:
tv2.setTextColor(Color.RED);
break;
case 2:
tv3.setTextColor(Color.RED);
break;
default:
break;
}
}

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
cursor.setXY(position, positionOffset);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}
});
adapter=new TabAdapter(getSupportFragmentManager());
tabsvp.setAdapter(adapter);
}
public class TabAdapter extends FragmentPagerAdapter{
public TabAdapter(FragmentManager fm) {
super(fm);
flist.add(new Tabfragment());
flist.add(new Renfragment());
flist.add(new Newsfragment());
}

@Override
public Fragment getItem(int arg0) {
return flist.get(arg0);
}

@Override
public int getCount() {
return flist.size();
}

}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.text1:
tabsvp.setCurrentItem(0);
break;
case R.id.text2:
tabsvp.setCurrentItem(1);
break;
case R.id.text3:
tabsvp.setCurrentItem(2);
break;
case R.id.shouye:
rel2.setVisibility(View.GONE);
rel.setVisibility(View.VISIBLE);
break;
case R.id.yong:
rel.setVisibility(View.GONE);
rel2.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
}

布局文件
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".WeActivity" >
<LinearLayout
android:id="@+id/rel"
android:layout_weight="5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#ffffff" >

<ImageView
android:id="@+id/sou"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_alignParentLeft="true"
android:layout_margin="12dp"
android:src="@drawable/li_home_search_left_bg" />

<ImageView
android:id="@+id/jiabei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="12dp"
android:src="@drawable/li_icon_personal_level" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:orientation="horizontal" >

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="关注"
android:textSize="16sp" />

<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="40sp"
android:layout_marginRight="40sp"
android:text="热门"
android:textSize="16sp" />

<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="最新"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>

<com.example.view.RedCursorView
android:id="@+id/cursor"
android:layout_width="214dp"
android:layout_height="6dp"
android:layout_gravity="center"
android:layout_marginLeft="30sp"
android:layout_marginRight="30sp" >
</com.example.view.RedCursorView>
<android.support.v4.view.ViewPager
android:id="@+id/tabsvp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/rel2"
android:layout_weight="5"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/libaozhi"
/>
</LinearLayout>

<LinearLayout
android:id="@+id/lin"
android:layout_width="fill_parent"
android:layout_height="52dp"
android:layout_gravity="center_horizontal"
android:background="#e0000000"
android:orientation="horizontal" >

<TextView
android:id="@+id/shouye"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_weight="2"
android:drawableTop="@drawable/shouye"
android:gravity="center"
android:text="首页"
android:textColor="#ff0000" />

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/li_maintab_party_pic_selected" />

<TextView
android:id="@+id/yong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_weight="2"
android:drawableTop="@drawable/yong"
android:gravity="center"
android:text="我的"
android:textColor="#ff0000" />
</LinearLayout>
</LinearLayout>

自定义view红线
public class RedCursorView extends View {
private int cursorColor = Color.RED; // 线的颜色
private int counts = 3; // 被分成了几块
private float posX = 0f; // 当前X坐标的位置
private Paint paint;

public RedCursorView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

public RedCursorView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public RedCursorView(Context context) {
super(context);
init();
}

private void init() {
// 初始化画笔
paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(cursorColor);
}

/**
* 设置ViewPager有几块
* @param counts
*/
public void setCounts(int counts) {
this.counts = counts;
}

/**
* 设置坐标
* @param pos 当前的item
* @param rate 变化率
*/
public void setXY(int pos, float rate) {
int single = getMeasuredWidth() / counts;
posX = pos * single + single * rate;
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStrokeWidth(getMeasuredHeight());
int width = getMeasuredWidth() / counts - 2;
canvas.drawLine(posX, 0, posX + width, 0, paint);
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: