Fragment嵌套,TabLatout,PullToRefreshListView

1.侧拉布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:fitsSystemWindows=“true”
tools:openDrawer=“start”>

<include
    layout="@layout/app_bar_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_detail"
    app:menu="@menu/activity_detail_drawer" />

</android.support.v4.widget.DrawerLayout>
2.app_bar布局
在这里插入图片描述
在这里插入图片描述
3.MainActivity
package com.bwei.week3_01;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;

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

import adapter.MyFragmentAdapter;
import fragment.AddFragment;
import fragment.MovieFragment;
import fragment.SetFragment;
import fragment.VipFragment;

public class DetailActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

private ViewPager view_page;
private RadioGroup radio_group;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    //初始化页面
    initViews();


}

private void initViews() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    view_page = findViewById(R.id.view_pager);
    radio_group = findViewById(R.id.radio_group);
    List<Fragment> fg=new ArrayList<>();
    fg.add(new MovieFragment());
    fg.add(new AddFragment());
    fg.add(new VipFragment());
    fg.add(new SetFragment());
    //创建适配器
    MyFragmentAdapter fragmentAdapter=new  MyFragmentAdapter(getSupportFragmentManager(),fg);
    view_page.setAdapter(fragmentAdapter);
    radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.rb1:
                view_page.setCurrentItem(0,false);
                break;
                case R.id.rb2:
                    view_page.setCurrentItem(1,false);
                    break;
                case R.id.rb3:
                    view_page.setCurrentItem(2,false);
                    break;
                case R.id.rb4:
                    view_page.setCurrentItem(3,false);
                    break;
            }
        }
    });
}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}
4.适配器
package adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**

  • Created by Administrator on 2018/11/15 0015.
    */

public class MyFragmentAdapter extends FragmentPagerAdapter{
private List list;
private String[] titles=new String[]{“热门影片”,“正在上映”,“即将上映”};
public MyFragmentAdapter(FragmentManager fm, List list) {
super(fm);
this.list = list;
}

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

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

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

}
5.首页fragment
package fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bwei.week3_01.R;

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

import adapter.MyFragmentAdapter;
import fragment.fragments2.HotFragment;
import fragment.fragments2.IngFragment;
import fragment.fragments2.PreFragment;

/**

  • Created by Administrator on 2018/11/15 0015.
    */

public class MovieFragment extends BaseFragment {

private View view;

private ViewPager move_pager;
private TabLayout tab_layout;

@Override
protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.movie_layout,null);
    initView();
    List<Fragment> fg=new ArrayList<>();
    fg.add(new HotFragment());
    fg.add(new IngFragment());
    fg.add(new PreFragment());

    //创建适配器
    MyFragmentAdapter fragmentAdapter=new  MyFragmentAdapter(getChildFragmentManager(),fg);
    move_pager.setAdapter(fragmentAdapter);
    tab_layout.setupWithViewPager(move_pager);
    return view;

}

private void initView() {
    tab_layout = view.findViewById(R.id.tab_layout);
    move_pager = view.findViewById(R.id.move_pager);
}

@Override
protected void initDatas() {

}

}

6.基类
package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**

  • Created by Administrator on 2018/11/15 0015.
    */

public abstract class BaseFragment extends Fragment{
public String TAG=this.getClass().getSimpleName();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return createView(inflater, container, savedInstanceState);
}

protected abstract View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser){
        initDatas();
    }
}

protected abstract void initDatas();

}
6.小标题
package fragment.fragments2;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.bwei.week3_01.R;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

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

import adapter.MyAdapter;
import app.MyApp;
import bean.NewsBean;
import fragment.BaseFragment;
import http.HttpCofig;
import utils.HttpUtils;

/**

  • Created by Administrator on 2018/11/15 0015.
    */

public class HotFragment extends BaseFragment {

private PullToRefreshListView listView;
private int page=0;
private int type=0;
private MyAdapter adapter;
private List<NewsBean.ResultBean.DataBean> list=new ArrayList<>();
@Override
protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.hot_layout,null);
    listView = view.findViewById(R.id.pullto_listview);
    listView.setMode(PullToRefreshBase.Mode.BOTH);
    listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            page=1;
            type=1;
            initDatas();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            page++;
            type=2;
            initDatas();
        }
    });
    return view;

}

@Override
protected void initDatas() {
    HttpUtils httpUtils=HttpUtils.getHttpUtils();
    httpUtils.get(HttpCofig.new_url+"&page="+page);
    httpUtils.setOnHttpLoadListener(new HttpUtils.HttpLoadListener() {




        @Override
        public void loadSuccess(String json) {
            Log.d(TAG, "loadSuccess: 数据+++++++"+json);
            Gson gson=new Gson();
            NewsBean newsBean = gson.fromJson(json, NewsBean.class);

            switch (type){
                case 0:
                    list = newsBean.getResult().getData();
                    adapter = new MyAdapter(MyApp.getContext(), list);
                    listView.setAdapter(adapter);
                    break;
                case 1:
                    adapter = new MyAdapter(MyApp.getContext(), list);
                    listView.setAdapter(adapter);
                    listView.onRefreshComplete();
                    break;
                case 2:
                    List<NewsBean.ResultBean.DataBean> list1 = newsBean.getResult().getData();
                    list.addAll(list1);
                    adapter.notifyDataSetChanged();
                    listView.onRefreshComplete();
                    break;

            }
            //创建适配器

        }

        @Override
        public void loadError(String error) {

        }
    });
}

}
7.多条目加载
8.刷新布局

<?xml version="1.0" encoding="utf-8"?>


<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pullto_listview"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
/>

9.小标题布局 <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="40dp"></android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
android:id="@+id/move_pager"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”></android.support.v4.view.ViewPager>

11.
package adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**

  • Created by Administrator on 2018/11/15 0015.0
    */

public class MyFragmentAdapter extends FragmentPagerAdapter{
private List list;
private String[] titles=new String[]{“热门影片”,“正在上映”,“即将上映”};
public MyFragmentAdapter(FragmentManager fm, List list) {
super(fm);
this.list = list;
}

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

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

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值