xml布局
<HorizontalScrollView
android:id="@+id/hs"
android:layout_width="400dp"
android:scrollbars="none"
android:layout_height="40dp">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/hs"></android.support.v4.view.ViewPager>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/viewpager"
android:layout_toEndOf="@+id/hs"
android:layout_toRightOf="@+id/hs"
android:text="频道管理" />
pindaoguanli
private View view;
private HorizontalScrollView hs;
private LinearLayout ll;
private ViewPager vp;
private Button button;
private List<TextView> list;
private List<ChannelBean> channelBeans;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment01, container, false);
initViews();
initDatas();
return view;
}
private void initViews() {
hs = view.findViewById(R.id.hs);
ll = view.findViewById(R.id.linearlayout);
vp = view.findViewById(R.id.viewpager);
button = view.findViewById(R.id.button);
}
private void initDatas() {
list = new ArrayList<>();
channelBeans = new ArrayList<>();
channelBeans.add(new ChannelBean("头条", true));
channelBeans.add(new ChannelBean("娱乐", true));
channelBeans.add(new ChannelBean("科技", true));
channelBeans.add(new ChannelBean("信息", true));
channelBeans.add(new ChannelBean("八卦", true));
channelBeans.add(new ChannelBean("北京", false));
channelBeans.add(new ChannelBean("上海", false));
channelBeans.add(new ChannelBean("天津", false));
channelBeans.add(new ChannelBean("重庆", false));
channelBeans.add(new ChannelBean("大大燕网", false));
for (int i = 0; i < channelBeans.size(); i++) {
TextView textView = new TextView(getActivity());
textView.setText(channelBeans.get(i).getName() + "");
textView.setTextSize(20);
textView.setId(i + 1000);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
vp.setCurrentItem(id - 1000);
}
});
if (i == 0) {
textView.setTextColor(Color.RED);
} else {
textView.setTextColor(Color.BLACK);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(50, 10, 50, 10);
ll.addView(textView, layoutParams);
list.add(textView);
}
vp.setAdapter(new MyAdapter(getActivity().getSupportFragmentManager()));
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
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() + 10;
hs.scrollTo(width * position, 0);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ChannelActivity.startChannelActivity((AppCompatActivity) getActivity(),channelBeans);
}
});
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
return Fragment001.getinstance(list.get(position).getText().toString());
}
@Override
public int getCount() {
return list.size();
}
}
}