XListView刷新加载,网络加载Xlv数据,网络加载ViewPager自动轮播,侧拉,TabLayout

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private DrawerLayout drawerLayout;
    private ArrayList list;
    private TabLayout tabLayout;
    private ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pager = findViewById(R.id.pagers);
        drawerLayout = findViewById(R.id.drawer);
        tabLayout = findViewById(R.id.tab);

        list = new ArrayList<>();
        list.add(new Afragment());
        list.add(new Bfragment());
        list.add(new Cfragment());
        list.add(new Dfragment());

        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

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

        Tab();

        initActionBar();
    }

    private void initActionBar() {
        //获取一个ActionBar对象
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        //给左上角一张图片,4.0意思默认图片,给true就可以直接使用
        actionBar.setDisplayHomeAsUpEnabled(true);
        //有一类提供了绑定DrawerLayout功能
        toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
        //DrawerLayout和ActionBar关联
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);

    }

    //A.设置左上角按钮具备点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void Tab() {   //设置TabLayout

        //tabLayout指示器有几个,创建几个
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());
        tabLayout.addTab(tabLayout.newTab());

        tabLayout.setupWithViewPager(pager);

        tabLayout.getTabAt(0).setText("蒸");
        tabLayout.getTabAt(1).setText("蒸");
        tabLayout.getTabAt(2).setText("日");
        tabLayout.getTabAt(3).setText("上");
    }
}

activity_main.xml



    

        


        

    

    


Afargment.java

public class Afragment extends Fragment{
    private ViewPager pager;
    private XListView xListView;
    public int page=1;
    private ItemAdapter itemAdapter;
    BannerAdapter bannerAdapter;

    public String path_xlistview="http://www.xieast.com/api/news/news.php?page="+page+"";
    public String path_viewpager="http://www.xieast.com/api/banner.php";

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int currentItem = pager.getCurrentItem();
            if (currentItem==bannerAdapter.getList().size()-1){
                     currentItem=0;
            }else {
                currentItem++;
            }
            pager.setCurrentItem(currentItem);

            handler.sendEmptyMessageDelayed(0,2000);
        }
    };

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

        pager = view.findViewById(R.id.pager);
        xListView = view.findViewById(R.id.xlv);

        itemAdapter = new ItemAdapter(getLayoutInflater());
        xListView.setAdapter(itemAdapter);

        bannerAdapter = new BannerAdapter(getLayoutInflater());
        pager.setAdapter(bannerAdapter);

        xListView.setPullLoadEnable(true);
        xListView.setPullRefreshEnable(true);
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page=1;

                initData(path_xlistview);
            }

            @Override
            public void onLoadMore() {

                initData(path_xlistview);
            }
        });

        initData(path_xlistview);

        initViewData(path_viewpager);
        handler.sendEmptyMessageDelayed(0,2000);
        return view;
    }

    private void initViewData(String path_viewpager) {

        new AsyncTask>() {
            @Override
            protected ArrayList doInBackground(String... strings) {
                String requestData2 = HttpUrl.requestData(strings[0]);
                BannerBean bannerBean = new Gson().fromJson(requestData2, BannerBean.class);

                if(bannerBean!=null){
                    return (ArrayList) bannerBean.getData();
                }
                return null;
            }

            @Override
            protected void onPostExecute(ArrayList dataBeans) {
                super.onPostExecute(dataBeans);
                bannerAdapter.setDatas(dataBeans);
            }
        }.execute(path_viewpager);
    }

    private void initData(String path_xlistview) {

        new AsyncTask>() {
            @Override
            protected ArrayList doInBackground(String... strings) {
                String requestData = HttpUrl.requestData(strings[0]);

                ItemBean ss = new Gson().fromJson(requestData, ItemBean.class);
                if(ss!=null){
                    return (ArrayList) ss.getData();
                }

                return null;
            }

            @Override
            protected void onPostExecute(ArrayList dataBeans) {
                super.onPostExecute(dataBeans);

                updateData(dataBeans);
                closeXlv();
            }
        }.execute(path_xlistview);
    }

    private void updateData(ArrayList dataBeans){

        if (page==1){
            itemAdapter.setDatas(dataBeans);
        }else {
            itemAdapter.addDatas(dataBeans);
        }
    }

    private void closeXlv(){
        page++;
        xListView.stopRefresh();
        xListView.stopLoadMore();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeMessages(0);
    }
}

