ViewPager+侧滑+无线轮播

无线轮播布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bwie.zhoukaob.MainActivity">

    <com.youth.banner.Banner
        android:id="@+id/ban"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        >
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="进入系统"
            android:layout_gravity="center"
            android:layout_marginTop="70dp"
            />
    </com.youth.banner.Banner>
</RelativeLayout>

无限轮播代码

package com.bwie.zhoukaob;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Banner banner;
    private List<String> list;
    private Button button;
    public final static String Ban[] = new String[]{
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_b329262313d97d9d.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_2fb703a965d000b5.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_6c53224efe151d84.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_3ff0b4622ed21b57.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_c7b4c6c2a9b6bd6d.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_74128fa6ca93ca4a.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_c9b044b98d2b1e8b.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_06ea598f5f2593e2.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_a528c52a13cf9f07.jpg",
            "http://juheimg.oss-cn-hangzhou.aliyuncs.com/cookbook/s/2/108_b641527e8451f21a.jpg"
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button) findViewById(R.id.button);
        banner = (Banner) findViewById(R.id.ban);
        list = new ArrayList<>();
        for (int i = 0; i < Ban.length; i++){
            list.add(Ban[i]);
        }

//无限轮播
//设置加载样式
        banner.setIndicatorGravity(Banner.RIGHT);
        banner.setBannerStyle(Banner.CIRCLE_INDICATOR_TITLE);
//是否自动轮播
        banner.isAutoPlay(true);
//设置轮播的时间间隔
        banner.setDelayTime(3000);
//设置加载的图片,解析出来的url
        banner.setImages(list);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it=new Intent(MainActivity.this,MyActivity.class);
                startActivity(it);
            }
        });
    }
}

主界面布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_my"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.zhoukaob.MyActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
       <ImageView
           android:id="@+id/image"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@mipmap/shaidan"
           />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/red"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@color/red"
            app:tabTextColor="@color/black"/>
        <android.support.v4.view.ViewPager
            android:id="@+id/vp_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <ListView
        android:id="@+id/lv"
        android:layout_width="240sp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#f00"
        android:dividerHeight="1dp"
        android:background="#fff"
        />
</android.support.v4.widget.DrawerLayout>


主界面代码

package com.bwie.zhoukaob;

import android.support.design.widget.TabLayout;
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.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.bwie.adapter.CeLaAdapter;
import com.bwie.bean.QQcela;
import com.bwie.fragment.ChannelFragment;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends AppCompatActivity {

    private ImageView imageView;
    private ListView listView;
    private DrawerLayout drawerLayout;
    private List<QQcela> list;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private String[] channels = {"推荐","热点","体育","娱乐","社会","汽车","教育","财经","科技","游戏"};
    private String[] Urls={
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",
            "http://gank.io/api/data/Android/10/1",};
    private LayoutInflater mInflater;
    private List<ChannelFragment> mViewList = new ArrayList<>();//页卡视图集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        mViewPager = (ViewPager) findViewById(R.id.vp_view);
        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        mInflater = LayoutInflater.from(this);
        imageView=(ImageView) findViewById(R.id.image);
        listView=(ListView) findViewById(R.id.lv);
        drawerLayout=(DrawerLayout) findViewById(R.id.activity_my);
        list=new ArrayList<QQcela>();
        list.add(new QQcela("搜索",R.mipmap.ic_launcher));
        list.add(new QQcela("收藏",R.mipmap.ic_launcher));
        list.add(new QQcela("消息",R.mipmap.ic_launcher));
        list.add(new QQcela("离线",R.mipmap.ic_launcher));
        list.add(new QQcela("活动",R.mipmap.ic_launcher));

        CeLaAdapter adapter=new CeLaAdapter(list,MyActivity.this);
        listView.setAdapter(adapter);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(listView);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                drawerLayout.closeDrawer(listView);
            }
        });
        for(int i=0;i<channels.length;i++){
            //创建栏目的fragment
            ChannelFragment fragment = new ChannelFragment();
            Bundle b = new Bundle();
            b.putString("name", channels[i]);//传递名字
            b.putString("url", Urls[i]);
            fragment.setArguments(b);
            //收集fragment
            mViewList.add(fragment);
            //给tablayout添加tab选项卡
            mTabLayout.addTab(mTabLayout.newTab().setText(channels[i]));//添加tab选项卡

        }
        FragmentManager fm = getSupportFragmentManager();
        MyFragmentPagerAdapter mAdapter = new MyFragmentPagerAdapter(fm, mViewList);
        mViewPager.setAdapter(mAdapter);//给ViewPager设置适配器
        mTabLayout.setupWithViewPager(mViewPager);//将TabLayout和ViewPager关联起来。
        mTabLayout.setTabsFromPagerAdapter(mAdapter);//给Tabs设置适配器
    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter{
        private List<ChannelFragment> mViewList;

        public MyFragmentPagerAdapter(FragmentManager fm, List<ChannelFragment> mViewList) {
            super(fm);
            this.mViewList = mViewList;
        }

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

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return channels[position];//页卡标题
        }

    }
}


 ChannelFragment

package com.bwie.fragment;

import android.os.AsyncTask;
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.ListView;

import com.bwie.adapter.PullRefreshLvAdapter;
import com.bwie.bean.User;
import com.bwie.dao.NewDao;
import com.bwie.utils.NetWorksUtils;
import com.bwie.zhoukaob.R;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.List;

/**
 * Created by 1 on 2017/9/16.
 */
