横向滑动+viewpage的完美结合

来源:互联网 发布:kali linux ddos 编辑:程序博客网 时间:2024/05/16 11:45

1.布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    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="com.example.view.MainActivity">    <HorizontalScrollView        android:id="@+id/hs"        android:layout_width="match_parent"        android:layout_height="wrap_content"        >        <LinearLayout  android:id="@+id/lt"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            >        </LinearLayout>    </HorizontalScrollView>   <android.support.v4.view.ViewPager       android:id="@+id/vp"       android:layout_width="match_parent"       android:layout_height="match_parent"       android:layout_below="@id/hs"       ></android.support.v4.view.ViewPager></RelativeLayout>
2.Activty

package com.example.view;import android.graphics.Color;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;import android.widget.TextView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {    private ViewPager vp;    private HorizontalScrollView hs;    private LinearLayout lt;    private String[] title;    private ArrayList<TextView> li;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        hs = (HorizontalScrollView) findViewById(R.id.hs);        vp = (ViewPager) findViewById(R.id.vp);        lt = (LinearLayout) findViewById(R.id.lt);        //创建数组 ,存放那个标题        title=new String[]{          "头条","上海",                "头条","上海",                "头条","上海",                "头条","上海",                "头条","上海"        };        //创建一个 集合 存放textview        li = new ArrayList<TextView>();        //创建 textview        for (int i=0;i<title.length;i++)        {//创建Textview            TextView textView = new TextView(MainActivity.this);            textView.setText(title[i]);            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(20,10,20,10);            //添加到布局中            lt.addView(textView,layoutParams);            //添加到集合            li.add(textView);        }        //为viewpage设置适配器        vp.setAdapter(new Mydapter(getSupportFragmentManager()));        //为viewpage设置监听        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {                for (int i=0;i<li.size();i++)                {                    if (position==i)                    {                        li.get(i).setTextColor(Color.RED);                    }else {                        li.get(i).setTextColor(Color.BLACK);                    }                }                //获取当前的textview                TextView textView = li.get(position);                //width是每次滑动的距离                int i = textView.getWidth() + 10;                //让scrollview滑动 滑动距离是textview的间距                hs.scrollTo(i*position,0);            }            @Override            public void onPageSelected(int position) {            }            @Override            public void onPageScrollStateChanged(int state) {            }        });    }    class Mydapter extends FragmentPagerAdapter{        public Mydapter(FragmentManager fm) {            super(fm);        }        @Override        public Fragment getItem(int position) {            return Fragment1.getinstance(li.get(position).getText().toString());        }        @Override        public int getCount() {            return li.size();        }    }}
3.新建一个类

package com.example.view;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;/** * author:Created by WangZhiQiang on 2017/9/13. */public class Fragment1 extends Fragment {    public static Fragment getinstance(String title ){        Fragment1 fragment1=new Fragment1();        //创建Bundle        Bundle bundle=new Bundle();//添加值        bundle.putString("title",title);        //把值添加到Arguments中        fragment1.setArguments(bundle);        return fragment1;    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        Bundle arguments = getArguments();        String title = arguments.getString("title");        TextView textView = new TextView(getActivity());        textView.setText(title);        return textView;    }}

原创粉丝点击