public class Myadapter extends FragmentPagerAdapter{
private List<Fragment> fragmentList;
private List<String> list;
public Myadapter(FragmentManager fm, List<Fragment> fragmentList,List<String> list) {
super(fm);
this.fragmentList = fragmentList;
this.list = list;
}
public Myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
private TabLayout tablay;
private ViewPager view_pager;
private SharedPreferences sp;
private List<String> list;
private List<Fragment> fragmentlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tablay = (TabLayout) findViewById(R.id.tablay);
view_pager = (ViewPager) findViewById(R.id.view_pager);
ImageView te_img = (ImageView) findViewById(R.id.te_img);
sp = getSharedPreferences("name", MODE_PRIVATE);
//view_pager.setAdapter(new Myadapter(getSupportFragmentManager()));
te_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(sp.getString("pdlist",null)!=null)
{
String pdlist = sp.getString("pdlist", null);
ChannelActivity.startChannelActivity(MainActivity.this,pdlist);
}else{
List<ChannelBean> pinlist = new ArrayList<>();
pinlist.add(new ChannelBean("头条",true));
pinlist.add(new ChannelBean("社会",true));
pinlist.add(new ChannelBean("国内",true));
pinlist.add(new ChannelBean("国际",true));
pinlist.add(new ChannelBean("娱乐",true));
ChannelActivity.startChannelActivity(MainActivity.this,pinlist);
}
}
});
}
@Override
protected void onResume() {
super.onResume();
if(sp.getString("pdlist",null)!=null)
{
try {
List<String>list = new ArrayList<>();
List<Fragment> fragmentlist = new ArrayList<>();
String pdlist = sp.getString("pdlist", null);
List<ChannelBean> pinlist = new ArrayList<>();
JSONArray json = new JSONArray(pdlist);
for (int i = 0; i <json.length(); i++) {
JSONObject obj = (JSONObject) json.get(i);
ChannelBean chl = new ChannelBean(obj.getString("name"),obj.getBoolean("isSelect"));
pinlist.add(chl);
}
for (int i = 0; i <pinlist.size() ; i++) {
if(pinlist.get(i).isSelect())
{
list.add(pinlist.get(i).getName());
if(list.get(i).equals("头条"))
{
fragmentlist.add(new fragment1());
}else if(list.get(i).equals("社会"))
{
fragmentlist.add(new fragment2());
}else if(list.get(i).equals("国内"))
{
fragmentlist.add(new fragment3());
}else if(list.get(i).equals("国际"))
{
fragmentlist.add(new fragment4());
}else if(list.get(i).equals("娱乐"))
{
fragmentlist.add(new fragment5());
}
}
}
Myadapter myadapter = new Myadapter(getSupportFragmentManager(),fragmentlist,list);
view_pager.setAdapter(myadapter);
tablay.setupWithViewPager(view_pager);
} catch (JSONException e) {
e.printStackTrace();
}
}else{
list = new ArrayList<>();
fragmentlist = new ArrayList<>();
list.add("头条");
list.add("社会");
list.add("国内");
list.add("国际");
list.add("娱乐");
fragmentlist.add(new fragment1());
fragmentlist.add(new fragment2());
fragmentlist.add(new fragment3());
fragmentlist.add(new fragment4());
fragmentlist.add(new fragment5());
Myadapter myadapter = new Myadapter(getSupportFragmentManager(),fragmentlist,list);
view_pager.setAdapter(myadapter);
tablay.setupWithViewPager(view_pager);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==101){
String pdlist=data.getStringExtra("json");
sp.edit().putString("pdlist",pdlist).commit();
}
}
private List<Fragment> fragmentList;
private List<String> list;
public Myadapter(FragmentManager fm, List<Fragment> fragmentList,List<String> list) {
super(fm);
this.fragmentList = fragmentList;
this.list = list;
}
public Myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
private TabLayout tablay;
private ViewPager view_pager;
private SharedPreferences sp;
private List<String> list;
private List<Fragment> fragmentlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tablay = (TabLayout) findViewById(R.id.tablay);
view_pager = (ViewPager) findViewById(R.id.view_pager);
ImageView te_img = (ImageView) findViewById(R.id.te_img);
sp = getSharedPreferences("name", MODE_PRIVATE);
//view_pager.setAdapter(new Myadapter(getSupportFragmentManager()));
te_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(sp.getString("pdlist",null)!=null)
{
String pdlist = sp.getString("pdlist", null);
ChannelActivity.startChannelActivity(MainActivity.this,pdlist);
}else{
List<ChannelBean> pinlist = new ArrayList<>();
pinlist.add(new ChannelBean("头条",true));
pinlist.add(new ChannelBean("社会",true));
pinlist.add(new ChannelBean("国内",true));
pinlist.add(new ChannelBean("国际",true));
pinlist.add(new ChannelBean("娱乐",true));
ChannelActivity.startChannelActivity(MainActivity.this,pinlist);
}
}
});
}
@Override
protected void onResume() {
super.onResume();
if(sp.getString("pdlist",null)!=null)
{
try {
List<String>list = new ArrayList<>();
List<Fragment> fragmentlist = new ArrayList<>();
String pdlist = sp.getString("pdlist", null);
List<ChannelBean> pinlist = new ArrayList<>();
JSONArray json = new JSONArray(pdlist);
for (int i = 0; i <json.length(); i++) {
JSONObject obj = (JSONObject) json.get(i);
ChannelBean chl = new ChannelBean(obj.getString("name"),obj.getBoolean("isSelect"));
pinlist.add(chl);
}
for (int i = 0; i <pinlist.size() ; i++) {
if(pinlist.get(i).isSelect())
{
list.add(pinlist.get(i).getName());
if(list.get(i).equals("头条"))
{
fragmentlist.add(new fragment1());
}else if(list.get(i).equals("社会"))
{
fragmentlist.add(new fragment2());
}else if(list.get(i).equals("国内"))
{
fragmentlist.add(new fragment3());
}else if(list.get(i).equals("国际"))
{
fragmentlist.add(new fragment4());
}else if(list.get(i).equals("娱乐"))
{
fragmentlist.add(new fragment5());
}
}
}
Myadapter myadapter = new Myadapter(getSupportFragmentManager(),fragmentlist,list);
view_pager.setAdapter(myadapter);
tablay.setupWithViewPager(view_pager);
} catch (JSONException e) {
e.printStackTrace();
}
}else{
list = new ArrayList<>();
fragmentlist = new ArrayList<>();
list.add("头条");
list.add("社会");
list.add("国内");
list.add("国际");
list.add("娱乐");
fragmentlist.add(new fragment1());
fragmentlist.add(new fragment2());
fragmentlist.add(new fragment3());
fragmentlist.add(new fragment4());
fragmentlist.add(new fragment5());
Myadapter myadapter = new Myadapter(getSupportFragmentManager(),fragmentlist,list);
view_pager.setAdapter(myadapter);
tablay.setupWithViewPager(view_pager);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==101){
String pdlist=data.getStringExtra("json");
sp.edit().putString("pdlist",pdlist).commit();
}
}