<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9" />
<RadioGroup
android:id="@+id/rg"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<RadioButton
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:button="@null"
android:text="首页"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18dp"
/>
<RadioButton
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:button="@null"
android:text="发现"
android:layout_weight="1"
android:gravity="center"
android:textSize="18dp"
/>
<RadioButton
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:button="@null"
android:text="下载"
android:layout_weight="1"
android:gravity="center"
android:textSize="18dp"
/>
<RadioButton
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:button="@null"
android:text="我的"
android:textSize="18dp"
android:layout_weight="1"
android:gravity="center"
/>
</RadioGroup>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/tabLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabLayout"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/psl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="100dp"/>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/ptListView"
android:layout_width="match_parent"
android:layout_height="700dp"
android:layout_below="@id/banner"
/>
</RelativeLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<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/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/t4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/t5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/img"
android:layout_width="200sp"
android:layout_height="200sp" />
</LinearLayout>
package com.example.sunrongzhen1511b20171118;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
/**
* Created by Sun on 2017/11/18.
*/
public class Fragment01 extends Fragment {
private TabLayout mTabLayout;
private ViewPager mVp;
String[] tabs = {"推荐", "课程", "实战", "职业路径"};
private ArrayList<FragmentVp> l;
private String[] urlS = {
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/1/",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/2/",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/3/",
"http://gank.io/api/data/%E7%A6%8F%E5%88%A9/4/"
};
private FragmentManager fm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.f1,container,false);
mTabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
mVp = (ViewPager) view.findViewById(R.id.vp);
l = new ArrayList<FragmentVp>();
fm = getActivity().getSupportFragmentManager();
for (int i=0;i<tabs.length;i++){
FragmentVp fragmentVp = new FragmentVp();
Bundle bundle = new Bundle();
bundle.putString("dataUrl",urlS[i]);
fragmentVp.setArguments(bundle);
l.add(fragmentVp);
mTabLayout.addTab(mTabLayout.newTab().setText(tabs[i]));
}
MyVpApdater myVpApdater = new MyVpApdater(fm);
mVp.setAdapter(myVpApdater);
mTabLayout.setupWithViewPager(mVp);
return view;
}
class MyVpApdater extends FragmentPagerAdapter{
public MyVpApdater(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return l.get(position);
}
@Override
public int getCount() {
return l.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
}
}
package com.example.sunrongzhen1511b20171118;
import android.os.AsyncTask;
import android.os.Bundle;
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.ListView;
import android.widget.ScrollView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.youth.banner.Banner;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Sun on 2017/11/17.
*/
public class FragmentVp extends Fragment{
private String dataUrl;
private PullToRefreshListView listv;
private String data;
private List<SuperClass.Bean> list=new ArrayList<SuperClass.Bean>();
MyAdapter adapter;
int i=1;
private Banner bann;
String url = "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg";
String url1 = "http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg";
String url2 = "https://img-my.youkuaiyun.com/uploads/201309/01/1378037235_7476.jpg";
String url3 = "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg";
String url4 = "http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg";
String url5 = "https://img-my.youkuaiyun.com/uploads/201309/01/1378037235_7476.jpg";
ArrayList<String> urls;
private View view;
private PullToRefreshScrollView newLv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_layout,container,false);
listv = (PullToRefreshListView) view.findViewById(R.id.ptListView);
bann = (Banner) view.findViewById(R.id.banner);
newLv = (PullToRefreshScrollView) view.findViewById(R.id.psl);
urls = new ArrayList<String>();
urls.add(url);
urls.add(url1);
urls.add(url2);
urls.add(url3);
urls.add(url4);
urls.add(url5);
bann.setImageLoader(new MyBannerImageLoader());//添加自定义的图片加载器
bann.setImages(urls);//设置图片资源
bann.start();//开始轮播
Bundle arguments = getArguments();//获得通过setArgument传的值
dataUrl = getArguments().getString("dataUrl");//得到数据url
adapter = new MyAdapter(getActivity(), list);
login(dataUrl+i);
listv.setAdapter(adapter);
newLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
newLv.postDelayed(new Runnable() {
@Override
public void run() {
i=1;
list.clear();
login(dataUrl+i);
newLv.onRefreshComplete();
}
},1000);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
i++;
login(dataUrl+i);
}
});
listv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
i++;
login(dataUrl+i);
}
});
return view;
}
private void login(String pamm){
new MyTask().execute(pamm);
}
class MyTask extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... params) {
String jj=params[0];
NetWorkUtils netWorkUtils = new NetWorkUtils();
data = netWorkUtils.getURL(jj);
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
SuperClass superClass = gson.fromJson(s, SuperClass.class);
List<SuperClass.Bean> lists = superClass.getResults();
list.addAll(lists);
adapter.notifyDataSetChanged();
}
}
}
package com.example.sunrongzhen1511b20171118;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
private RadioGroup mRg;
private FrameLayout mFrag;
private Fragment01 f1;
private Fragment02 f2;
private Fragment03 f3;
private Fragment04 f4;
private List<Fragment> list;
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
list = new ArrayList<Fragment>();
f1=new Fragment01();
f2=new Fragment02();
f3=new Fragment03();
f4=new Fragment04();
list.add(f1);
list.add(f2);
list.add(f3);
list.add(f4);
manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.frag,list.get(0)).commit();
mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.bt1:
manager.beginTransaction().replace(R.id.frag,f1).commit();
break;
case R.id.bt2:
manager.beginTransaction().replace(R.id.frag,f2).commit();
break;
case R.id.bt3:
manager.beginTransaction().replace(R.id.frag,f3).commit();
break;
case R.id.bt4:
manager.beginTransaction().replace(R.id.frag,f4).commit();
break;
}
}
});
}
private void initView() {
mRg = (RadioGroup) findViewById(R.id.rg);
mFrag = (FrameLayout) findViewById(R.id.frag);
}
}
viewpage+fragment
最新推荐文章于 2019-02-25 19:45:03 发布