欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android HorizontalScrollView滑动与ViewPager切换案例详解

时间:2022-08-12 10:02:49|栏目:Android代码|点击:

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实现登录注册界面框架

栏    目:Android代码

下一篇:Android自定义输入法软键盘

本文标题:Android HorizontalScrollView滑动与ViewPager切换案例详解

本文地址:http://www.codeinn.net/misctech/210576.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有