带滑动条的ViewPager的框架(无具体数据+侧拉框)

//主布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawble"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.day16_20171215_1.MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            android:id="@+id/vpage"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <RadioGroup
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radio"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="30dp"
                android:id="@+id/rb1"
                android:text="首页"
                android:gravity="center_horizontal"
                android:button="@null" />
            <RadioButton
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="30dp"
                android:id="@+id/rb2"
                android:text="搜索"
                android:gravity="center_horizontal"
                android:button="@null" />
            <RadioButton
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:id="@+id/rb3"
                android:text="我的"
                android:gravity="center_horizontal"
                android:button="@null" />
        </RadioGroup>
    </RelativeLayout>
    <ListView
        android:background="#ff00ff"
        android:layout_gravity="start"
        android:id="@+id/lvs"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</android.support.v4.widget.DrawerLayout>

//主方法

package com.example.day16_20171215_1;
import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.day16_20171215_1.Adapter.MyAdapter;
import com.example.day16_20171215_1.Adapter.MyAdapter2;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    private ViewPager vpage;
    private RadioGroup radio;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private DrawerLayout dawlaout;
    private ListView lvs;
    private ArrayList<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        dawlaout=(DrawerLayout)findViewById(R.id.drawble);
        vpage=(ViewPager)findViewById(R.id.vpage);
        lvs=(ListView)findViewById(R.id.lvs);
        radio=(RadioGroup)findViewById(R.id.radio);
        rb1=(RadioButton)findViewById(R.id.rb1);
        rb2=(RadioButton)findViewById(R.id.rb2);
        rb3=(RadioButton)findViewById(R.id.rb3);
        vpage.setAdapter(new MyAdapter(getSupportFragmentManager()));
        lvs.setAdapter(new MyAdapter2(this,list));
        vpage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    switch(position){
                        case 0:
                            rb1.setTextColor(Color.RED);
                            rb2.setTextColor(Color.BLACK);
                            rb3.setTextColor(Color.BLACK);
                            break;
                        case 1:
                            rb2.setTextColor(Color.RED);
                            rb1.setTextColor(Color.BLACK);
                            rb3.setTextColor(Color.BLACK);
                            break;
                        case 2:
                            rb3.setTextColor(Color.RED);
                            rb2.setTextColor(Color.BLACK);
                            rb1.setTextColor(Color.BLACK);
                            break;
                    }
            }
            @Override
            public void onPageSelected(int position) {
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        vpage.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        vpage.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        vpage.setCurrentItem(2);
                        break;
                }
            }
        });
        lvs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        vpage.setCurrentItem(0);
                        break;
                    case 1:
                        vpage.setCurrentItem(1);
                        break;
                    case 2:
                        vpage.setCurrentItem(2);
                        break;
                }
                dawlaout.closeDrawer(lvs);
            }
        });
    }
    private void initData() {
        list=new ArrayList<>();
        list.add("首页");
        list.add("搜索");
        list.add("我的");
    }
}

//页面一的布局(即Fragment1)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.astuetz.PagerSlidingTabStrip
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:id="@+id/tabs"/>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp"></android.support.v4.view.ViewPager>
</LinearLayout>

//页面一的代码

package com.example.day16_20171215_1.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.astuetz.PagerSlidingTabStrip;
import com.example.day16_20171215_1.Adapter.MyAdapter3;
import com.example.day16_20171215_1.R;
/**
 * Created by lenovo on 2017/12/15.
 */
public class Fragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment1, null);
      //这里是有一部分的滑动条代码的
        ViewPager vp= (ViewPager) view.findViewById(R.id.vp);
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        vp.setAdapter(new MyAdapter3(getActivity().getSupportFragmentManager()));
        //标题和viewpager进行关联
        tabs.setViewPager(vp);
        return view;
    }
}
//页面二的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试数据2"/>
</LinearLayout>
//页面二的代码
package com.example.day16_20171215_1.Fragment;
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 com.example.day16_20171215_1.R;
/**
 * Created by lenovo on 2017/12/15.
 */
