layout布局
<RelativeLayout 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" tools:context=".MainActivity" > <HorizontalScrollView android:id="@+id/sc" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none" > <LinearLayout android:id="@+id/line_sc" android:layout_width="match_parent" android:layout_height="wrap_content" > </LinearLayout> </HorizontalScrollView> <LinearLayout android:id="@+id/view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/sc" /> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/view" > </android.support.v4.view.ViewPager> </RelativeLayout>
MainActivity.java
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); /** * 获取窗口的 宽度 */ // 创建DisplayMetrics类对象 DisplayMetrics dm = new DisplayMetrics(); // 将当前窗口的一些信息放在DisplayMetrics类中 getWindowManager().getDefaultDisplay().getMetrics(dm); widthPixels = dm.widthPixels; // 初始化数据 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); } }
horizontalScrollView viewpager 字体颜色 滑动距离
FragmentManager fm = getSupportFragmentManager(); viewPager.setAdapter(new MyFragmentAdapter(fm, getData())); viewPager.setOnPageChangeListener(new OnPageChangeListener() { public void onPageSelected(int arg0) { // TODO Auto-generated method stub setTag(arg0); } public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); public ArrayList<Fragment> getData() { ArrayList<Fragment> list = new ArrayList<Fragment>(); for (int i = 0; i < str.length; i++) { list.add(new F1(str[i])); } return list; } private void setData() { // TODO Auto-generated method stub List<String> list = new ArrayList<String>(); for (int i = 0; i < str.length; i++) { View view = View.inflate(MainActivity.this, android.R.layout.simple_list_item_1, null); TextView textView = (TextView) view .findViewById(android.R.id.text1); list.add(str[i]); textView.setText(list.get(i)); scroll_ly.addView(view); textView.setTag(i); textView.setOnClickListener(this); } TextView childAt = (TextView) scroll_ly.getChildAt(0); // childAt.setTextColor(Color.RED); childAt.setBackgroundColor(Color.RED); childAt.setTextColor(Color.WHITE); } // 设置scrollview中textview的变化 public void setTag(int position) { for (int i = 0; i < scroll_ly.getChildCount(); i++) { /** * textView滑动的位置 */ // 获取textview的viewpager滑动的当前子类 TextView textView = (TextView) scroll_ly.getChildAt(position); int k = textView.getMeasuredWidth(); int l = textView.getLeft(); int s = k / 2 + l - wid / 2; // 设置scrollView x轴的距离 scrollView.smoothScrollTo(s, 0); /** * textview滑动变色 */ TextView textView2 = (TextView) scroll_ly.getChildAt(i); if (position == i) { textView2.setBackgroundColor(Color.RED); textView2.setTextColor(Color.WHITE); } else { textView2.setTextColor(Color.BLACK); textView2.setBackgroundColor(Color.WHITE); } } } public void onClick(View v) { // TODO Auto-generated method stub if (v.getTag() != null) { int i = (Integer) v.getTag(); View childAt = scroll_ly.getChildAt(i); if (v == childAt) { viewPager.setCurrentItem(i); } } }
到此这篇关于Android HorizontalScrollView滑动与ViewPager切换案例详解的文章就介绍到这了,更多相关Android HorizontalScrollView滑动与ViewPager切换内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/221062/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)