You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.5 KiB

package com.example.dxsdpdb;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
public class MissionFragment extends Fragment implements ViewPager.OnPageChangeListener {
private View view=null; // 碎片的布局实例
private ViewPager viewPager; //内导航的碎片的容器
private RadioGroup rgChannel=null; // 内导航由单选按钮组构成
private HorizontalScrollView hvChannel=null; //单选按钮组可滚动动
private String[] channelList = {"全部任务","待付款","待接受","待完成","已完成","已过期"}; //默认的内导航栏目
private MissionFragmentAdapter adapter; //viewPager 的适配器
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public MissionFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(view==null){
view=inflater.inflate(R.layout.fragment_mission, container, false);
viewPager=(ViewPager)view.findViewById(R.id.vpNewsList);
initViewPager(); //设置 ViewPager
rgChannel=(RadioGroup)view.findViewById(R.id.rgChannel);
hvChannel=(HorizontalScrollView)view.findViewById(R.id.hvChannel);
initTab(inflater); //初始化内导航标签
rgChannel.setOnCheckedChangeListener( //单选按钮的监听事件响应
new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group,int checkedId) {
//单击某个选项按钮时 viewPager 进行切换
viewPager.setCurrentItem(checkedId);
}
});
}
return view;
}
private void initTab(LayoutInflater inflater){
for(int i=0;i<channelList.length;i++){//以下添加单选按钮的实例到内导航
RadioButton rb=(RadioButton)inflater.inflate(R.layout.tab_rb, null);
rb.setId(i);
rb.setText(channelList[i]);
RadioGroup.LayoutParams params=new
RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
rgChannel.addView(rb,params);
}
rgChannel.check(0); //第一个选项
}
//设置 ViewPager 容器的适配器。
private void initViewPager(){
FragmentManager fragmentManager = super.getActivity().getSupportFragmentManager();
//参数 channelList 将被适配器用来在动态切换碎片的时候实时创建碎片
adapter=new MissionFragmentAdapter(fragmentManager, channelList);
//设置 ViewPager 的适配器
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(2);
//设置显示第 1 个碎片
viewPager.setCurrentItem(0);
//设置 ViewPager 的切换监听
viewPager.addOnPageChangeListener(this);
}
//以下三个方法是 ViewPager 的切换监听器 ViewPager.OnPageChangeListener 的事件处理方法
//ViewPager.OnPageChangeListener 的实现采用了接口的方式
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
//某个碎片被选中时的动作,对应的内导航标签将要滑动到屏幕中央,并且有下划线显示
public void onPageSelected(int i) {
setTab(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
private void setTab(int idx){
RadioButton rb=(RadioButton)rgChannel.getChildAt(idx);
rb.setChecked(true);
int left=rb.getLeft();
int width=rb.getMeasuredWidth();
DisplayMetrics metrics=new DisplayMetrics();
super.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth=metrics.widthPixels;
int len=left+width/2-screenWidth/2;
hvChannel.smoothScrollTo(len, 0);
}
}