TabLayout+Fragment

package test.bwei.com.zhangjian20170819lianxi.Activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

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

import test.bwei.com.zhangjian20170819lianxi.Bean.MyPath;
import test.bwei.com.zhangjian20170819lianxi.Fragment.MyFragment;
import test.bwei.com.zhangjian20170819lianxi.R;

public class MainActivity extends FragmentActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<MyPath> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //加载资源id
        tabLayout= (TabLayout) findViewById(R.id.tb);
        viewPager= (ViewPager) findViewById(R.id.vp);
        //适配数据
        list=new ArrayList<>();
        list.add(new MyPath("推荐","http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page=1"));
        list.add(new MyPath("野史","http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page=1"));
        list.add(new MyPath("后宫","http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page=1"));
        list.add(new MyPath("解密","http://www.yulin520.com/a2a/forum/allTypeList?sign=376C5BFC22179A1B8FF3A86D4588B29F&pageSize=10&ts=1877785007&forumType=0&page=1"));

        //加载布局模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        for (int i=0;i<list.size();i++){
            tabLayout.addTab(tabLayout.newTab().setText(list.get(i).getTitle()));
        }
        //关联
        MyAdapter adapter=new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(adapter);

    }

    class MyAdapter extends FragmentPagerAdapter{

        @Override
        public CharSequence getPageTitle(int position) {
            return list.get(position).getTitle();
        }

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return MyFragment.newInstanc(list.get(position).getTitle(),list.get(position).getPath());
    }

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

}

Fragment

package test.bwei.com.zhangjian20170819lianxi.Fragment;

import android.os.Bundle;
import android.os.Handler;
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.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwei.imageloaderlibrary.ImageLoaderUtils;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import test.bwei.com.xlistlibrary.XListView;
import test.bwei.com.zhangjian20170819lianxi.Bean.MyBean;
import test.bwei.com.zhangjian20170819lianxi.Bean.MySqlite;
import test.bwei.com.zhangjian20170819lianxi.Dao.MyDao;
import test.bwei.com.zhangjian20170819lianxi.R;




public class MyFragment extends Fragment {

    private String path;
    private XListView listView;
    private MyDao dao;
    private List<MySqlite> list;
    private Handler handler=new Handler();
    private MyAdapter adapter;
    private int page=1;
    private List<MyBean> list2;

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

        listView= (XListView) view.findViewById(R.id.xlv);

        //调用数据库
        dao = new MyDao(getActivity());

        //调用方法
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page=1;
                getTime();
                getData();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       listView.stopRefresh();
                    }
                },2000);
            }

            @Override
            public void onLoadMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        getData();
                        listView.stopLoadMore();
                    }
                },2000);
            }
        });

        getData();

        return view;
    }

    public static Fragment newInstanc(String title,String path){
        MyFragment fragment=new MyFragment();
        Bundle bundle=new Bundle();
        bundle.putString("title",title);
        bundle.putString("path",path);
        fragment.setArguments(bundle);
        return fragment;
    }

    //得到时间
    public void getTime(){
        long millis = System.currentTimeMillis();
        Date date=new Date(millis);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = format.format(date);
        listView.setRefreshTime(format1);

    }

    //请求网络数据
    public void getData(){
        //得到网路传输的接口地址
        path = getArguments().getString("path");
        AsyncHttpClient client=new AsyncHttpClient();
        client.get(getActivity(), path, new TextHttpResponseHandler() {




            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Gson gson = new Gson();
                MyBean bean = gson.fromJson(responseString, MyBean.class);
                list = dao.sel();
                if (list.size()==0){
                    for (int i=0;i<bean.getData().size();i++){
                        dao.ins(bean.getData().get(i));
                    }

                    list=dao.sel();
                }

                init();
            }
        }) ;
    }

    //定义适配器
    public void init(){
        if (adapter==null){
            adapter=new MyAdapter();
            listView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list!=null?list.size():0;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null){
                convertView = View.inflate(getActivity(), R.layout.item, null);
                holder=new ViewHolder();
                holder.textView= (TextView) convertView.findViewById(R.id.ti);
                holder.textView2= (TextView) convertView.findViewById(R.id.phone);
                holder.textView3= (TextView) convertView.findViewById(R.id.yulin);
                holder.imageView= (ImageView) convertView.findViewById(R.id.headImg);
                convertView.setTag(holder);
            }else {
                holder= (ViewHolder) convertView.getTag();
            }

            holder.textView.setText(list.get(position).getTitle());
            holder.textView2.setText(list.get(position).getPhone());
            holder.textView3.setText(list.get(position).getYulin());
            DisplayImageOptions options = ImageLoaderUtils.getOptions();
            ImageLoader.getInstance().displayImage(list.get(position).getHeadImg(),holder.imageView,options);

            return convertView;
        }
    }

    class ViewHolder{
        TextView textView,textView2,textView3;
        ImageView imageView;
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值