afrag.xml



    

    

    

HttpUrl.java

public class HttpUrl {
    public static String requestData(String string){
        String getgson=null;
        try {
            URL url = new URL(string);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(50*1000);
            urlConnection.setReadTimeout(50*1000);
            int responseCode = urlConnection.getResponseCode();
            if(responseCode==200){
                String gsonString = stream2String(urlConnection.getInputStream());
                getgson=gsonString;

            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return getgson;
    }

    private static String stream2String(InputStream inputStream) {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        try {
            for (String tmp = bufferedReader.readLine(); tmp!=null; tmp=bufferedReader.readLine()) {
                stringBuilder.append(tmp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuilder.toString();
    }
}

ItemAdapter.java

public class ItemAdapter extends BaseAdapter{

    private LayoutInflater layoutInflater;
    ArrayList li;

    public ItemAdapter(LayoutInflater layoutInflater) {
        this.layoutInflater = layoutInflater;
        li = new ArrayList<>();
    }

    public void setDatas(ArrayList datas) {
        li.clear();
        li.addAll(datas);
        notifyDataSetChanged();
    }

    public void addDatas(ArrayList dataBeans) {

        li.addAll(dataBeans);
        notifyDataSetChanged();
    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder=null;
        if (view==null){
            view = layoutInflater.inflate(R.layout.item, viewGroup, false);

            viewHolder = new ViewHolder(view);
        }else {

            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.update((ItemBean.DataBean) getItem(i));
        return view;
    }


    class ViewHolder{
        private ImageView imageView;
        private TextView text1;
        private TextView text2;

        public ViewHolder(View view) {

            imageView = view.findViewById(R.id.image);
            text1 = view.findViewById(R.id.t1);
            text2 = view.findViewById(R.id.t2);
            view.setTag(this);
        }

        public void update(ItemBean.DataBean item) {
            text1.setText(item.getTitle());
            text2.setText(item.getAuthor_name());

            ImageLoader.getInstance().displayImage(item.getThumbnail_pic_s(),imageView);
        }
    }
}

BannerAdapter.java

public class BannerAdapter extends PagerAdapter{
    private LayoutInflater layoutInflater;
    ArrayList list;

    public ArrayList getList() {
        return list;
    }

    public BannerAdapter(LayoutInflater layoutInflater) {
        this.layoutInflater = layoutInflater;
        list = new ArrayList<>();
    }

    public void setDatas(ArrayList datas) {
        list.addAll(datas);
        notifyDataSetChanged();
    }

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

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ImageView imageView = new ImageView(layoutInflater.getContext());
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        ImageLoader.getInstance().displayImage(list.get(position).getImg(),imageView);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}

App.java

public class App extends Application{
    @Override
    public void onCreate() {
        super.onCreate();

      //  RoundedBitmapDisplayer bitmapDisplayer = new RoundedBitmapDisplayer(20);

        BitmapDisplayer bitmapDisplayer = new CircleBitmapDisplayer();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .displayer(bitmapDisplayer)
                .showImageOnLoading(R.drawable.ic_launcher)
                .showImageForEmptyUri(R.drawable.ic_launcher)
                .cacheInMemory(true)
                .build();


        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration
                .Builder(this)
                .defaultDisplayImageOptions(options)
                .build();

        ImageLoader.getInstance().init(configuration);
    }
}

你可能感兴趣的:(XListView刷新加载,网络加载Xlv数据,网络加载ViewPager自动轮播,侧拉,TabLayout)