博文目的:
-
课堂上老师讲了遍,课后自己又敲了遍,现在再来复习一遍。
实现功能
-
代码环境
-
貌似不重要,只要是实现思路
创建视图文件
-
创建在layout目录下创建 activity_viewpager.xml 文件
-
MyPageView是自己创建的类,主要是显示当前页面。
-
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="200dp"
>
<com.gx.myapplication.control.MyPageView
android:id="@+id/av_viewav_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.gx.myapplication.control.MyPageView>
<LinearLayout
android:id="@+id/av_indicator"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp">
</LinearLayout>
</RelativeLayout>
重写MyPageView类
-
在其他位置创建MyPageView类
package com.gx.myapplication.control;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
/**
* 重写PageView控件
*
* MyPageView
* data 2017/9/11 14:44
* author L-z
*/
public class MyPageView extends ViewPager {
public MyPageView(Context context, AttributeSet attributeSet){
super(context,attributeSet);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event){
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
last_dx=event.getX();
last_dy=event.getY();
getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
if(Math.abs(event.getX()-last_dx)>Math.abs(event.getY())){
getParent().requestDisallowInterceptTouchEvent(true);
}else{
getParent().requestDisallowInterceptTouchEvent(false);
}
break;
case MotionEvent.ACTION_CANCEL:
getParent().requestDisallowInterceptTouchEvent(true);
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
float last_dx;
float last_dy;
}
创建activity_viewpage_itme.xml文件
-
这里才是每个轮播图的ImageView
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/cover"
android:background="@android:color/holo_red_light"
android:scaleType="centerCrop"
android:contentDescription="@string/app_name"
android:src="@drawable/bg3">
</ImageView>
创建 Activity
-
创建Second_Activity.java的文件,这里就不详细描述了
-
注意,我是在ListView的头部加入轮播的
-
这里说一下思路:首先在onCreate中先得到轮播视图中的MyPageView对象和小圆点对象,然后将整个轮播视图放在ListView的头部。当然,还要依赖 轮播图的实现类 MyVpAdater 主要是添加视图和切换视图。
接下来就是加载数据了,initPages()方法。还有个细节就是轮播切换的时候,需要更改小圆点addOnPageChangeListener事件
/**
* 方法结构:
* 1、系统构造方法重写
* 2、加载数据
* 3、对应的实现类+对应的方法
* Second_Activity
* data 2017/9/11 16:41
* author L-z
*/
public class Second_Activity extends AppCompatActivity {
@Override
protected void onCreate( Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
myAdapter=new MyAdapter();
ListView listView=(ListView)findViewById(R.id.listView);
listView.setAdapter(myAdapter);
initData();
View View=getLayoutInflater().inflate(R.layout.activity_viewpager,listView,false);
pageView= (MyPageView)View.findViewById(R.id.av_viewav_pager);
indicator= (LinearLayout)View.findViewById(R.id.av_indicator);
myVpAdapter = new MyVpAdater();
pageView.setAdapter(myVpAdapter);
pageView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_SETTLING) {
updateIndicator();
}
}
});
listView.addHeaderView(View);
}
public void initData () {
itemobjs.add(new Itemobj("正常运行","一天前","正在加载数据",""));
Log.d("Show","开始执行");
AsyncHttpClient client=new AsyncHttpClient();
try{
client.get("http://*******************",new AsyncHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.d("Show","到这里了,说明方法没问题");
try {
JSONObject object=new JSONObject(new String(responseBody));
JSONArray jsonArray=object.getJSONArray("novels");
itemobjs.clear();
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
itemobjs.add(new Itemobj(
jsonObject.getString("author"),
jsonObject.getString("updated"),
jsonObject.getString("novel_name"),
jsonObject.getString("poster")
));
}
Log.d("Show","没问题");
initPages();
myAdapter.notifyDataSetChanged();
myVpAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}catch (Exception e){
e.printStackTrace();
}
}
public void initPages(){
pages.clear();
ImageView cover = (ImageView) getLayoutInflater().inflate(R.layout.activity_viewpage_itme, null);
cover.setImageResource(R.drawable.bg1);
pages.add(cover);
cover = (ImageView) getLayoutInflater().inflate(R.layout.activity_viewpage_itme, null);
cover.setImageResource(R.drawable.bg2);
pages.add(cover);
cover = (ImageView) getLayoutInflater().inflate(R.layout.activity_viewpage_itme, null);
cover.setImageResource(R.drawable.bg3);
pages.add(cover);
indicator.removeAllViews();
for (int i=0;i<3;++i){
float width = Tool.dip2px(this,8.0f);
float height = Tool.dip2px(this,8.0f);
float margin_right = Tool.dip2px(this, 5.0f);
View view = new View(this);
if (i==0) {
view.setBackgroundResource(R.mipmap.indicator_selected);
}else {
view.setBackgroundResource(R.mipmap.indicator_nor);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)width, (int)height, 1.0f);
params.rightMargin = (int)margin_right;
view.setLayoutParams(params);
indicator.addView(view);
}
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return itemobjs.size();
}
@Override
public Object getItem(int position) {
return itemobjs.get(position);
}
@Override
public long getItemId(int position) {
return 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.activity_relativelayout,null);
holder=new Holder();
holder.name=(TextView)convertView.findViewById(R.id.rl_name);
holder.author=(TextView)convertView.findViewById(R.id.rl_author);
holder.date=(TextView)convertView.findViewById(R.id.rl_date);
convertView.setTag(holder);
}else{
holder=(Holder)convertView.getTag();
}
Itemobj itemobj=(Itemobj)getItem(position);
holder.name.setText(itemobj.name);
holder.author.setText(itemobj.author);
holder.date.setText(itemobj.date);
ImageView imageView = (ImageView)convertView.findViewById(R.id.rl_View);
imageView.setImageResource(R.drawable.bg2);
return convertView;
}
}
class Itemobj{
Itemobj(String author,String date,String name,String imgUrl){
this.author=author;
this.date=date;
this.name=name;
this.imgUrl=imgUrl;
}
String name;
String author;
String date;
String imgUrl;
}
class Holder{
TextView name;
TextView author;
TextView date;
}
class MyVpAdater extends PagerAdapter {
@Override
public int getCount() {
return pages.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem (ViewGroup container, int position) {
container.addView(pages.get(position));
return pages.get(position);
}
@Override
public void destroyItem (ViewGroup container,int position, Object object) {
View view = (View) object;
container.removeView(view);
}
}
public void updateIndicator () {
int pos = pageView.getCurrentItem();
for (int i=0; i<indicator.getChildCount(); ++i) {
View view = indicator.getChildAt(i);
view.setBackgroundResource(R.mipmap.indicator_nor);
}
indicator.getChildAt(pos).setBackgroundResource(R.mipmap.indicator_selected);
}
MyAdapter myAdapter;
List<Itemobj> itemobjs=new LinkedList<>();
List<View> pages = new LinkedList<>();
MyVpAdater myVpAdapter;
MyPageView pageView;
LinearLayout indicator;
}