dengdai

//记得添加权限,导入包

//Mainactivity

public class MainActivity extends AppCompatActivity {
    ArrayList<Fragment> fraarr = new ArrayList<>();
    ArrayList<Button> btarr = new ArrayList<>();
    private ViewPager vp;
    private Button bt1;
    private DrawerLayout drawerlayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp);
        Button bt1 = (Button) findViewById(R.id.bt1);
        Button bt2 = (Button) findViewById(R.id.bt2);
        Button bt3 = (Button) findViewById(R.id.bt3);
        Button bt4 = (Button) findViewById(R.id.bt4);
        btarr.add(bt1);
        btarr.add(bt2);
        btarr.add(bt3);
        btarr.add(bt4);
        vpshuju();
        vpspq vpspq = new vpspq(getSupportFragmentManager());
        vp.setAdapter(vpspq);
//点击变色
        for(int y=0;y<btarr.size();y++){
            Button button = btarr.get(y);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //获取点击ID
                    int id = view.getId();
                    Log.e("chen", "onClick: ------"+id );
                    vp.setCurrentItem(id%btarr.size());
                }
            });
        }
//滑动变色
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for(int i=0;i<btarr.size();i++){
                    if(i==position%btarr.size()){
                        Button button = btarr.get(i);
                        button.setTextColor(Color.RED);
                    }else{
                        btarr.get(i).setTextColor(Color.BLACK);
                    }
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class vpspq extends FragmentPagerAdapter {


        public vpspq(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fraarr.get(position);
        }

        @Override
        public int getCount() {
            return fraarr.size();
        }
    }
    //自己新建frament充当viewpager的数据源
    public void vpshuju(){
        yi yi = new yi();
        er er = new er();
        san san = new san();
        si si = new si();
        fraarr.add(yi);
        fraarr.add(er);
        fraarr.add(san);
        fraarr.add(si);
    }
}


//Mainactivity的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout

    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.yue10kao.MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.v4.view.ViewPager
            android:id="@+id/vp"
            android:layout_width="wrap_content"
            android:layout_height="600dp">
        </android.support.v4.view.ViewPager>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:text="你好啊"
                android:textColor="#cc0000"
                android:id="@+id/bt1"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <Button
                android:text="我好啊"
                android:id="@+id/bt2"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <Button
                android:text="大家好"
                android:id="@+id/bt3"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
            <Button
                android:text="好好好"
                android:id="@+id/bt4"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />
        </LinearLayout>

</LinearLayout>
    <LinearLayout
        android:id="@+id/ll"
        android:background="@color/colorAccent"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice">
        <ImageView
            android:id="@+id/img"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:text="电饭锅"
            android:layout_width="200dp"
            android:layout_height="match_parent"/>
        <TextView
            android:text="老师的"
            android:layout_width="200dp"
            android:layout_height="match_parent"/>
        <TextView
            android:text="个单价"
            android:layout_width="200dp"
            android:layout_height="match_parent"/>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>


// Frament集合里面的  yi

public class yi extends Fragment{
    private List<String> datas = new ArrayList<String>();
    private View inflate;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = View.inflate(getActivity(), R.layout.one, null);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//获取控件
        tabLayout = (TabLayout) inflate.findViewById(R.id.tas);
        viewPager = (ViewPager) inflate.findViewById(R.id.tasvp);
//横向滑动的集合"福利", "Android", "iOS", "休息视频", "拓展资源", "前端", "all
        datas.add("福利");
        datas.add("Android");
        datas.add("iOS");
        datas.add("休息视频");
        datas.add("拓展资源");
        datas.add("前端");
        datas.add("all");
//适配器
        vpsp vpsp = new vpsp(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(vpsp);
//进行关联
        tabLayout.setupWithViewPager(viewPager);
    }

    class vpsp extends FragmentPagerAdapter{
        //有参数的构造
        public vpsp(FragmentManager fm) {
            super(fm);
        }
        //返回选项卡的文本 ,,,添加选项卡
        @Override
        public CharSequence getPageTitle(int position) {
            return datas.get(position);
        }
        //创建fragment对象并返回
        @Override
        public Fragment getItem(int position) {
            content content = new content();
            Bundle bundle = new Bundle();
            bundle.putString("title",datas.get(position));
            content.setArguments(bundle);
            return content;        }
        //返回数量
        @Override
        public int getCount() {
            return datas.size();
        }
    }
}

