引导页:
activity_main中布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.com.myyuekao1.MainActivity"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp"></android.support.v4.view.ViewPager> </LinearLayout>
MainActivity中代码:
package com.example.com.myyuekao1; import android.content.Intent; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<View>list=new ArrayList(); private ViewPager vp; private Button but; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = findViewById(R.id.vp); but = findViewById(R.id.but); getDatas(); vp.setAdapter(new MyAdapter()); } private void getDatas() { View v1=View.inflate(this,R.layout.view,null); list.add(v1); View v2=View.inflate(this,R.layout.view1,null); list.add(v2); View v3=View.inflate(this,R.layout.view2,null); list.add(v3); } public void dianji(View view){ Intent intent = new Intent(MainActivity.this, Second.class); startActivity(intent); } private class MyAdapter extends PagerAdapter { @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return object==view; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(list.get(position)); } } }
注:创建三个布局文件,把图片放进后跳转:
图片轮播和下方小圆点
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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.com.myyuekao1.Second2"> <!--只能有一个子孩子 --> <com.handmark.pulltorefresh.library.PullToRefreshScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/psv"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!--轮播图--> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/vp"></android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linpot" android:orientation="horizontal" android:gravity="center" android:layout_gravity="bottom"></LinearLayout> </FrameLayout> <com.example.com.myyuekao1.MylistView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/mylv" > </com.example.com.myyuekao1.MylistView> </LinearLayout> </com.handmark.pulltorefresh.library.PullToRefreshScrollView> </LinearLayout>
图片轮播小圆点:
Activite:
package com.example.com.myyuekao1; import android.graphics.Bitmap; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ScrollView; import android.widget.Toast; import com.example.com.myyuekao1.util.MyTask; import com.google.gson.Gson; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshScrollView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import java.util.ArrayList; import java.util.List; public class Second2 extends AppCompatActivity { private PullToRefreshScrollView psv; private MylistView mylv; private int type; private ViewPager vp; private LinearLayout linpot; private int num=0; private int one = 1; private List<ImageView> li = new ArrayList<>(); private ArrayList<String> as; private List<jsonBean.DataBean>list=new ArrayList<>(); private String urldata="http://www.93.gov.cn/93app/data.do?channelId=0&startNum="+num; private DisplayImageOptions options; private Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { int position = vp.getCurrentItem(); position++; vp.setCurrentItem(position); //调用方法 //改变指示器 // setSelectedPoint(position%li.size()); ins(position % as.size()); sendEmptyMessageDelayed(0, 1000); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second2); psv = findViewById(R.id.psv); mylv = findViewById(R.id.mylv); vp = findViewById(R.id.vp); linpot = findViewById(R.id.linpot); intpsv(); getdatas(); options = new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时 .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2字节 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 //displayer(new RoundedBitmapDisplayer(100))//设置圆角 30代表半径 自定义 .build(); vp.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: //移除所有的消息,不再自动轮播 myHandler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_MOVE: myHandler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_UP: //再次发送消息 myHandler.sendEmptyMessageDelayed(0, 1000); break; case MotionEvent.ACTION_CANCEL: //再次发送消息 // Toast.makeText(Second.this, "sdfdsfdsfds", Toast.LENGTH_SHORT).show(); myHandler.sendEmptyMessageDelayed(0, 2000); break; } return false; } }); } private void setSelectedPoint(int pageIndex) { for (int i = 0; i < li.size(); i++) { if (i == pageIndex) { li.get(i).setImageResource(R.drawable.point_selected); } else { li.get(i).setImageResource(R.drawable.point_unselected); } } } private void getdatas() { MyTask myTask = new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson = new Gson(); jsonBean jsonBean = gson.fromJson(jsonstr, jsonBean.class); List<jsonBean.DataBean> data = jsonBean.getData(); if (type==1){ list.clear(); } list.addAll(jsonBean.getData()); as = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { String picUrl = data.get(i).getIMAGEURL(); as.add(picUrl); } as = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { String picUrl = data.get(i).getIMAGEURL(); as.add(picUrl); } if (one == 1) { //linpot.removeAllViews(); for (int i = 0; i < data.size(); i++) { ImageView iv = new ImageView(Second2.this); if (i == 0) { iv.setBackgroundResource(R.drawable.point_selected); } else { iv.setBackgroundResource(R.drawable.point_unselected); } linpot.addView(iv); li.add(iv); /* //设置小圆点的margin值 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1); lp.setMargins(5, 10, 5, 10); iv.setLayoutParams(lp);*/ } } vp.setAdapter(new MyVpAdapter()); mylv.setAdapter(new MyAdapter4(Second2.this,data)); myHandler.sendEmptyMessageDelayed(0,1000); } }); myTask.execute(urldata); psv.onRefreshComplete(); } private void intpsv() { psv.setMode(PullToRefreshBase.Mode.BOTH); psv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) { type = 1; num = 10; myHandler.removeCallbacksAndMessages(null); urldata="http://www.93.gov.cn/93app/data.do?channelId=0&startNum="+num; getdatas(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) { type = 2; num+=10; myHandler.removeCallbacksAndMessages(null); urldata="http://www.93.gov.cn/93app/data.do?channelId=0&startNum="+num; getdatas(); } }); } private class MyVpAdapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, final int position) { ImageView img = new ImageView(Second2.this); //img.setImageResource(R.drawable.default_ptr_flip); Log.d("--", "instantiateItem: "+img); ImageLoader.getInstance().displayImage(as.get(position%as.size()),img,options); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(Second2.this,as.get(position)+"", Toast.LENGTH_SHORT).show(); } }); container.addView(img); return img; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } /* private void initPoint() { //移除所有的子视图 linpot.removeAllViews(); //清空集合 li.clear(); }*/ private void ins(int pageIndex) { Log.d("aaa=========", li.size() + "dasfsdfsdfsdf"); for (int i = 0; i < li.size(); i++) { if (i == pageIndex) { li.get(i).setImageResource(R.drawable.point_selected);//设置为选中的 } else { li.get(i).setImageResource(R.drawable.point_unselected); } } } }
注小圆点制作用shape
point_selected
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!--填充--> <solid android:color="#fff"></solid> <!--描边--> <stroke android:color="#f00" android:width="2dp"></stroke> <!--设置大小--> <size android:width="10dp" android:height="10dp"></size> </shape>
point_unselected:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#fff"></solid> <!--圆角--> <corners android:radius="5dp"></corners> <!--设置大小--> <size android:width="12dp" android:height="5dp"></size> </shape>
抽屉布局中数据请求后点击跳转的抽屉的布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dl"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f00" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9.1" android:id="@+id/fl"> </FrameLayout> <RadioGroup android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.9" android:id="@+id/rg" android:orientation="horizontal" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/but1" android:gravity="center" android:checked="true" android:drawableBottom="@drawable/select" android:button="@null" android:text="首页"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/but2" android:gravity="center" android:checked="true" android:drawableBottom="@drawable/select1" android:button="@null" android:text="咨询"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/but3" android:gravity="center" android:checked="true" android:drawableBottom="@drawable/select2" android:button="@null" android:text="设置"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/but4" android:checked="true" android:gravity="center" android:onClick="tiao" android:drawableBottom="@drawable/select3" android:button="@null" android:text="我的"/> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="200dp" android:layout_height="match_parent" android:orientation="vertical" android:background="#f00" android:layout_gravity="start" > <ImageView android:layout_width="180dp" android:layout_height="180dp" android:id="@+id/img" android:background="@mipmap/ic_launcher"/> <ListView android:layout_width="match_parent" android:layout_marginTop="100dp" android:layout_height="match_parent" android:id="@+id/lv"></ListView> </LinearLayout> </android.support.v4.widget.DrawerLayout>