自定义horinatilalview
来源:互联网 发布:c语言中函数和 编辑:程序博客网 时间:2024/06/06 02:24
private ViewPager viewPager;
private LinearLayout linearLayout;
public SimplePagerIndicator(Context context) {
super(context);
}
public SimplePagerIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SimplePagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 对外提供一个方法,设置viewPager
*/
public void setViewPager(ViewPager viewPager){
this.viewPager = viewPager;
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
this.addView(linearLayout);
//给linearLayout添加条目
for (int i =0;i<viewPager.getAdapter().getCount();i++){
View view = View.inflate(getContext(), R.layout.indicator_layout, null);
//设置点击事件
view.setOnClickListener(this);
//找到控件
TextView text_title = view.findViewById(R.id.text_title);
TextView text_indicator = view.findViewById(R.id.text_indicator);
text_title.setText(viewPager.getAdapter().getPageTitle(i));
if (i == 0){
text_indicator.setVisibility(View.VISIBLE);
//设置标题的颜色
text_title.setTextColor(Color.RED);
}else {
text_indicator.setVisibility(View.INVISIBLE);
text_title.setTextColor(Color.BLACK);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,5,10,5);
//把条目添加到linearLayout中
linearLayout.addView(view,layoutParams);
}
}
@Override
public void onClick(View view) {//view代表点击的控件
//遍历linaearLyaout里面的子条目
for (int i = 0;i<linearLayout.getChildCount();i++){
if (view == linearLayout.getChildAt(i)){
//选中viewPager那一页
viewPager.setCurrentItem(i,false);//
}
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0;i<linearLayout.getChildCount();i++){
View view = linearLayout.getChildAt(i);
TextView text_title = view.findViewById(R.id.text_title);
TextView text_indicator = view.findViewById(R.id.text_indicator);
if (i == position){
text_indicator.setVisibility(View.VISIBLE);
//设置标题的颜色
text_title.setTextColor(Color.RED);
//控制scrollView滚动到某个位置
this.smoothScrollTo((int) linearLayout.getChildAt(i).getX()-linearLayout.getChildAt(i).getWidth(),0);
}else {
text_indicator.setVisibility(View.INVISIBLE);
//设置标题的颜色
text_title.setTextColor(Color.BLACK);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实现一个frement
public class NewsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText(getArguments().getString("title",""));
return textView;
}
public static NewsFragment getInstance(String title){
NewsFragment newsFragment = new NewsFragment();
Bundle bundle = new Bundle();
bundle.putString("title",title);
newsFragment.setArguments(bundle);
return newsFragment;
}
}
-------------------------------------------------------------------------------
public class MainActivity extends AppCompatActivity {
private ProgressView progressView;
private ViewPager viewPager;
private List<String> list;
private SimplePagerIndicator simplePagerIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
simplePagerIndicator = findViewById(R.id.simple_indicator);
list = new ArrayList<>();
list.add("推荐");
list.add("推荐新闻");
list.add("推荐娱乐");
list.add("推荐歌");
list.add("推荐视频");
list.add("局势");
list.add("军事");
list.add("国际");
list.add("推荐");
list.add("推荐新闻");
list.add("推荐娱乐");
list.add("推荐歌");
list.add("推荐视频");
list.add("局势");
list.add("军事");
list.add("国际");
list.add("推荐");
list.add("推荐新闻");
list.add("推荐娱乐");
list.add("推荐歌");
list.add("推荐视频");
list.add("局势");
list.add("军事");
list.add("国际");
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
return NewsFragment.getInstance(list.get(position));
}
@Override
public int getCount() {
return list.size();
}
});
simplePagerIndicator.setViewPager(viewPager);
//添加监听事件
viewPager.addOnPageChangeListener(simplePagerIndicator);
- 自定义horinatilalview
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义TexBox,自定义ComboBox
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义一个自定义对话框
- 自定义控件-自定义吐司
- 自定义checkbox,自定义button
- 自定义控件--自定义对话框
- 自定义控件----自定义属性
- 自定义控件:自定义Toast
- 自定义View自定义属性
- NKOJ 3884 (NOI 2005) 维修数列(Splay)
- 批量处理服务
- java将字符串写入到文本里
- 【JavaScript】原生JS实现AJAX、JSONP
- WEB 前端菜鸟,感觉很迷茫,该怎么做?
- 自定义horinatilalview
- CDOJ 1292 卿学姐种花 暴力 分块 线段树
- js根据日期计算星期几
- linux之cut命令的用法
- Java解析yaml
- 杨辉三角
- 状压dp Mondriaan's Dream
- hdu-5444(简单建树和遍历)
- 【课程笔记】遥感技术应用实验_几何校正