scrollviewindicater标题滑动

主界面:

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private HorizontalScrollView scrollView;
    private LinearLayout layout;
    private ViewPager viewPager;
    private String[] titles;
    private ArrayList<TextView> TextViewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollView = (HorizontalScrollView) findViewById(R.id.scroll);
        layout = (LinearLayout) findViewById(R.id.linear);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        titles = new String[]{"头条", "娱乐", "科技", "信息", "八卦", "北京", "上海", "天津",
                "重庆", "大大燕网"};
        initData();

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return BlankFragment.getintances(titles[position]);
            }

            @Override
            public int getCount() {
                return titles.length;
            }
        });

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < TextViewList.size(); i++) {
                    if (position == i) {
                        TextViewList.get(i).setTextColor(Color.RED);
                    } else {
                        TextViewList.get(i).setTextColor(Color.BLACK);
                    }
                }
                TextView textView = TextViewList.get(position);
                int width = textView.getWidth();
                scrollView.scrollTo((width + 20) * position, 0);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        TextViewList = new ArrayList<TextView>();
        for (int i = 0; i < titles.length; i++) {
            TextView textView = new TextView(MainActivity.this);
            textView.setText(titles[i]);
            textView.setTextSize(24);
            textView.setOnClickListener(this);
            textView.setId(i + 10000);
            if (i == 0) {
                textView.setTextColor(Color.RED);
            } else {
                textView.setTextColor(Color.BLACK);
            }

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(20, 10, 20, 10);
            layout.addView(textView, layoutParams);
            TextViewList.add(textView);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        viewPager.setCurrentItem(id-10000);
    }
}

Fragment:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 作者:李治的爷爷
 * 时间:2017/10/12
 */

public class BlankFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        Bundle bundle = getArguments();
        String name = bundle.getString("name");
        textView.setText(name);
        return textView;
    }

    public static BlankFragment getintances(String name){
        BlankFragment blankFragment = new BlankFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name",name);
        blankFragment.setArguments(bundle);
        return blankFragment;
    }
}

布局:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.bwie.scrollviewindicater.MainActivity">

    <HorizontalScrollView
        android:id="@+id/scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/linear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

        </LinearLayout>

    </HorizontalScrollView>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


    </android.support.v4.view.ViewPager>

</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值