在fragment裡進行頻道管理並和tablayout相關聯
先給專案的build.gradle進行適配:
maven {url “https://jitpack.io”}
對建立的Module中的build.gradle匯入依賴:
implementation 'com.github.andyoom:draggrid:v1.1.4'
先給fragment進行佈局:
<?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”>
**<Button** android:id="@+id/to_cm" android:layout_width="40dp" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="@id/top" app:layout_constraintBottom_toBottomOf="@id/top" app:layout_constraintRight_toRightOf="parent"/> <android.support.**design.widget.TabLayout** android:id="@+id/top" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@id/to_cm" /> <android.support**.v4.view.ViewPager** android:id="@+id/pager" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/top" />
</android.support.constraint.ConstraintLayout>
Fragment:
package com.example.rikao_18.pindao;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.andy.library.ChannelActivity;
import com.andy.library.ChannelBean;
import com.example.rikao_18.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
import static com.andy.library.ChannelActivity.REQUEST_CODE;
import static com.andy.library.ChannelActivity.RESULT_CODE;
import static com.andy.library.ChannelActivity.RESULT_JSON_KEY;
public class VedioFragment extends Fragment {
//定義變數
private TabLayout tab;
private ViewPager viewpager;
private List datas;
private VedioAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.videofragment,container,false);
//獲取資源ID
tab=view.findViewById(R.id.top);
viewpager=view.findViewById(R.id.pager);
//介面卡
adapter=new VedioAdapter(getChildFragmentManager());
viewpager.setAdapter(adapter);
//tab和viewpager關聯
tab.setupWithViewPager(viewpager);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//例項化datas
datas=new ArrayList <>();
//新增頻道
datas.add(new ChannelBean("體育", true));
datas.add(new ChannelBean("關注", true));
datas.add(new ChannelBean("推薦", true));
datas.add(new ChannelBean("國際", true));
datas.add(new ChannelBean("熱點", true));
datas.add(new ChannelBean("軍事", true));
datas.add(new ChannelBean("八卦", true));
datas.add(new ChannelBean("遊戲", false));
datas.add(new ChannelBean("寵物", false));
datas.add(new ChannelBean("汽車", false));
datas.add(new ChannelBean("熱賣", false));
datas.add(new ChannelBean("外賣", false));
datas.add(new ChannelBean("地圖", false));
datas.add(new ChannelBean("視訊", false));
datas.add(new ChannelBean("圖片", false));
datas.add(new ChannelBean("音樂", false));
datas.add(new ChannelBean("家居", false));
datas.add(new ChannelBean("網路", false));
datas.add(new ChannelBean("購物", false));
datas.add(new ChannelBean("其他", false));
//將選出來是true的頻道放入到集合中
adapter.setMjihe(getSelectedResult(datas));
//按鈕的點選事件
view.findViewById(R.id.to_cm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Gson gson = new Gson();
// 將集合轉換成gson型別
String jsonArray = gson.toJson(datas);
Intent it=new Intent(getActivity(),ChannelActivity.class);
it.putExtra(RESULT_JSON_KEY,jsonArray);
startActivityForResult(it,REQUEST_CODE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==REQUEST_CODE){
String json = data.getStringExtra("json");
datas=new Gson().fromJson(json,new TypeToken<ArrayList<ChannelBean>>(){}.getType());
adapter.setMjihe(getSelectedResult(datas));
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
//將頻道是isSelect的選出來
private List<ChannelBean> getSelectedResult(List<ChannelBean> datas) {
List<ChannelBean> result=new ArrayList <>();
for (ChannelBean bean:datas) {
if (bean.isSelect()){
result.add(bean);
}
}
return result;
}
}
ViewPager的介面卡:
package com.example.rikao_18.pindao;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.andy.library.ChannelBean;
import java.util.ArrayList;
import java.util.List;
public class VedioAdapter extends FragmentPagerAdapter {
private List<ChannelBean> mjihe;
public VedioAdapter(FragmentManager fm) {
super(fm);
//例項化集合
mjihe=new ArrayList <>();
}
//更新
public void setMjihe(List <ChannelBean> mjihe) {
this.mjihe = mjihe;
notifyDataSetChanged();
}
@Override
public Fragment getItem(int i) {
//返回 簡單的Fragment
return new AFragment();
}
@Override
public int getCount() {
return mjihe.size();
}
//和tab關聯---返回集合的name到tab
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mjihe.get(position).getName();
}
}
介面卡中的getItem()返回的fragment:
package com.example.rikao_18.pindao;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint(“ValidFragment”)
public class AFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText(“頁面”);
return textView;
}
}
*!!!注意:
若是有image-loader,
要在清單檔案註冊name:android:name=".App"
此時image-loader的App要繼承AppApplication
## 清單檔案 :