横向滑动菜单,HorizontalScrollView,fragment,Viewpager结合使用

这里写图片描述

package com.wzq.scrollviewindicater;

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.HorizontalScrollView; 
import android.widget.LinearLayout; 
import android.widget.TextView;

import com.wzq.scrollviewindicater.fragment.MFragment;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity implements OnClickListener {

private LinearLayout ll_head;
private ViewPager viewPager;
private String[] titles;
private ArrayList<TextView> textViewList;
private HorizontalScrollView horizontalScrollView;
private ArrayList<TextView> titlesView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ll_head = (LinearLayout) findViewById(R.id.ll_head);
    horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    titles = new String[]{"头条", "娱乐", "科技", "信息", "八卦", "北京", "上海", "天津",
            "重庆", "大大燕网"};
    //动态添加顶部textview
    initTitle();
    viewPager.setAdapter(new MPageAdapter(getSupportFragmentManager()));
    initListener();

}

/**
 * 设置滑动viewpager监听
 */
private void initListener() {
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            //滑动的时候, 改变文字颜色,
            for (int i = 0; i < titles.length; i++) {
                if (i == position) {//1
                    titlesView.get(i).setTextColor(Color.RED);
                }else {
                    titlesView.get(i).setTextColor(Color.GRAY);
                }
            }

            //处理滑动顶部标题
            int width = titlesView.get(0).getWidth();//textview的宽度,

            horizontalScrollView.scrollTo((width+20)*position,0);

        }
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
        @Override
        public void onPageScrollStateChanged(int state) { }
    });

}

/**
 * 动态添加标题;
 */
private void initTitle() {
    titlesView = new ArrayList<>();
    // 我
    for (int i = 0; i < titles.length; i++) {
        TextView textView = new TextView(MainActivity.this);
        textView.setText(titles[i]);
        textView.setTextSize(26);
        if (i == 0) {
            textView.setTextColor(Color.RED);//处理第一次进入第一个标题为红色
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置外边距,左上右下;
        params.setMargins(10, 10, 10, 10);
        textView.setId(i);
        textView.setOnClickListener(this);//点击文字的时候,会进入onClick方法;
        titlesView.add(textView);
        ll_head.addView(textView, params);
    }

}

/**
 * 处理顶部文字的点击事件;
 * @param v
 */
@Override
public void onClick(View v) {
    int id = v.getId();//id就是标题的下标;
    //ID是哪个Textview,我将来让viewpager滑动到哪个页面;
    viewPager.setCurrentItem(id);

}

private class MPageAdapter extends FragmentPagerAdapter {


    public MPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        //动态返回Fragment;
        return MFragment.getInstance(titles[position]);
    }

    @Override
    public int getCount() {
        return titles.length;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值