布局 1.= 主布局 侧拉框 外加自定义viewpager
DrawerLayout
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/draw"
tools:openDrawer="start"
tools:context=".LoginActivity02">
<!--自定义viewpager-->
<com.example.yuekaolianxi.NoScrollViewPager
android:id="@+id/Login_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:id="@+id/Login_linear"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="页面一" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"
/>
<TextView
android:id="@+id/textView_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="页面二" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"
/>
</LinearLayout>
自定义viewpager
com.example.yuekaolianxi.NoScrollViewPager
创建
NoScrollViewPager 类 extends 继承 ViewPager
public class NoScrollViewPager extends ViewPager {
/**是否禁止左右滑动,true为禁止,false为不禁止*/
private boolean noScroll = true;
public NoScrollViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
//huizhikuangao
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public NoScrollViewPager(Context context){
super(context);
}
public void setNoScroll(boolean noScroll) {
this.noScroll = noScroll;
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
}
//是否拦截
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (noScroll){
return false;
}else {
return super.onInterceptTouchEvent(ev);
}
}
@Override
public void setCurrentItem(int item, boolean smoothScroll) {
super.setCurrentItem(item, smoothScroll);
}
@Override
public void setCurrentItem(int item) {
super.setCurrentItem(item);
}
}
侧拉点击事件 进行页面的跳转
public class LoginActivity02 extends AppCompatActivity implements View.OnClickListener{
private DrawerLayout drawerLayout;
private LinearLayout linearLayout;
private ViewPager viewPager;
//单击事件
private TextView one;
private TextView two;
private List<Fragment> list ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login02);
//初始化页面
initview();
}
private void initview() {
drawerLayout = findViewById(R.id.draw);
linearLayout = findViewById(R.id.Login_linear);
viewPager = findViewById(R.id.Login_view);
one = findViewById(R.id.textView_one);
two = findViewById(R.id.textView_two);
//确定单击
one.setOnClickListener(this);
two.setOnClickListener(this);
list = new ArrayList<>();
//添加布局
list.add(new Fragment_One());
list.add(new Fragment_Two());
//设置适配器
FragmentLoginAdapter adapter = new FragmentLoginAdapter(getSupportFragmentManager(),list);
viewPager.setAdapter(adapter);
}
//单击的方法
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.textView_one:
//关闭侧划栏
drawerLayout.closeDrawers();
//viewFragment出现根据下标
viewPager.setCurrentItem(0);
// Intent one = new Intent(LoginActivity02.this,One.class);
// startActivity(one);
break;
case R.id.textView_two:
//关闭侧划栏
drawerLayout.closeDrawers();
//viewFragment出现根据下标
viewPager.setCurrentItem(1);
// Intent two = new Intent(LoginActivity02.this,Two.class);
// startActivity(two);
break;
default:
break;
}
}
}
页面1.多条目加载
布局
<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/fragment_one_text"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center"
android:textSize="14sp"
android:text="页面一"
/>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/fragment_one_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
页面操作
public class Fragment_One extends Fragment {
private PullToRefreshListView listView;
private String url = "https://www.apiopen.top/satinCommentApi?id=27610708&page=";
private int page = 1;
private HttpUtils httpUtils;
private Handler handler = new Handler();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one,container,false);
//获取Id
initSetView(view);
getData();
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
//获取数据
//
super.onActivityCreated(savedInstanceState);
}
//获取控件ID
private void initSetView(View view) {
listView = view.findViewById(R.id.fragment_one_list);
//设置头部底部的视图
//2.设置模式 BOTH 可以上拉 下拉 pull_from_end代表上拉,pull_from_start代表下拉
listView.setMode(PullToRefreshListView.Mode.BOTH);
final ILoadingLayout start = listView.getLoadingLayoutProxy(true, false);
start.setPullLabel("下拉刷新");
start.setRefreshingLabel("正在刷新");
start.setReleaseLabel("放开刷新");
ILoadingLayout end = listView.getLoadingLayoutProxy(false, true);
end.setPullLabel("上拉加载");
end.setRefreshingLabel("正在加载");
end.setReleaseLabel("放开加载");
listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page=1;
getData();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//必须在异步中完成 在列表没有数据的时候,不再请求接口,调用当前方法
listView.onRefreshComplete();
}
},2000);
//last最近的,最后一次update修改/更新
start.setLastUpdatedLabel("上次更新时间"+new SimpleDateFormat("HH:mm").format(new Date(System.currentTimeMillis())));
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
page++;
getData();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//必须在异步中完成 在列表没有数据的时候,不再请求接口,调用当前方法
listView.onRefreshComplete();
}
},2000);
}
});
}
//获取数据
public void getData() {
String path = url+page;
HttpUtils.getdata(path, getActivity(), new JsonBack() {
@Override
public void getjsondata(String json) {
Gson gson = new Gson();
PageOneBeans pageOneBeans = gson.fromJson(json, PageOneBeans.class);
Log.i("TAG", json+"");
List<PageOneBeans.DataBean.NormalBean.ListBeanX> list = pageOneBeans.getData().getNormal().getList();
Log.i("TAG", list.size()+"");
Log.i("TAG", list.toString());
Fragment_One_Adapter adapter = new Fragment_One_Adapter(list,getActivity());
listView.setAdapter(adapter);
}
});
}
}
页面2.GridView
<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/fragment_one_text"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center"
android:textSize="14sp"
android:text="页面二"
/>
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pull_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:layout_margin="5dp"
android:numColumns="2"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF"
></com.handmark.pulltorefresh.library.PullToRefreshGridView>
</LinearLayout>
页面2中的gridview
<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">
<ImageView
android:id="@+id/grid_image"
android:layout_width="145dp"
android:layout_height="193dp"
android:scaleType="fitXY"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
页面2操作
public class Fragment_Two extends Fragment {
private PullToRefreshGridView pullToRefreshGridView;
private String url1= "https://www.apiopen.top/meituApi?page=";
private int page = 1;
private Handler handler = new Handler();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two,container,false);
//初始化界面
initViews(view);
getData();
return view;
}
//初始化界面
private void initViews(View view) {
//获得组件
pullToRefreshGridView = view.findViewById(R.id.pull_gridview);
//设置模式
pullToRefreshGridView.setMode(PullToRefreshGridView.Mode.BOTH);
//上拉刷新
final ILoadingLayout start = pullToRefreshGridView.getLoadingLayoutProxy(true, false);
start.setPullLabel("上拉刷新");
start.setRefreshingLabel("正在刷新");
start.setReleaseLabel("放开刷新");
ILoadingLayout end = pullToRefreshGridView.getLoadingLayoutProxy(false, true);
end.setPullLabel("上拉加载");
end.setRefreshingLabel("正在加载");
end.setReleaseLabel("放开加载");
pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
@Override
public void onPullDownToRefresh(final PullToRefreshBase<GridView> pullToRefreshBase) {
page = 1 ;
getData();
handler.postDelayed(new Runnable() {
@Override
public void run() {
pullToRefreshGridView.onRefreshComplete();
}
},2000);
start.setLastUpdatedLabel("上次更新时间"+new SimpleDateFormat("HH:mm").format(new Date(System.currentTimeMillis())));
}
@Override
public void onPullUpToRefresh(final PullToRefreshBase<GridView> pullToRefreshBase) {
page ++ ;
getData();
handler.postDelayed(new Runnable() {
@Override
public void run() {
pullToRefreshGridView.onRefreshComplete();
}
},2000);
}
});
}
public void getData() {
HttpUtils.getdata(url1 + page, getActivity(), new JsonBack() {
@Override
public void getjsondata(String json) {
Gson gson = new Gson();
Fragment_Two_GridBeans fragment_two_gridBeans = gson.fromJson(json, Fragment_Two_GridBeans.class);
List<Fragment_Two_GridBeans.DataBean> list = fragment_two_gridBeans.getData();
Fragment_Two_Adapter adapter = new Fragment_Two_Adapter(list,getActivity());
pullToRefreshGridView.setAdapter(adapter);
}
});
}
}