1. 程式人生 > >Fragment頻道管理

Fragment頻道管理

1.fragmnet頁面

public class showFone extends Fragment {

private TabLayout tabLayout;
private ViewPager pager;
private ImageButton button;
private List<ChannelBean> list;
private ShowBase showBase;
private String jsonStr = "";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.showfone,container,false);
    tabLayout = v.findViewById(R.id.tab);
    pager = v.findViewById(R.id.pagera);
    button = v.findViewById(R.id.imag);
    return v;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
   //例項化介面卡
    showBase = new ShowBase(getChildFragmentManager());
    pager.setAdapter(showBase);
   list = new ArrayList<>();
   list.add(new ChannelBean("關注",true));
   list.add(new ChannelBean("推薦",true));
   list.add(new ChannelBean("熱點",true));
   list.add(new ChannelBean("軍事",true));
   list.add(new ChannelBean("其他",true));
   list.add(new ChannelBean("我的",true));
   list.add(new ChannelBean("條目1",false));
   list.add(new ChannelBean("條目2",false));
   list.add(new ChannelBean("條目3",false));
   list.add(new ChannelBean("條目4",false));
   list.add(new ChannelBean("條目5",false));
   list.add(new ChannelBean("條目6",false));
    //向介面卡傳選中的值
    showBase.setData(isSelectPD(list));
    tabLayout.setupWithViewPager(pager);
    //點選按鈕進行跳轉
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //ChannelActivity.startChannelActivity(MainActivity.this,list);
            Gson gson = new Gson();
            String jsonArray = gson.toJson(list);
            Intent intent = new Intent(getActivity(), ChannelActivity.class);
            intent.putExtra(RESULT_JSON_KEY, jsonArray);
            startActivityForResult(intent, REQUEST_CODE);
        }
    });
}

public List<ChannelBean> isSelectPD(List<ChannelBean> list){
    List<ChannelBean> isok = new ArrayList<>();
    for (int i = 0;i<list.size();i++){
        if(list.get(i).isSelect()){
            isok.add(list.get(i));
        }
    }
    return isok;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE){
        //獲取tab要展示的內容
        jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
        //清空之前的欄目
        tabLayout.removeAllTabs();
        Type type=new TypeToken<ArrayList<ChannelBean>>(){}.getType();
        list = new Gson().fromJson(jsonStr,type);
        //傳給介面卡
        showBase.setData(isSelectPD(list));
        tabLayout.setupWithViewPager(pager);
    }
}
}

2.XML佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.TabLayout
    android:id="@+id/tab"
    android:layout_width="0dp"
    android:layout_height="50dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/imag"
    app:layout_constraintTop_toTopOf="parent"
    ></android.support.design.widget.TabLayout>

<ImageButton
    android:id="@+id/imag"
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toRightOf="@id/tab"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="@id/tab"
    android:background="@drawable/jia"
    ></ImageButton>
<android.support.v4.view.ViewPager
    android:id="@+id/pagera"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/tab"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    ></android.support.v4.view.ViewPager>

</android.support.constraint.ConstraintLayout>

3.介面卡

public class ShowBase extends FragmentPagerAdapter {
    List<ChannelBean> list = new ArrayList<>();

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

public void setData(List<ChannelBean> list) {
    this.list = list;
    notifyDataSetChanged();
}

@Override
public Fragment getItem(int i) {
    switch (i){
        case 0:
            return new Sone();
        default:
            return new Stwo();
    }
}

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

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

在當前專案的build.gradle匯入依賴

implementation 'com.github.andyoom:draggrid:v1.0.1'

在最大的build.gradle匯入

allprojects {
   repositories {
       google()
       jcenter()
       //匯入這句話
       maven {url "https://jitpack.io"}
   }
}

許可權

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//重力許可權
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>