package com.bwie.test;
import java.util.ArrayList;
import com.bwie.adapter.MyAdapter; import com.bwie.fragment.F1; import com.bwie.utils.Myutil;
import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.view.Menu; import android.view.View; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private LinearLayout line_sc; private HorizontalScrollView sc; private String[] column = new String[] { "舍内要闻", "本社介绍", "履行职能", "自身建设", "社员风采", "自我介绍", "校内趣闻", "爆料" }; private ArrayList<Fragment> list; private ViewPager vp; private int widthPixels; private ArrayList<TextView> list_view;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
/** * 获取窗口的 宽度 */ widthPixels=getWindowManager().getDefaultDisplay().getWidth();
// 初始化数据 init();
// 获取栏目的数据 getColumn();
// 创建fragment数据 setFragment();
// 为vp设置数据 FragmentManager fm = getSupportFragmentManager(); vp.setAdapter(new MyAdapter(fm, list));
setTitle_1(vp.getCurrentItem());
// 为vp设置页面滑动监听 vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override public void onPageSelected(int arg0) {
//设置游标 setTitle_1(arg0);
//设置滑动 setAutoScroll(arg0);
}
@Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub
}
@Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub
} });
}
protected void setAutoScroll(int p) { // TODO Auto-generated method stub
//获取当前要展示的游标 TextView textView = list_view.get(p); //获取其宽度 int w = textView.getMeasuredWidth(); int left = textView.getLeft(); int address=left-widthPixels/2;
sc.smoothScrollTo(address, 0);
}
private void setFragment() {
// 创建集合存放fragment对象 list = new ArrayList<Fragment>(); for (int i = 0; i < column.length; i++) { F1 f1 = new F1();
// 传值 Bundle b = new Bundle(); b.putInt("column", i); f1.setArguments(b);
// 将创建的fragment添加到集合中 list.add(f1); } } private void setTitle_1(int arg0) { //设置当前显示的textview控件 TextView textView = list_view.get(arg0); //将当前的textview显示 textView.setVisibility(View.VISIBLE);
for(int i=0;i<list_view.size();i++) { TextView tv = list_view.get(i);
if(tv!=textView) { tv.setVisibility(View.GONE); } } }
private void getColumn() { // TODO Auto-generated method stub
list_view = new ArrayList<TextView>(); for (int i = 0; i < column.length; i++) {
// 创建线性布局来整体作为水平滚动的子布局 LinearLayout sc = new LinearLayout(MainActivity.this); // 设置线性布局的排列方式(垂直) sc.setOrientation(1); sc.setPadding(30, 0, 0, 0);
// 1.首先向线性布局中添加textview // 创建textview TextView tv = new TextView(MainActivity.this); // 为tv设置数据 tv.setText(column[i]); LinearLayout.LayoutParams params = new LayoutParams( widthPixels / 5, LayoutParams.WRAP_CONTENT); sc.addView(tv, params);
// 2.向线性布局中添加下面的横线 TextView tv_line = new TextView(MainActivity.this); LinearLayout.LayoutParams params_2 = new LayoutParams( widthPixels / 5, 10); tv_line.setBackgroundColor(Color.GRAY); tv_line.setVisibility(View.INVISIBLE); list_view.add(tv_line); sc.addView(tv_line, params_2);
// 将数据添加到 线性布局中 line_sc.addView(sc); }
}
private void init() { line_sc = (LinearLayout) findViewById(R.id.line_sc); sc = (HorizontalScrollView) findViewById(R.id.sc); vp = (ViewPager) findViewById(R.id.vp);
}
}
|