自定义水平滚动栏(关联ViewPager)

来源:互联网 发布:科技成就梦想,网络 编辑:程序博客网 时间:2024/06/06 01:34
<span style="font-family:Comic Sans MS;font-size:18px;">public class HorizontalScrollViewDiy extends LinearLayout{    private LinearLayout linearLayoutchild;    private ViewPager mviewPager;    private TextView mproject;    private List<String> mlist=new ArrayList<String>();    private List<TextView> tlist=new ArrayList<TextView>();    private Map<Integer,TextView> map=new HashMap<Integer, TextView>();    public HorizontalScrollViewDiy(Context context) {        super(context);        initview();    }    public HorizontalScrollViewDiy(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        initview();    }    public HorizontalScrollViewDiy(Context context, AttributeSet attrs) {        super(context, attrs);         initview();    }    private void initview(){      LayoutInflater.from(getContext()).inflate(R.layout.horizontalscrollviewdiy,this);      linearLayoutchild=(LinearLayout)findViewById(R.id.linearlayoutchild);    }    public void setProject(List<String> list,int textsize,int width,final int textcolor,final int textChangeColor,final int textChangeBackColor){             mlist.clear();             mlist=list;        for(int i=0;i<mlist.size();i++){            mproject=new TextView(getContext());            mproject.setText(mlist.get(i));            mproject.setTextColor(getResources().getColor(textcolor));            mproject.setTextSize(textsize);            mproject.setGravity(Gravity.CENTER);            mproject.setLayoutParams(new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT));            mproject.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View view) {                    tlist.add((TextView) view);                    for(int j=0;j<map.size();j++){                        if((TextView)view==map.get(j)){                            mviewPager.setCurrentItem(j);                        }                    }                    if(tlist.get(0)!=tlist.get(1)){                    tlist.get(1).setTextColor(getResources().getColor(textChangeColor));                    tlist.get(1).setBackgroundColor(getResources().getColor(textChangeBackColor));                    tlist.get(0).setTextColor(getResources().getColor(textcolor));                    tlist.get(0).setBackgroundColor(getDrawingCacheBackgroundColor());                    }                    tlist.remove(0);                }            });            map.put(i, mproject);            linearLayoutchild.addView(mproject);        }            mviewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {                @Override                public void onPageScrolled(int i, float v, int i2) {                }                @Override                public void onPageSelected(int i) {                   tlist.add(map.get(i));                    map.get(i).setTextColor(getResources().getColor(textChangeColor));                    map.get(i).setBackgroundColor(getResources().getColor(textChangeBackColor));                    tlist.get(0).setTextColor(getResources().getColor(textcolor));                    tlist.get(0).setBackgroundColor(getDrawingCacheBackgroundColor());                    tlist.remove(0);                }                @Override                public void onPageScrollStateChanged(int i) {                }            });        map.get(0).setTextColor(getResources().getColor(textChangeColor));        map.get(0).setBackgroundColor(getResources().getColor(textChangeBackColor));        tlist.add(map.get(0));    }    public void setViewPager(ViewPager viewPager){        mviewPager=viewPager;    }}</span>
<span style="font-family:Comic Sans MS;font-size:18px;"></span><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>    <LinearLayout            xmlns:android="http://schemas.android.com/apk/res/android"                   android:layout_width="match_parent"                   android:layout_height="match_parent"                   android:id="@+id/linearlayoutparent"            >    <HorizontalScrollView android:layout_width="match_parent"                          android:layout_height="match_parent"                          android:id="@+id/horizontalscrollviewdiy"                          android:scrollbars="none"            >        <LinearLayout                     android:layout_width="match_parent"                      android:layout_height="match_parent"                      android:id="@+id/linearlayoutchild"                      android:gravity="center_vertical"                >        </LinearLayout>    </HorizontalScrollView>    </LinearLayout>



0 0