public class ChannelFragment extends Fragment{
    private String name;
    private String news_url;
    private PullToRefreshListView listView;
    private  List<User.ResultsBean> results;
    private PullRefreshLvAdapter adapter;
    private NewDao newDao;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        name = (String) bundle.get("name");
        news_url = (String) bundle.get("url");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.channelfragment, null);
        listView=(PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
        pullToRefreshData();
        newDao=new NewDao(getActivity());

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                            pullToRefreshData();
            }
        });
        listView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                     loadMoreData();
            }
        });
        return view;
    }
    public void pullToRefreshData(){
        new AsyncTask<String,Integer,String>(){

            @Override
            protected String doInBackground(String... strings) {
                String str=new NetWorksUtils().getJsonUrlJiexi(news_url);
                return str;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                User user = gson.fromJson(s, User.class);
                results = user.getResults();
                for (int i=0;i<results.size();i++){
                    String desc=results.get(i).getDesc();
                    String type=results.get(i).getType();
                    newDao.insert(desc,type);
                }
                adapter=new PullRefreshLvAdapter(results, getActivity());
                listView.setAdapter(adapter);
                listView.onRefreshComplete();
            }
        }.execute(news_url);
    }
    public void loadMoreData(){
        new AsyncTask<String,Integer,String>(){

            @Override
            protected String doInBackground(String... strings) {
                String str=new NetWorksUtils().getJsonUrlJiexi(news_url);
                return str;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                User user = gson.fromJson(s, User.class);
                List<User.ResultsBean> results2 = user.getResults();
                results.addAll(results2);
                adapter.notifyDataSetChanged();
            }
        }.execute(news_url);
    }
}

ChannelFragment布局

<?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.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true"
        ptr:ptrAnimationStyle="rotate"
        ptr:ptrHeaderTextColor="#ffffff"
        ptr:ptrHeaderSubTextColor="#00ffff"
        ptr:ptrHeaderBackground="@null"
        ptr:ptrDrawable="@mipmap/ic_launcher"/>
</LinearLayout>


PullRefreshLvAdapter适配器

package com.bwie.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.bean.User;
import com.bwie.zhoukaob.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by 1 on 2017/9/16.
 */

public class PullRefreshLvAdapter extends BaseAdapter{
    private List<User.ResultsBean> list;
    private Context context;

    public PullRefreshLvAdapter(List<User.ResultsBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

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

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

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        int rs=0;
        if(list.get(position).getImages()!=null){
            rs=0;
        }else if (list.get(position).getImages()==null){
            rs=1;
        }
        return rs;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        ViewHolder2 viewHolder2;
        int rs=getItemViewType(i);
        if(rs==1){
            if(view==null){
                viewHolder=new ViewHolder();
                view=View.inflate(context, R.layout.item2,null);
                viewHolder.textView=(TextView) view.findViewById(R.id.texts);
                view.setTag(viewHolder);
            }else {
                viewHolder=(ViewHolder) view.getTag();
            }
            viewHolder.textView.setText(list.get(i).getDesc());
        }else if (rs==0){
            if (view==null){
                viewHolder2=new ViewHolder2();
                view=View.inflate(context,R.layout.item,null);
                viewHolder2.imageView2=(ImageView) view.findViewById(R.id.image2);
                viewHolder2.textView2=(TextView) view.findViewById(R.id.text);
                view.setTag(viewHolder2);
            }else{
                viewHolder2=(ViewHolder2) view.getTag();
            }
            ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0),viewHolder2.imageView2);
            viewHolder2.textView2.setText(list.get(i).getDesc());
        }
        return view;
    }
    class  ViewHolder{
        TextView textView;
    }
    class ViewHolder2{
        ImageView imageView2;
        TextView textView2;
    }
}

item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:id="@+id/image2"
    android:layout_width="50dp"
    android:layout_height="50dp" />
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>



item2

<?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:id="@+id/texts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>


celaitem

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/imageView"
        android:layout_weight="1" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_weight="1" />
</LinearLayout>


CeLaAdapter适配


package com.bwie.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.bean.QQcela;
import com.bwie.zhoukaob.R;

import java.util.List;

/**
 * Created by 1 on 2017/9/16.
 */

public class CeLaAdapter extends BaseAdapter {
    private List<QQcela> list;
    private Context context;

    public CeLaAdapter(List<QQcela> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view=View.inflate(context, R.layout.celaitem,null);
        ImageView imageView=(ImageView) view.findViewById(R.id.imageView);
        TextView textView=(TextView) view.findViewById(R.id.textView);
        imageView.setImageResource(list.get(i).getImg());
        textView.setText(list.get(i).getName());
        return view;
    }
}


NetWorksUtils工具



package com.bwie.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by 1 on 2017/9/16.
 */

public class NetWorksUtils {
    public String getJsonUrlJiexi(String jsonstr){
        URL url=null;
        HttpURLConnection httpURLConnection=null;
        String JsonUrl="";
        try {
            url=new URL(jsonstr);
            httpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setReadTimeout(5000);
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode==200){
                InputStream inputStream = httpURLConnection.getInputStream();
                byte[] bytes = new byte[1024];
                int i=0;
                while ((i=inputStream.read(bytes))!=-1){
                    JsonUrl +=new String(bytes,0,i);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JsonUrl;
    }
}


MyApp

package com.bwie.app;

import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
 * Created by 1 on 2017/9/16.
 */

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(this);
        ImageLoader.getInstance().init(configuration);
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值