侧拉菜单
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.yuekaomn.MainActivity">
<!--侧拉菜单-->
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drlayout">
<!--主页面的布局-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fcontent">
</FrameLayout>
<!--侧拉菜单的布局 android:layout_gravity="start" 菜单在左边 end 菜单在右边 -->
<RelativeLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#80FF80"
android:id="@+id/rel">
<ImageView
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/img"/>
<ListView
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
android:layout_below="@id/img"></ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
竖着的Tablayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<!--
app:tabGravity="center"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimaryDark"
app:tabTextColor="@color/colorPrimary"
-->
<q.rorbin.verticaltablayout.VerticalTabLayout
android:id="@+id/tab"
android:layout_width="100dp"
android:layout_height="match_parent"
></q.rorbin.verticaltablayout.VerticalTabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:background="@color/colorPrimary"></android.support.v4.view.ViewPager>
</LinearLayout>
Viewpage+RadioGroup布局
<?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">
<RadioGroup
android:layout_alignParentBottom="true"
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb_01"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center"
android:text="首页"
android:button="@null"
android:background="@drawable/but"
android:checked="true"
/>
<RadioButton
android:id="@+id/rb_02"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="分类"
android:textSize="18sp"
android:button="@null"
android:background="@drawable/but"
/>
<RadioButton
android:id="@+id/rb_03"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center"
android:text="发现"
android:button="@null"
android:background="@drawable/but"
/>
<RadioButton
android:id="@+id/rb_04"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center"
android:text="购物车"
android:button="@null"
android:background="@drawable/but"
/>
<RadioButton
android:id="@+id/rb_05"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center"
android:text="我的"
android:button="@null"
android:background="@drawable/but"
/>
</RadioGroup>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_above="@+id/rg"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</RelativeLayout>
PullToRefreshScrollView嵌套Banner.GridView
<?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"
>
<!-- android:orientation="vertical"-->
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
>
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="240dp"></com.youth.banner.Banner>
<GridView
android:layout_below="@+id/banner"
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="480dp"
android:numColumns="2"
></GridView>
</RelativeLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</RelativeLayout>
按钮的颜色
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@color/colorAccent"></item>
<item android:drawable="@color/colorPrimary"></item>
</selector>
功能代码:
TabLayout的功能代码
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ListView listView;
private RelativeLayout rel;
private ArrayList datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找组件
drawerLayout = (DrawerLayout) findViewById(R.id.drlayout);
listView = (ListView) findViewById(R.id.lv);
rel = (RelativeLayout) findViewById(R.id.rel);
//构造数据
datas = new ArrayList<String>();
datas.add("个人设置");
datas.add("缓存");
datas.add("夜间模式");
datas.add("配置");
//主页面Fragment
ContentFragment contentFragment=new ContentFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fcontent,contentFragment).commit();
//设置数组适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//关闭侧滑菜单
drawerLayout.closeDrawer(rel);
}
});
}
}
自定义Gridview的方法
public class MyGridview extends GridView {
public MyGridview(Context context) {
super(context);
}
public MyGridview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
Banner需要的imagload的方法
public class Imagload extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
//得到ImageLoader的实例
com.nostra13.universalimageloader.core.ImageLoader instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
//通过ImageLoader设置图片
instance.displayImage((String) path, imageView);
}
}
实现三秒切换图片的方法
public class Confregment2 extends Fragment{
private int count = 0;
private ImageView img;
ArrayList<String> list = new ArrayList<>();
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
count++;
String s = list.get(count % list.size());
ImageLoader.getInstance().displayImage(s,img, MyApp.getDefaultDisplayOption());
sendEmptyMessageDelayed(0,3000);
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.confragment2, null);
img = (ImageView) view.findViewById(R.id.img);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MyTask myTask = new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson = new Gson();
Bean2 bean = gson.fromJson(jsonstr, Bean2.class);
List<Bean2.DataScrollBean> dataScroll = bean.getDataScroll();
Log.i("aaa",dataScroll.toString());
for(int i=0;i<dataScroll.size();i++)
{
String imageurl = dataScroll.get(i).getIMAGEURL();
String url="http://www.93.gov.cn"+imageurl;
list.add(url);
}
handler.sendEmptyMessage(0);
}
});
myTask.execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0");
}
}
实现viewpage+fragment+RadioGroup联动的代码
public class ContentFragment extends Fragment {
private RadioGroup rg;
private ViewPager vp;
List<Fragment> list = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=View.inflate(getActivity(), R.layout.item,null);
rg = (RadioGroup) v.findViewById(R.id.rg);
vp = (ViewPager) v.findViewById(R.id.vp);
return v;
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list.add(new shouyefregment());
list.add(new flfregment());
list.add(new fxfregment());
list.add(new cwcfregment());
list.add(new wodefregment());
vp.setOffscreenPageLimit(list.size());
myadapter myadapter = new myadapter(getChildFragmentManager());
vp.setAdapter(myadapter);
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
rg.check(rg.getChildAt(position).getId());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId){
case R.id.rb_01:
vp.setCurrentItem(0);
break;
case R.id.rb_02:
vp.setCurrentItem(1);
break;
case R.id.rb_03:
vp.setCurrentItem(2);
break;
case R.id.rb_04:
vp.setCurrentItem(3);
break;
case R.id.rb_05:
vp.setCurrentItem(4);
break;
}
}
});
}
class myadapter extends FragmentPagerAdapter {
public myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
}
实现Tablayout+viewpage+fragment的代码
public class flfregment extends Fragment{
private VerticalTabLayout tab;
ArrayList<String> datas = new ArrayList<>();
private ViewPager vp;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.flfragment, null);
tab = (VerticalTabLayout) view.findViewById(R.id.tab);
vp = (ViewPager) view.findViewById(R.id.vp);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//添加选项卡
datas.add("香包手袋");
datas.add("美妆个护");
datas.add("钟表珠宝");
datas.add("手机数码");
datas.add("电脑办公");
datas.add("家用电器");
datas.add("食品生鲜");
datas.add("酒水饮料");
datas.add("母婴童装");
//设置加载页数
vp.setOffscreenPageLimit(datas.size());
vp.setAdapter(new MyAdapter(getChildFragmentManager()));
//进行关联
tab.setupWithViewPager(vp);
}
class MyAdapter extends FragmentPagerAdapter{
//带参的构造方法
public MyAdapter(FragmentManager fm) {
super(fm);
}
//返回选项卡的文本 ,,,添加选项卡
@Override
public CharSequence getPageTitle(int position) {
return datas.get(position);
}
@Override
public Fragment getItem(int position) {
Log.d("zzz","position:"+position);
//创建fragment对象并返回
Confregment2 confregment2 = new Confregment2();
return confregment2;
}
@Override
public int getCount() {
return datas.size();//返回选项卡的数量
}
}
}
PullToRefreshScrollView嵌套Banner.GridView实现功能代码
public class shouyefregment extends Fragment {
private GridView gv;
private int index = 400;
private ListView pullToRefreshListView;
private Handler myHandler = new Handler();
List<Bean.NewslistBean> data=new ArrayList<Bean.NewslistBean>();
private ViewPager vp;
List<String> list = new ArrayList<>();
private Banner banner;
private PullToRefreshScrollView sv;
private MyAdapter adapter;
//https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=3
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.syfragment, null);
gv = (GridView) view.findViewById(R.id.gv);
sv = (PullToRefreshScrollView) view.findViewById(R.id.sv);
banner = (Banner) view.findViewById(R.id.banner);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initLv();
initData();
setAdapter();
//Binner
initdata2();
}
public void initLv() {
//设置刷新模式 ,both代表支持上拉和下拉,pull_from_end代表上拉,pull_from_start代表下拉
sv.setMode(PullToRefreshBase.Mode.BOTH);
//*这里通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
//如果想区分上拉和下拉状态的不同,可以分别设置*//*
ILoadingLayout startLabels = sv.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在拉");
startLabels.setReleaseLabel("放开刷新");
ILoadingLayout endLabels = sv.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
//*如果Mode设置成Mode.BOTH,需要设置刷新Listener为OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。
// 如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置刷新Listener为OnRefreshListener,同时实现onRefresh()方法。
//当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM_END的时候只调用onPullUpToRefresh()方法.
//*加载数据完成后 必须 调用下 onRefreshComplete() 完成关闭 header,footer视图
sv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
addToTop();//请求网络数据,并更新listview组件
refreshComplete();//数据加载完成后,关闭header,footer
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
addToBottom();//请求网络数据,并更新listview组件
refreshComplete();//数据加载完成后,关闭header,footer
}
});
}
public void initData(){
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Bean bean=gson.fromJson(jsonstr, Bean.class);
List<Bean.NewslistBean> newslist = bean.getNewslist();
data.addAll( newslist) ;
//设置适配器
setAdapter();
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=5");
}
public void addToBottom(){
index--;
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Bean bean=gson.fromJson(jsonstr, Bean.class);
List<Bean.NewslistBean> newslist = bean.getNewslist();
data.addAll( newslist) ;
//设置适配器
setAdapter();
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+index);
}
public void setAdapter(){
if(adapter==null){
adapter = new MyAdapter();
gv.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
}
public void addToTop(){
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Bean bean=gson.fromJson(jsonstr, Bean.class);
List<Bean.NewslistBean> newslist = bean.getNewslist();
data.addAll(0,newslist) ;
//设置适配器
setAdapter();
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1");
}
public void initdata2(){
list.add("http://p0.so.qhimgs1.com/bdr/_240_/t011b994ed459c6ca3c.jpg");
list.add("http://p2.so.qhmsg.com/bdr/_240_/t0175d0526d5f1079e7.jpg");
list.add("http://p4.so.qhimgs1.com/bdr/_240_/t01137b4acd5ed715d4.jpg");
list.add("http://p2.so.qhimgs1.com/bdr/_240_/t01522004aef7e26f72.jpg");
list.add("http://p0.so.qhimgs1.com/bdr/_240_/t015bbdf7db769761fc.jpg");
banner.setImageLoader(new Imagload());
//为Banner设置实现轮播的图片路径的集合
banner.setImages(list);
//开启轮播
banner.start();
}
//* * 刷新完成时关闭
public void refreshComplete() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
sv.onRefreshComplete();
}
}, 1000);
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if(convertView==null)
{
vh = new ViewHolder();
convertView= View.inflate(getContext(), R.layout.gridviewitem, null);
vh.tv = (TextView) convertView.findViewById(R.id.tv);
vh.img = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(vh);
}else{
vh= (ViewHolder) convertView.getTag();
}
vh.tv.setText(data.get(position).getTitle());
ImageLoader.getInstance().displayImage(data.get(position).getPicUrl(),vh.img,MyApp.getDefaultDisplayOption());
return convertView;
}
}
class ViewHolder{
TextView tv;
ImageView img;
}
}