public class Fragment2 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment2, null);
        return view;
    }
}
//页面三的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试数据3"/>
</LinearLayout>
//页面三的代码
package com.example.day16_20171215_1.Fragment;
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 com.example.day16_20171215_1.R;
/**
 * Created by lenovo on 2017/12/15.
 */
public class Fragment3 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fragment3, null);
        return view;
    }
}
//下面就是滑动条的代码了
//首先是第一个页面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
      <TextView
          android:text="搜索"
          android:textSize="35dp"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
</LinearLayout>
//第一个页面的代码
package com.example.day16_20171215_1.Fragment;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.View;
import com.example.day16_20171215_1.R;
/**
 * Created by lenovo on 2017/12/18.
 */
public class Fragment1_1 extends Fragment{
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        View view = View.inflate(getActivity(),R.layout.fragment1_1,null);
        return view;
    }
}
//第二个页面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="ssee"
        android:textSize="35dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
//第二个页面的代码
package com.example.day16_20171215_1.Fragment;
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 com.example.day16_20171215_1.R;
/**
 * Created by lenovo on 2017/12/18.
 */
public class Fragment1_2 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment1_2, null);
        return view;
    }
}
//第三个页面的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="wode"
        android:textSize="35dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
//第三个页面的代码
package com.example.day16_20171215_1.Fragment;
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 com.example.day16_20171215_1.R;
/**
 * Created by lenovo on 2017/12/18.
 */
public class Fragment1_3 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment1_3, null);
        return view;
    }
}
//ViewPager的适配器
package com.example.day16_20171215_1.Adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.day16_20171215_1.Fragment.Fragment1;
import com.example.day16_20171215_1.Fragment.Fragment2;
import com.example.day16_20171215_1.Fragment.Fragment3;
/**
 * Created by lenovo on 2017/12/15.
 */
public class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
    Fragment fragment=null;
        switch (position){
            case 0:
                fragment= new Fragment1();
                break;
            case 1:
                fragment=new Fragment2();
                break;
            case 2:
                fragment=new Fragment3();
                break;
        }
        return fragment;
    }
    @Override
    public int getCount() {
        return 3;
    }
}
//侧拉框的适配器
package com.example.day16_20171215_1.Adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
 * Created by lenovo on 2017/12/15.
 */
public class MyAdapter2 extends BaseAdapter{
    private List<String> list;
    private Context con;
    public MyAdapter2(Context con,List<String> list) {
        this.con = con;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv=new TextView(con);
        tv.setText(list.get(position));
        tv.setTextSize(35);
        return tv;
    }
}
//滑动条的适配器
package com.example.day16_20171215_1.Adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.example.day16_20171215_1.Fragment.Fragment1_1;
import com.example.day16_20171215_1.Fragment.Fragment1_2;
import com.example.day16_20171215_1.Fragment.Fragment1_3;
/**
 * Created by lenovo on 2017/12/18.
 */
public class MyAdapter3 extends FragmentPagerAdapter{
    private String [] arr ={"页面一","页面二","页面三"};
    public MyAdapter3(FragmentManager fm) {
        super(fm);
    }
    /**
     *
     * @param position
     * @return
     */
    @Override
    public CharSequence getPageTitle(int position) {
        return arr[position];
    }
    @Override
    public Fragment getItem(int position) {
        Fragment fragment=null;
        switch (position){
            case 0:
                fragment=new Fragment1_1();
                break;
            case 1:
                fragment=new Fragment1_2();
                break;
            case 2:
                fragment=new Fragment1_3();
                break;
        }
        return fragment;
    }
    @Override
    public int getCount() {
        return 3;
    }
}
//由于没有相应的数据展示,所以不用在配置文件里添加权限
//但是需要一些网络支持
 compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
上面的是导航栏的依赖(也就是侧拉框的)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值