第三方框架实现横向滚动条

来源:互联网 发布:银行内控优化心得体会 编辑:程序博客网 时间:2024/05/02 01:29

TabPageIndicator自定义控件

  • 引入第三方依赖库library

在布局中要引用自定义控件的全路径

<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" >    <com.viewpagerindicator.TabPageIndicator        android:id="@+id/indicator"        android:background="@drawable/base_action_bar_bg"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="match_parent"        android:layout_height="0dp"         android:layout_weight="1"/></LinearLayout>

在style中定义样式(当滑动viewpager时导航项背景变红色,同时下标也是红色)

<style name="AppBaseTheme" parent="android:Theme.Light">        <!--            Theme customizations available in newer API levels can go in            res/values-vXX/styles.xml, while customizations related to            backward-compatibility can go here.        -->    </style>    <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style>     <style name="StyledIndicators" parent="@android:style/Theme.Light">        <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item>    </style>    <style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">        <item name="android:background">@drawable/tab_indicator</item>        <item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>        <item name="android:textSize">14sp</item>        <item name="android:dividerPadding">8dp</item>        <item name="android:showDividers">middle</item>        <item name="android:paddingLeft">10dp</item>        <item name="android:paddingRight">10dp</item>        <item name="android:fadingEdge">horizontal</item>        <item name="android:fadingEdgeLength">8dp</item>    </style>    <style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">        <item name="android:typeface">monospace</item>        <item name="android:textColor">@drawable/selector_tabtext</item>    </style>

创建drawable文件夹
selector_tabtext.xml 选中时文字颜色

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_selected="true" android:color="#EE2C2C" />    <item android:state_pressed="true" android:color="#EE2C2C" />    <item android:state_focused="true" android:color="#EE2C2C" />    <item android:color="@android:color/black"/></selector> 

tab_indicator.xml

//此处的corlor属性均在library中<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />    <item android:state_selected="false" android:state_pressed="true" android:drawable="@android:color/transparent" />         <item android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/base_tabpager_indicator_selected" />    <item android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/base_tabpager_indicator_selected" /></selector>

MainActivity

private static final String[] TITLE = new String[] { "资讯", "热点", "博客", "推荐"};//ViewPager的adapterTabPageIndicatorAdapter adapter =new TabPageIndicatorAdapter(getSupportFragmentManager()); ViewPager pager = (ViewPager)findViewById(R.id.viewpager); pager.setAdapter(adapter); //实例化TabPageIndicator然后设置ViewPager与之关联        TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);        indicator.setViewPager(pager);            indicator.setOnPageChangeListener(new OnPageChangeListener() {            @Override            public void onPageSelected(int arg0) {                Toast.makeText(getApplicationContext(), TITLE[arg0], Toast.LENGTH_SHORT).show();            }            @Override            public void onPageScrolled(int arg0, float arg1, int arg2) {            }            @Override            public void onPageScrollStateChanged(int arg0) {            }        });//FragmentPagerAdapter class TabPageIndicatorAdapter extends FragmentPagerAdapter {            public TabPageIndicatorAdapter(FragmentManager fm) {                super(fm);            }            @Override            public Fragment getItem(int position) {                //新建一个Fragment来展示ViewPager item的内容,并传递参数                Fragment fragment = new ItemFragment();                  Bundle args = new Bundle();                  args.putString("arg", URL[position]);                fragment.setArguments(args);                  return fragment;            }            @Override            public CharSequence getPageTitle(int position) {                return TITLE[position % TITLE.length];            }            @Override            public int getCount() {                return TITLE.length;            }        }

Fragment 选v4包下的

public class ItemFragment extends Fragment {    private String url;    private ListView lv;    private MainActivity context;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View contextView = inflater.inflate(R.layout.fragment_item, container, false);        lv = (ListView) contextView.findViewById(R.id.lv);        //获取Activity传递过来的参数        Bundle mBundle = getArguments();        url = mBundle.getString("arg");        Log.i("Main",url);        return contextView;    }    @Override    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        context = (MainActivity) getActivity();        HttpUtils httpUtils=new HttpUtils();        httpUtils.send(HttpMethod.POST, url,new RequestCallBack<String>() {            @Override            public void onFailure(HttpException arg0, String arg1) {                // TODO Auto-generated method stub            }            @Override            public void onSuccess(ResponseInfo<String> arg0) {                String str = arg0.result;                Log.i("Main", str);                //此处是Xstream解析                XStream xStream=new XStream(new DomDriver());                xStream.processAnnotations(User.class);                User user = (User) xStream.fromXML(str);                List<News> list = user.newslist.news;                lv.setAdapter(new MyBaseAdapter(context,list));            }        });    }}
1 0
原创粉丝点击