侧滑,tablayout,PullToRefreshListView

------------------------------主页面布局

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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">FrameLayout>

    <RelativeLayout
        android:id="@+id/linear_layout"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#ffffff">

        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">ListView>
    RelativeLayout>
android.support.v4.widget.DrawerLayout>
--------------------------------------------------侧滑主页面
package fragment.bwie.com.lianxi17;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

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

import fragment.bwie.com.lianxi17.Fragment_01.Fragment_01;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;
    private FrameLayout frameLayout;
    private RelativeLayout relativeLayout;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
        relativeLayout = (RelativeLayout) findViewById(R.id.linear_layout);
        listView = (ListView) findViewById(R.id.list_view);

        List list = new ArrayList<>();
        list.add("666");
        list.add("999");
        list.add("6666");
        list.add("444");

        ArrayAdapter arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,list);
        listView.setAdapter(arrayAdapter);
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new Fragment_01()).commit();
        drawerLayout.closeDrawer(relativeLayout);


    }
}
--------------------------------------------------tablayout,viewpager布局
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="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/table_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="center"
        app:tabIndicatorColor="#FF4600"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="#0078D7"
        app:tabTextColor="#000000"
        >android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">android.support.v4.view.ViewPager>

LinearLayout>
--------------------------------------------tablayout,viewpager布局
package fragment.bwie.com.lianxi17.Fragment_01;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import fragment.bwie.com.lianxi17.R;

/**
 * Created by CZ on 2017/9/19.
 */
public class Fragment_01 extends Fragment {

    private TabLayout tabLayout;
    private ViewPager pager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, container, false);
        tabLayout = (TabLayout) view.findViewById(R.id.table_layout);
        pager = (ViewPager) view.findViewById(R.id.pager);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //"头条","社会","国内","娱乐","体育","军事","科技","财经","时尚"
        final List list = new ArrayList<>();
        list.add("头条");
        list.add("社会");
        list.add("国内");
        list.add("娱乐");
        list.add("体育");
        list.add("军事");
        list.add("科技");
        list.add("财经");
        list.add("时尚");
        pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position);
            }

            @Override
            public Fragment getItem(int position) {
                NewsFragment newsFragment = new NewsFragment();
                Bundle bundle = new Bundle();
                if (list.get(position).equals("头条")) {
                    bundle.putString("name", "头条");
                } else if (list.get(position).equals("社会")) {
                    bundle.putString("name", "社会");
                } else if (list.get(position).equals("国内")) {
                    bundle.putString("name", "国内");
                } else if (list.get(position).equals("娱乐")) {
                    bundle.putString("name", "娱乐");
                } else if (list.get(position).equals("体育")) {
                    bundle.putString("name", "体育");
                } else if (list.get(position).equals("军事")) {
                    bundle.putString("name", "军事");
                } else if (list.get(position).equals("科技")) {
                    bundle.putString("name", "科技");
                } else if (list.get(position).equals("财经")) {
                    bundle.putString("name", "财经");
                } else if (list.get(position).equals("时尚")) {
                    bundle.putString("name", "时尚");
                }
                newsFragment.setArguments(bundle);
                return newsFragment;
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        tabLayout.setupWithViewPager(pager);
    }
}
-----------------------------------------------
PullToRefreshListView布局
package fragment.bwie.com.lianxi17.Fragment_01;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import fragment.bwie.com.lianxi17.R;
import fragment.bwie.com.lianxi17.adapter.MyAdapter;
import fragment.bwie.com.lianxi17.bena.Bean;

/**
 * Created by CZ on 2017/9/19.
 */
public class NewsFragment extends Fragment {

    private PullToRefreshListView listView;
    private ILoadingLayout startLabels;
    private ILoadingLayout endLabels;
    private int num = 1;
    private List list = new ArrayList<>();
    private MyAdapter myAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.new_layout, container, false);
        listView = (PullToRefreshListView) view.findViewById(R.id.refresh_list_view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        final String string = bundle.getString("name", "头条");
        Log.i("-----------",string);
        getData(string);


        listView.setMode(PullToRefreshBase.Mode.BOTH);
        startLabels = listView.getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新");
        startLabels.setRefreshingLabel("正在刷新...");
        startLabels.setReleaseLabel("放开刷新");

        endLabels = listView.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新");
        endLabels.setRefreshingLabel("正在载入...");
        endLabels.setReleaseLabel("放开刷新...");
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            //下拉
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                num = 1;
                getData(string);
                myAdapter.notifyDataSetChanged();
            }

            //上啦
            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                num++;
                getData(string);
                myAdapter.notifyDataSetChanged();
            }
        });

    }

    private void getData(final String string) {
        AsyncTask asyncTask = new AsyncTask() {
            @Override
            protected String doInBackground(Void... voids) {
                String path = null;
                try {
                    path = "http://v.juhe.cn/toutiao/index?type=" + URLEncoder.encode(string,"utf-8") + "&key=c4479ad58f41e7f78a8fa073d0b1f1b5&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=" + num;
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    URL url = new URL(path);
                    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 json = streamTostring(inputStream, "utf-8");
                        return json;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                Bean bean = gson.fromJson(s, Bean.class);

                list.addAll(bean.getResult().getData());

                Log.i("----------------------",bean.getResult().getData().size()+"-------");
                myAdapter = new MyAdapter(bean.getResult().getData(), getActivity());
                listView.setAdapter(myAdapter);

                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                startLabels.setLastUpdatedLabel("上次刷新时间:" + simpleDateFormat.format(date));

            }
        };
        asyncTask.execute();
    }

    private String streamTostring(InputStream inputStream, String s) {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, s);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuilder builder = new StringBuilder();
            String string = null;
            while ((string = bufferedReader.readLine()) != null) {
                builder.append(string);
            }
            bufferedReader.close();
            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
----------------------------------------------会发生的错误,解决方法
在styles文件里写一个Notitle
 
  
<style name="NoTitle" parent="Theme.AppCompat.Light.NoActionBar">
    
    <item name="colorPrimary">@color/colorPrimaryitem>
    <item name="colorPrimaryDark">@color/colorPrimaryDarkitem>
    <item name="colorAccent">@color/colorAccentitem>
style>
然后早配置里修改
android:theme="@style/NoTitle"
 
  
 
  

你可能感兴趣的:(侧滑,tablayout,PullToRefreshListView)