横向滑动菜单:horizontalscrollview+viewpager+Fragment

蒲魁
2023-12-01
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private HorizontalScrollView scrollView;
    private LinearLayout linearLayout;
    private ViewPager viewPager;
    private String[] titles;
    private ArrayList<TextView> list;
    private LinearLayout.LayoutParams params;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        scrollView = (HorizontalScrollView) findViewById(R.id.Scrollview);
        linearLayout = (LinearLayout) findViewById(R.id.Linearlayout);
        viewPager = (ViewPager) findViewById(R.id.Viewpager);
        //创建数据源
        titles = new String[]{"头条", "社会", "国内", "国际", "娱乐", "体育", "军事", "科技", "财经", "时尚"};
        //创建集合
        list = new ArrayList<>();
        //设置数据
        initdata();
        //创建适配器
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragment.getinfo(list.get(position).getText().toString());
            }
            @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 < list.size(); i++) {
                    if (position == i) {
                        list.get(i).setTextColor(Color.RED);
                    } else {
                        list.get(i).setTextColor(Color.BLACK);
                    }
                }
                TextView textView = list.get(position);
                int width = textView.getWidth();//获取宽度
                //计算该滑到哪里
                scrollView.scrollTo((width + 20) * position, 0);//viewpager滑动时与滚动控件想配合一起滑动
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
    public void onClick(View view) {
        int id = view.getId();
        viewPager.setCurrentItem(id - 10000);
    }
    private void initdata() {
        //添加数据,设置宽高,设置字体颜色
        for (int i = 0; i < titles.length; i++) {
            //自己定义一个textview
            TextView view = new TextView(this);
            //把数据放入textview里
            view.setText(titles[i]);
            //设置数据最大小
            view.setTextSize(20);
            //通过判断刚进入为黑色,否则为红色
            if (i == 0) {
                view.setTextColor(Color.RED);
            } else {
                view.setTextColor(Color.BLACK);
            }
            //给textview设置点击事件
            view.setOnClickListener(this);
            view.setId(i + 10000);
            //设置textview控件的宽高
            params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            //通过宽高设置neibianju
            params.setMargins(20, 10, 20, 10);
            //将textview控件添加到滚动控件里面的LinearLayout布局
            linearLayout.addView(view, params);
            //把数据添加到集合
            list.add(view);
        }
    }
}
//class类
public class fragment extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        String sss = arguments.getString("sss");
        TextView textView = new TextView(getActivity());
        textView.setText(sss);
        return textView;
    }
    public static Fragment getinfo(String titles){
        fragment fragment0 = new fragment();
        Bundle bundle = new Bundle();
        bundle.putString("sss",titles);
        fragment0.setArguments(bundle);
        return fragment0;
    }
}
//主布局
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="chendemin.bwei.com.viewpagerindacaterhorizontalscrollview.MainActivity"
    android:orientation="vertical">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Scrollview"
        android:layout_marginTop="30dip"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:id="@+id/Linearlayout"
            >
        </LinearLayout>
    </HorizontalScrollView>
    <android.support.v4.view.ViewPager
        android:id="@+id/Viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
</LinearLayout>
 类似资料: