侧拉,多条目展示并刷新加载

package com.example.week2;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {

侧拉
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                viewpager.setCurrentItem(0);
                return true;
            case R.id.navigation_dashboard:
                viewpager.setCurrentItem(1);
                return true;
            case R.id.navigation_notifications:
                viewpager.setCurrentItem(2);
                return true;
        }
        return false;
    }
};
private ViewPager viewpager;
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    viewpager =findViewById(R.id.viewpager);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.left_drawer,new Fragment_left())
            .commit();
    drawer =findViewById(R.id.drawer);
    toggle = new ActionBarDrawerToggle(this, drawer, R.string.drawer_open, R.string.drawer_close);
    //同步状态
    toggle.syncState();
    drawer.addDrawerListener(toggle);
    viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            switch (i){
                case 0:
                    return new FragmentOne();
                case 1:
                    return new FragmentTwo();
                case 2:
                    return new FragmentTwo();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 3;
        }
    });
    final BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int i) {
            switch (i){
                case 0:
                    navigation.setSelectedItemId(R.id.navigation_home);
                    break;
                case 1:
                    navigation.setSelectedItemId(R.id.navigation_dashboard);
                    break;
                case 2:
                    navigation.setSelectedItemId(R.id.navigation_notifications);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (toggle.onOptionsItemSelected(item)){
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void closedrawer(){
    drawer.closeDrawer(Gravity.START);
}

}
多条目展示并刷新加载
package com.example.week2;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.xlistview.src.me.maxwin.view.XListView;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class FragmentOne extends Fragment {

private XListView xListView;
private MAdapter2 mAdapter2;
private String apiurl="http://www.xieast.com/api/news/news.php?page=";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmentone, container, false);
    xListView =view.findViewById(R.id.xListView);
    mAdapter2 = new MAdapter2(getActivity());
    xListView.setAdapter(mAdapter2);
    xListView.setPullLoadEnable(true);
    xListView.setXListViewListener(new XListView.IXListViewListener() {
        @Override
        public void onRefresh() {
            page=1;
            init();
        }

        @Override
        public void onLoadMore() {
            init();
        }
    });
    init();
    return view;
}
private int page;
@SuppressLint("StaticFieldLeak")
private void init() {
    new AsyncTask<String,Void,List<UserBean.DataBean>>(){

        @Override
        protected List<UserBean.DataBean> doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setConnectTimeout(5000);
                urlConnection.setReadTimeout(5000);
                int responseCode = urlConnection.getResponseCode();
                if(responseCode==200){
                    InputStream inputStream = urlConnection.getInputStream();
                    String result = strams(inputStream);
                   Gson gson=new Gson();
                    UserBean userBean = gson.fromJson(result, UserBean.class);
                    return  userBean.getData();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<UserBean.DataBean> dataBeans) {
            super.onPostExecute(dataBeans);
            if(dataBeans==null){
                xListView.stopLoadMore();
                xListView.stopRefresh();
                return;
            }
            if(page==1) {
                mAdapter2.setList(dataBeans);
            }else{
                mAdapter2.addList(dataBeans);
            }
            xListView.setPullLoadEnable(dataBeans.size()>0);
            xListView.stopLoadMore();
            xListView.stopRefresh();
            page++;
        }
    }.execute(apiurl+page);
}
private String strams(InputStream inputStream) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    for(String temp=bufferedReader.readLine();temp!=null;temp=bufferedReader.readLine()){
        stringBuilder.append(temp);
    }
    return stringBuilder.toString();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值