TabLayout+异步+viewpager+GET解析

=================================封装基类================================

public abstract class Fenzhuang extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        LinearLayout layout = (LinearLayout) findViewById(R.id.fen);
        View view = View.inflate(this, getLayout(), null);
        layout.addView(view);
        inData();
    }

    public abstract void inData();

    public abstract int getLayout();
}

==================
public class MainActivity extends Fenzhuang {

    private TabLayout tabLayout;
    private ViewPager pager;
    protected List<Fragment> list = new ArrayList<>();
    private TextView di;
    private String[] mTitle = {"电影", "全部", "条目三", "条目四", "条目五", "条目六"};


    @Override
    public void inData() {
        tabLayout = (TabLayout) findViewById(R.id.tab);
        pager = (ViewPager) findViewById(R.id.view);

        for (int a = 0; a < mTitle.length; a++) {
            list.add(new Ment1());
        }
        MypagerAdapter mypagerAdapter = new MypagerAdapter(getSupportFragmentManager());
        pager.setAdapter(mypagerAdapter);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setupWithViewPager(pager);

    }

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    private class MypagerAdapter extends FragmentPagerAdapter {
        public MypagerAdapter(FragmentManager fm) {
            super(fm);
        }

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

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

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }
    }
}
===================================Helper========================================

public class Hepler {

    public Hepler() {

    }

    public Hepler get(String url) {
       MyAsyncTask myAsyncTask = new MyAsyncTask(url);
        myAsyncTask.execute();
        return this;
    }


    private class MyAsyncTask extends AsyncTask<String, Integer, String> {
        private String url, method, string;

        public MyAsyncTask(String url) {
            this.url = url;
        }

        @Override
        protected String doInBackground(String... strings) {
            String a = "0";
            try {
                URL url1 = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
                connection.setRequestMethod("GET");
                connection.setReadTimeout(5000);
                connection.connect();

                int i = connection.getResponseCode();
                if (i == HttpURLConnection.HTTP_OK) {
                    InputStream is = connection.getInputStream();
                    String data = convert2sString(is);
                    a = data;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return a;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if ("0".equals(s)) {

            } else {
                linstener.success(s);
            }
        }
    }


    private HttpListener linstener;

    public void result(HttpListener linstener) {
        this.linstener = linstener;
    }

    public interface HttpListener {
        void success(String data);
    }

    private String convert2sString(InputStream is) throws IOException {
        ByteArrayOutputStream ia = new ByteArrayOutputStream();
        int len = -1;
        byte[] buffer = new byte[512];
        while ((len = is.read(buffer)) != -1) {
            ia.write(buffer, 0, len);
        }
        return new String(ia.toByteArray());
    }
}

=====================================viewpager===========================================

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager pager;
    protected List<Fragment> list = new ArrayList<>();
    private TextView di;
    private String[] mTitle = {"电影", "全部", "条目三", "条目四", "条目五", "条目六"};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = (TabLayout) findViewById(R.id.tab);
        pager = (ViewPager) findViewById(R.id.view);
        
        for (int a = 0; a < mTitle.length; a++) {
            list.add(new Ment1());
        }
        MypagerAdapter mypagerAdapter = new MypagerAdapter(getSupportFragmentManager());
        pager.setAdapter(mypagerAdapter);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setupWithViewPager(pager);

    }
    private class MypagerAdapter extends FragmentPagerAdapter {
        public MypagerAdapter(FragmentManager fm) {
            super(fm);
        }

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

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

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }
    }

}

==============================================Frament===============================================public class Ment1 extends Fragment implements View.OnClickListener {
   
    private XListView listView;
    private String dataUrl = "http://172.17.8.100/movieApi/movie/v1/findHotMovieList?page=1&count=10";
    private String dataUrl1 = "http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?page=1&count=10";

    private NewsAdatpter newsAdatpter;
    private TextView zhu;
    private TextView ye;
    private TextView web;
    private List<News.ResultBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.men1, null, false);
        listView = (XListView) view.findViewById(R.id.xlist);

        ye = (TextView) view.findViewById(R.id.sang);
        web = (TextView) view.findViewById(R.id.ji);
        ye.setOnClickListener(this);
        web.setOnClickListener(this);

        newsAdatpter = new NewsAdatpter(getActivity());
        listView.setAdapter(newsAdatpter);

        listView.setPullLoadEnable(true);
        listView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                //下拉刷新
                doHttp(dataUrl);
            }

            @Override
            public void onLoadMore() {
                //上拉加载
                listView.stopLoadMore();
            }
        });

        return view;
    }

    private void doHttp(String dataUrl ) {
        new Hepler().get(dataUrl).result(new Hepler.HttpListener() {
            @Override
            public void success(String data) {
                News bean = new Gson().fromJson(data, News.class);
                list = bean.getResult();
                newsAdatpter.setList(list);
                listView.stopRefresh();
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sang:
                list.clear();
                doHttp(dataUrl1);
                break;
            case R.id.ji:
                list.clear();
                doHttp(dataUrl);
                break;
        }

    }
}

=================================  适配器==================================

public class NewsAdatpter extends BaseAdapter {

   private Context context;

    public NewsAdatpter(Context context) {
        this.context = context;
    }
   
    private List<News.ResultBean> list = new ArrayList<>();

    public void setList(List<News.ResultBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder1 viewHolder1;
        if (view == null) {
            view = View.inflate(context, R.layout.lay, null);
            viewHolder1 = new ViewHolder1();
            viewHolder1.img = (ImageView) view.findViewById(R.id.news_pic);
            viewHolder1.men1 = (TextView) view.findViewById(R.id.news_desc);
            viewHolder1.men2 = (TextView) view.findViewById(R.id.news_title);
            view.setTag(viewHolder1);
        } else {
            viewHolder1 = (ViewHolder1) view.getTag();
        }

        News.ResultBean bean = list.get(i);
        viewHolder1.men1.setText(bean.getName());
        viewHolder1.men2.setText(bean.getSummary());
        ImageLoader.getInstance().displayImage(bean.getImageUrl(), viewHolder1.img);

        return view;
    }
   class ViewHolder1 {
        ImageView img;
        TextView men1;
        TextView men2;
    }
}

===================================布局==============================

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">



    <android.support.v4.view.ViewPager
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="450dp"
        android:layout_weight="1"></android.support.v4.view.ViewPager>


    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:tabIndicatorColor="#d43c3c"
        app:tabSelectedTextColor="#d43c3c" />
</LinearLayout>

=========men1

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="首页" />

        <TextView
            android:id="@+id/sang"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="正在上映" />

        <TextView
            android:id="@+id/ji"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="即将上映" />


    </LinearLayout>

    <com.example.xlistviewlib.XListView
        android:id="@+id/xlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </com.example.xlistviewlib.XListView>

</LinearLayout>

=========lay

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

    <ImageView
        android:id="@+id/news_pic"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginRight="10dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/news_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/news_pic"
        android:ellipsize="end"
        android:lines="1"
        android:text="我是Title" />

    <TextView
        android:id="@+id/news_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/news_title"
        android:layout_toRightOf="@+id/news_pic"
        android:ellipsize="end"
        android:lines="3"
        android:text="我是Desc" />
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/qq_43131935/article/details/82744538