//yi的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tas"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabTextColor="@color/colorPrimary"
        >
    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tasvp"
        android:layout_below="@+id/tas"
        >
    </android.support.v4.view.ViewPager>


</LinearLayout>

//content  java文件


public class content extends Fragment {
    ArrayList<ex> arr = new ArrayList<>();
    private View view;
    private PullToRefreshListView lv;
    private int i=1;
    private Myadapter myadapter;
    private String title;
    private TextView cte;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.contentlayout, null);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle arguments = getArguments();
//        Log.e("chen", "onCreateView: 我是关注"+arguments.toString() );
        //获取值
        title = arguments.getString("title");
        cte = view.findViewById(R.id.cte);
        cte.setText(title);

        lv = view.findViewById(R.id.lv);
        //设置可以下拉
        lv.setMode(PullToRefreshBase.Mode.BOTH);
        myadapter = new Myadapter();
        lv.setAdapter(myadapter);       //URLEncoder.encode(title)将中文得url转换格式才能解析
        new MAsyncTask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title)+"/10/"+i);
        lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            //向下
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                arr.clear();
                i++;
                new MAsyncTask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title)+"/10/"+i);
            }
            //向上
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                i++;
                new MAsyncTask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title)+"/10/"+i);
            }
        });
    }
    class Myadapter extends BaseAdapter {
        @Override
        public int getCount() {
            return arr.size();
        }

        @Override
        public Object getItem(int i) {
            return arr.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        class chen{
            ImageView tu;
            TextView yi;
            TextView er;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ex ex = arr.get(i);
            chen chen = new chen();
            if(view==null){
                view = View.inflate(getActivity(), R.layout.lvbj, null);
                chen.tu=view.findViewById(R.id.img);
                chen.yi=view.findViewById(R.id.na);
                chen.er=view.findViewById(R.id.ti);
                view.setTag(chen);
            }else{
                chen = (chen) view.getTag();
            }
//            chen.tu.setImageResource();
            new AsyncTaskimg(chen.tu).execute(ex.getTu());
            chen.yi.setText(ex.getName());
            chen.er.setText(ex.getTime());
            return view;
        }
    }
    class AsyncTaskimg extends AsyncTask<String,Void,Bitmap> {

        private ImageView tt;

        public AsyncTaskimg(ImageView tt) {
            this.tt = tt;
        }

        private Bitmap bitmap;

        @Override
        protected Bitmap doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                //设置链接超时时间;
                urlConnection.setConnectTimeout(6000);
                //设置读取返回数据的超时时间
                urlConnection.setReadTimeout(5000);
                int responseCode = urlConnection.getResponseCode();
                //响应码200代表链接和返回数据成功
                if(responseCode ==200){
                    InputStream inputStream = urlConnection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(inputStream);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            tt.setImageBitmap(bitmap);
        }
    }
    class MAsyncTask extends AsyncTask<String,Void,String> {
        @Override
        protected String doInBackground(String... strings) {
            StringBuilder stringBuilder = new StringBuilder();
            String s="";
            try {
                URL url = new URL(strings[0]);
                URLConnection urlConnection = url.openConnection();
                InputStream inputStream = urlConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                while((s=bufferedReader.readLine())!=null){
                    stringBuilder.append(s);
                }
                String pjsj = stringBuilder.toString();
                Log.e("chen", "doInBackground: 拼接的内容"+pjsj );
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return stringBuilder.toString();
        }

        @Override
        protected void onPostExecute(String s) {
//连接适配器
            super.onPostExecute(s);
            Gson gson = new Gson();
            bean bean = gson.fromJson(s, bean.class);
            List<com.example.yue10kao.bean.ResultsBean> results = bean.getResults();
            for (int i = 0; i < results.size(); i++) {
                if(results.get(i).getImages()!=null)
                {
                    List<String> images = results.get(i).getImages();
                    String s1 = images.get(0);
                    String desc = results.get(i).getDesc();
                    String publishedAt = results.get(i).getPublishedAt();
                    arr.add(new ex(s1,desc,publishedAt));
                }else {
                    String desc = results.get(i).getDesc();
                    String publishedAt = results.get(i).getPublishedAt();
                    arr.add(new ex( desc, publishedAt));
                }
                Log.e("chen", "onPostExecute: 集合的内容"+arr );
            }
            myadapter.notifyDataSetChanged();
            lv.onRefreshComplete();
        }
    }
}

//contentlayout  布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/cte"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dfxcvnxcv"
        android:textColor="@color/colorAccent"/>
    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />

</LinearLayout>

//ex  泛型类

//集合李添加内容的泛型
public class ex {
    private String tu;
    private String name;
    private String time;

    @Override
    public String toString() {
        return "ex{" +
                "tu='" + tu + '\'' +
                ", name='" + name + '\'' +
                ", time='" + time + '\'' +
                '}';
    }

    public String getTu() {
        return tu;
    }

    public void setTu(String tu) {
        this.tu = tu;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public ex(String tu, String name, String time) {
        this.tu = tu;
        this.name = name;
        this.time = time;
    }

    public ex(String name, String time) {
        this.name = name;
        this.time = time;
    }
}

//lvbj   布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img"
        android:src="@mipmap/ic_launcher_round"
        android:layout_width="150dp"
        android:layout_height="150dp" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/na"
            android:text="dfcxc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/ti"
            android:text="sdf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>


//bean 类

public class bean {
    /**
     * error : false
     * results : [{"_id":"59b7a98e421aa911847a0392","createdAt":"2017-09-12T17:31:58.794Z","desc":"ViewStub学习","images":["http://img.gank.io/9f51b7bc-73d3-4ce4-bd59-c61cb0c64325"],"publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"http://rkhcy.github.io/2017/09/12/ViewStub%E5%AD%A6%E4%B9%A0/","used":true,"who":"HuYounger"},{"_id":"59b7d963421aa911847a0394","createdAt":"2017-09-12T20:56:03.184Z","desc":"基于Snowflake算法而写的一种unique id生成器","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/zjutkz/ASnowflake","used":true},{"_id":"59b8ad56421aa911878707ed","createdAt":"2017-09-13T12:00:22.260Z","desc":"看我逆向小米rom层应用做碎片化适配","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247486906&idx=1&sn=d6f473abffdea7b2ece6f6bb1f72e084","used":true,"who":"陈宇明"},{"_id":"59b9dfc1421aa9118c8262c1","createdAt":"2017-09-14T09:47:45.123Z","desc":"一个用 Kotlin 写的查单词 idea 插件","images":["http://img.gank.io/4939784c-eea2-4b60-9c7a-d1b4d9f9c8b5"],"publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/jungletian/BreakWords","used":true,"who":"ZhangTitanjum"},{"_id":"59ba01b6421aa911878707f5","createdAt":"2017-09-14T12:12:38.108Z","desc":"Floo 开源了,一个支持 AOP、栈控制、跨页面带信、和动态变更映射的 URL 路由","publishedAt":"2017-09-14T16:36:51.63Z","source":"web","type":"Android","url":"https://github.com/drakeet/Floo","used":true,"who":"drakeet"},{"_id":"59b667cf421aa9118887ac12","createdAt":"2017-09-11T18:39:11.631Z","desc":"用少量Rxjava代码,为retrofit添加退避重试功能","publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"http://www.jianshu.com/p/fca90d0da2b5","used":true,"who":"小鄧子"},{"_id":"59b673ef421aa9118887ac13","createdAt":"2017-09-11T19:30:55.50Z","desc":"Android 端基于 OpenCV 的边框识别功能","publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"https://pqpo.me/2017/09/11/opencv-border-recognition/","used":true,"who":"Linmin Qiu"},{"_id":"59b69738421aa9118c8262ad","createdAt":"2017-09-11T22:01:28.352Z","desc":"用ContentProvider初始化你的库","images":["http://img.gank.io/3b0b193d-6abf-4714-9d5a-5508404666f4"],"publishedAt":"2017-09-12T08:15:08.26Z","source":"web","type":"Android","url":"http://zjutkz.net/2017/09/11/%E4%B8%80%E4%B8%AA%E5%B0%8F%E6%8A%80%E5%B7%A7%E2%80%94%E2%80%94%E4%BD%BF%E7%94%A8ContentProvider%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BD%A0%E7%9A%84Library/","used":true},{"_id":"5996c673421aa96729c57262","createdAt":"2017-08-18T18:50:27.254Z","desc":"UiAutomator2.0升级填坑记","publishedAt":"2017-09-07T13:25:26.977Z","source":"web","type":"Android","url":"http://skyseraph.com/2017/06/04/Android/UiAutomator2.0%E5%8D%87%E7%BA%A7%E5%A1%AB%E5%9D%91%E8%AE%B0/","used":true,"who":"SkySeraph"},{"_id":"59a37311421aa901bcb7dbb2","createdAt":"2017-08-28T09:34:09.768Z","desc":"Scrum:官僚者们的游戏","publishedAt":"2017-09-07T13:25:26.977Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247486650&idx=1&sn=9c6e2e0fd5780aab2bcb02b823e7316e","used":true,"who":"陈宇明"}]
     */

    private boolean error;
    private List<ResultsBean> results;

    @Override
    public String toString() {
        return "bean{" +
                "error=" + error +
                ", results=" + results +
                '}';
    }

    public bean(boolean error, List<ResultsBean> results) {
        this.error = error;
        this.results = results;
    }

    public boolean isError() {
        return error;
    }

    public void setError(boolean error) {
        this.error = error;
    }

    public List<ResultsBean> getResults() {
        return results;
    }

    public void setResults(List<ResultsBean> results) {
        this.results = results;
    }

    public static class ResultsBean {
        /**
         * _id : 59b7a98e421aa911847a0392
         * createdAt : 2017-09-12T17:31:58.794Z
         * desc : ViewStub学习
         * images : ["http://img.gank.io/9f51b7bc-73d3-4ce4-bd59-c61cb0c64325"]
         * publishedAt : 2017-09-14T16:36:51.63Z
         * source : web
         * type : Android
         * url : http://rkhcy.github.io/2017/09/12/ViewStub%E5%AD%A6%E4%B9%A0/
         * used : true
         * who : HuYounger
         */

        private String _id;
        private String createdAt;
        private String desc;
        private String publishedAt;
        private String source;
        private String type;
        private String url;
        private boolean used;
        private String who;
        private List<String> images;

        @Override
        public String toString() {
            return "ResultsBean{" +
                    "_id='" + _id + '\'' +
                    ", createdAt='" + createdAt + '\'' +
                    ", desc='" + desc + '\'' +
                    ", publishedAt='" + publishedAt + '\'' +
                    ", source='" + source + '\'' +
                    ", type='" + type + '\'' +
                    ", url='" + url + '\'' +
                    ", used=" + used +
                    ", who='" + who + '\'' +
                    ", images=" + images +
                    '}';
        }

        public ResultsBean(String _id, String createdAt, String desc, String publishedAt, String source, String type, String url, boolean used, String who, List<String> images) {
            this._id = _id;
            this.createdAt = createdAt;
            this.desc = desc;
            this.publishedAt = publishedAt;
            this.source = source;
            this.type = type;
            this.url = url;
            this.used = used;
            this.who = who;
            this.images = images;
        }

        public String get_id() {
            return _id;
        }

        public void set_id(String _id) {
            this._id = _id;
        }

        public String getCreatedAt() {
            return createdAt;
        }

        public void setCreatedAt(String createdAt) {
            this.createdAt = createdAt;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public String getPublishedAt() {
            return publishedAt;
        }

        public void setPublishedAt(String publishedAt) {
            this.publishedAt = publishedAt;
        }

        public String getSource() {
            return source;
        }

        public void setSource(String source) {
            this.source = source;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public boolean isUsed() {
            return used;
        }

        public void setUsed(boolean used) {
            this.used = used;
        }

        public String getWho() {
            return who;
        }

        public void setWho(String who) {
            this.who = who;
        }

        public List<String> getImages() {
            return images;
        }

        public void setImages(List<String> images) {
            this.images = images;
        }
    }
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值