android 自定义视图之简单的轮播

来源:互联网 发布:centos无法输入密码 编辑:程序博客网 时间:2024/05/07 11:25

目的:实现一个像web网页的轮播效果 用到的主视图是 pageview。

效果截图如下:



主要是每隔2秒回播放一个页面,用handler类实现 其中的一些方法 详细看代码 (实现过程)

具体实现流程如下:

1)布局开始 ,代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   
    tools:context=".MainActivity" >


    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="200dp"
       />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/viewPager"
        android:background="#33000000"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="此处显示广告条"
            android:textColor="@android:color/white"
            android:textSize="18sp" />
        <!-- 此处显示下拉的点 -->
        <LinearLayout
            android:id="@+id/point"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </LinearLayout>
    </LinearLayout>


</RelativeLayout>

1)我们可以看到并没有点,就是每滑动一下就进入下一个点 ,布局里面没有 ,点可以是美工给我们的 imageview 由于没有 ,自己创建了shape.xml 文件 和 select .xml文件 ,通过不      同的事假触发加载不同的.五个点事java代码生成的

主类如下:


package com.example.h;


import java.util.ArrayList;
import java.util.List;


import java.util.logging.LogRecord;


import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends Activity {
private int lastPosition;//记录上一个点
private ViewPager viewPager ;
private TextView text ;
private LinearLayout linearLayout ;
public static List<ImageView> list = new ArrayList<ImageView>();
//几张图片的id用个数组装起来,用imageview的setbackgroundResourece(id)加载
int[] imageIds ={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
    
//图片标题集合
private final String[] imageDescriptions = {
"巩俐不低俗,我就不能低俗",
"扑树又回来啦!再唱经典老歌引万人大合唱",
"揭秘北京电影如何升级",
"乐视网TV版大派送",
"热血屌丝的反杀"
};




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

linearLayout = (LinearLayout) findViewById(R.id.point);
text = (TextView) findViewById(R.id.textview);
viewPager = (ViewPager) findViewById(R.id.viewPager);

//初始化list,给list 添加imageview
for (int i = 0; i < imageIds.length; i++) {
//图片加载
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(imageIds[i]);
list.add(imageView);//listview加载了5张图片
//增加五个点其实也是五个imageview
ImageView pointView = new ImageView(this);
//声明一个LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(5, 5);
params.rightMargin = 20 ;
//对点设置layoutparams

pointView.setLayoutParams(params);
//点的资源是个select 选择器
pointView.setBackgroundResource(R.drawable.point_bg);
if(i==0){//第一个点开始设置为true,加载对应的xml文件
pointView.setEnabled(true);

}else{
pointView.setEnabled(false);
}
//添加到线性布局中
linearLayout.addView(pointView);




}
viewPager.setAdapter(new Myadapter());
//设置当前viewpage显示的页面
viewPager.setCurrentItem(Integer.MAX_VALUE/2);
//handler发信息每隔俩秒发送一次信息
handler.sendEmptyMessageDelayed(0, 2000);


//给viewpage添加页面改变监听事件
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

//当页面改变时触发让标题栏显示此效果
public void onPageSelected(int position) {
//显示内容
text.setText(imageDescriptions[position%5]);
    //小图片修改啊
linearLayout.getChildAt(position%5).setEnabled(true);
//把上一个修改成enable
linearLayout.getChildAt(lastPosition).setEnabled(false);
//把现在的位置值给lastposition,好下次滑动时本次点为颜色变化
lastPosition = position%5;



}

//当页面滚动时触发,一般开发不写此方法
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {


}
//当页面滚动状态触发,一般开发不写逻辑代码
public void onPageScrollStateChanged(int state) {


}
});
}

//定义handler类实现自动跟新
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {

//得到viewpage并获得当前viewpager的当前的id 并使其加一
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
//再次发信息更新
handler.sendEmptyMessageDelayed(0, 2000);


}


};




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

  动态调用给textview设置值 通过String数组,还有生成的点,一般来说 适配器复杂点都会用集合装数据然后 通过position(适配器里面的)

其余代码如下:

适配器类:

package com.example.h;


import java.util.ArrayList;
import java.util.List;


import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/*
 * 为viewpage设置适配器
 * 
 * */
public class Myadapter extends PagerAdapter {



@Override
public int getCount() {
           //为了实现轮播的自动效果
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
if(view == object){

return view==object;
}else{

return false ;
}

}


@Override
public Object instantiateItem(ViewGroup container, int position) {
//位置对5取余
container.addView(MainActivity.list.get(position%5));
return MainActivity.list.get(position%5);
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
               container.removeView((View)object);
                object = null ;
}


}

三个xml文件:主要是为了花圆和选折其状态


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/point_normal" android:state_enabled="false"></item>
    <item android:drawable="@drawable/point_focus" android:state_enabled="true"></item>


</selector>



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="oval"
     >
     <size android:width="5dp" android:height="5dp"/>
     <solid android:color="#aaFFFFFF"/>
    


</shape>



<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
     >
     <size android:width="5dp" android:height="5dp"/>
     <solid android:color="#55000000"/>


</shape>

亮点:

handler类的使用俩个核心代码 反复使用

3 个xml 文件 

4.要把自动生成的v4牛奶瓶删了 自己再builderpath,系统生成的奶不好喝 ,还有运行是要把v4奶瓶勾上  

5.关注核心代码 颜色的 最起码要学会handler简单用法

6.要学会的pageadapter怎么用

7.对了 实现轮播除了handler 还有设置了adapter 有31亿个位置 ,通过对5取余,一开始设置其中的一个位置 (中间),可以左右滑动


0 0
原创粉丝点击