布局 <? xml version = "1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_red_light" android:gravity="center" android:padding="10dp" android:text="今日头条" android:textColor="#FFFFFF" android:textSize="16sp"/> <HorizontalScrollView android:id="@+id/hsv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none"> <LinearLayout android:id="@+id/ll_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#e2dada" android:orientation="horizontal"></LinearLayout> </HorizontalScrollView> <android.support.v4.view.ViewPager android:id="@+id/vp_content" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </LinearLayout> -------------------------------------------------------------------- <? xml version = "1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt_content" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="haha "/> </LinearLayout> MainActivity ------------------------------------------------------------ public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView TouTiao; private LinearLayout line; private HorizontalScrollView horiz; private ViewPager vp; private List<String> list; // private List<Fragment> flist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); // 1. 向LinearLayout里面添加数据 // 2. 向ViewPager里面添加Fragment addTitleAndFragment(); vpData();//ViewPager的适配器和ViewPager监听事件 } private void vpData() { //给ViewPager设置适配器 vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return ContentFragment.newInstance(list.get(position)); // return flist.get(position); } @Override public int getCount() { return list.size(); } }); //viewPager的监听事件 vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //改变字体颜色 changeTextColor(position); //计算控件要滑动的距离 int marges = 20 * position; // 定义一个textview宽度的综合 int widthTotal = 0; // 遍历textview的宽度 for (int j = 0; j < position; j++) { TextView tex = (TextView) line.getChildAt(position); widthTotal += tex.getWidth(); } //计算滑动的位置 horiz.scrollTo(marges + widthTotal, 0); } @Override public void onPageScrollStateChanged(int state) { } }); } private void changeTextColor(int position) { //改变字体颜色 for (int i = 0; i < list.size(); i++) { //1.首先要拿到Textview TextView textview = (TextView) line.getChildAt(i); //2.如果选中 字体改变颜色 红色 否则默认黑色 if (position == i) { textview.setTextColor(Color.RED); } else { textview.setTextColor(Color.BLACK); } } } /** * 向LinearLayout里面添加数据 * 向ViewPager添加Fragment */ private void addTitleAndFragment() { for (int i = 0; i < list.size(); i++) { //代码添加TextView TextView tv = new TextView(this); tv.setText(list.get(i));//添加数据内容 //设置字体大小 和字体颜色 设置字体大小是直接添加数字即可 默认为sp tv.setTextColor(Color.BLACK); tv.setTextSize(18); //----------设置默认字体颜色 第一个是默认选中,红色,其他的是黑色,不选中 if (i == 0) { tv.setTextColor(Color.RED); } else { tv.setTextColor(Color.BLACK); } tv.setId(i + 2000);//给设置个id 并且设置个点击事件 tv.setOnClickListener(MainActivity.this); //设置textview要添加进的ViewGroup的宽高 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT ); params.setMargins(10, 5, 10, 5);//设置边距 // 给textview设置一个相对父控件的参数 tv.setLayoutParams(params); line.addView(tv); //添加Fragment 先去fragment里面做传值 // Fragment fra = ContentFragment.newInstance(list.get(i)); // flist.add(fra); } } /** * 初始化数据 */ private void initData() { list = new ArrayList<>(); // flist = new ArrayList<>(); list.add("推荐"); list.add("热点"); list.add("科技"); list.add("视频"); list.add("数码"); list.add("社会"); list.add("汽车"); list.add("娱乐"); list.add("电影"); list.add("问答"); list.add("北京"); list.add("图片"); list.add("体育"); list.add("财经"); list.add("军事"); list.add("国际"); } private void initView() { TouTiao = (TextView) findViewById(R.id.TouTiao); line = (LinearLayout) findViewById(R.id.line); horiz = (HorizontalScrollView) findViewById(R.id.horiz); vp = (ViewPager) findViewById(R.id.vp); } //点击事件 @Override public void onClick(View v) { int id = v.getId(); int position = id - 2000; vp.setCurrentItem(position); //点击变色 changeTextColor(position); } } ContentFragment------------------------------------------------ public class ContentFragment extends Fragment { private View view; private TextView frag_title; private static final String KEY = "ASD"; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_title, container, false); initView(view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } /** * Fragment静态传值 */ public static ContentFragment newInstance(String string) { ContentFragment frag = new ContentFragment(); Bundle bundle = new Bundle(); bundle.putString(KEY, string); frag.setArguments(bundle); return frag; } private void initView(View view) { frag_title = (TextView) view.findViewById(R.id.frag_title); String str = (String) getArguments().get(KEY); frag_title.setText(str); } }
自定义title标题联动
最新推荐文章于 2022-01-11 22:41:42 发布