PullAndTab组合横排滑动上落下提

Tab的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.heyongwu1509a20170918.MainActivity">
    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabs"
        app:tabIndicatorColor="@color/red"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/red"
        app:tabTextColor="@color/black"/>
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/vp"/>
</LinearLayout>
pull的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true"
        ptr:ptrAnimationStyle="rotate"
        ptr:ptrHeaderTextColor="#ffffff"
        ptr:ptrHeaderSubTextColor="#00ffff"
        ptr:ptrHeaderBackground="@null"
        />
</LinearLayout>



主方法


public class MainActivity extends AppCompatActivity {
    //上面的滑动布局
    private TabLayout tabLayout;

    private LayoutInflater in;
    private ViewPager vp;

    private String[] channels = {里面写横排条目};
    private String[] urls={"里面写地址可用是json也可以是数组"};
    //存放fragment的集合
    private List<ChannelFragment> nview=new ArrayList();
    //创建的fragment对象
    private ChannelFragment channelFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp= (ViewPager) findViewById(R.id.vp);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        in = LayoutInflater.from(this);
        for(int i=0;i<channels.length;i++){
            channelFragment = new ChannelFragment();
            Bundle bundle = new Bundle();
            bundle.putString("url",urls[i]);
            channelFragment.setArguments(bundle);
            nview.add(channelFragment);
            tabLayout.addTab(tabLayout.newTab().setText(channels[i]));
        }
        FragmentManager fm = getSupportFragmentManager();
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(fm, nview);
        vp.setAdapter(adapter);
        tabLayout.setupWithViewPager(vp);
        tabLayout.setTabsFromPagerAdapter(adapter);

    }

//viewpager适配器

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private List<ChannelFragment> nview;
        public MyFragmentPagerAdapter(FragmentManager fm, List nview) {
            super(fm);
            this.nview=nview;
        }

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

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

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

}

//这个是fragment的方法
 
public class ChannelFragment extends Fragment{
    private String news_url;
    private ListView lv;
    PullToRefreshListView mPullRefreshListView;
    String ss = "";
    View view;
    private List<News> newslist;
    private MyAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        news_url = (String) bundle.get("url");

    }


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




        return view;
    }
        private void initDate() {
            xiala();
    }
       private void initView() {
        mPullRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
        mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            //跟新方法
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                xiala();


            }
        });

        //设置滑动尾部的监听器
        mPullRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                shangti();
                //滑动到底部是,提示已经到底部了

            }
        });
    }
    public void xiala(){
        new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... params) {
                String s = params[0];
                String getsss = new HttpUrl().getsss(s);
                return getsss;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Bean bean = new Gson().fromJson(s, Bean.class);
                newslist = bean.getNewslist();
                adapter = new MyAdapter(newslist, getActivity());
                mPullRefreshListView.setAdapter(adapter);
                mPullRefreshListView.onRefreshComplete();
            }
        }.execute(news_url);

    }
    public void shangti(){
        new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... params) {
                String s = params[0];
                String getsss = new HttpUrl().getsss(s);
                return getsss;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Bean bean = new Gson().fromJson(s, Bean.class);
                newslist.addAll(bean.getNewslist());
                adapter.notifyDataSetChanged();
            }
        }.execute(news_url);

    }
}


适配器还有一个布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

建立一个Myapp


public class Loader extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        File file = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "UniversalImageLoader/Cache");
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(options)
                .memoryCache(new LRULimitedMemoryCache(12*1024*1024))
                .memoryCacheSize(12*1024*1024)
                .diskCache(new UnlimitedDiskCache(file))
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(configuration);
    }
}

然后就是适配器啦

public class MyAdapter extends BaseAdapter {
    private List<News> list;
    private Context context;

    public MyAdapter(List<News> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

    @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) {
        DisplayImageOptions option = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                 
                .imageScaleType(ImageScaleType.EXACTLY)
                .build();
       convertView = View.inflate(context,R.layout.item2,null);
        TextView tv = (TextView) convertView.findViewById(R.id.tv);
        ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
        tv.setText(list.get(position).getCtime()+list.get(position).getDescription()+list.get(position).getCtime());
        ImageLoader.getInstance().displayImage(list.get(position).getPicUrl(),iv,option);
        return convertView;
    }
}




异步的httpurl方法

public class HttpUrl {
    public String getsss(String ss){
        URL url = null;
        String hh = "";
        HttpURLConnection httpURLConnection = null;
        try {
            url = new URL(ss);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == 200){
                InputStream inputStream = httpURLConnection.getInputStream();
                byte [] b =new byte[1024];
                int len = 0;
                while((len = inputStream.read(b))!=-1){
                    hh += new String(b,0,len);
                }
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hh;
    }

}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值