首先来看实现效果:
自定义view:
public class ShufflingView extends ViewFlipper implements View.OnClickListener {
private Context mContext;
private List<String> mShufflingList;
private onShufflingChildClickListener mShufflingChildClickListener;
public ShufflingView(Context context) {
super(context);
init(context);
}
public ShufflingView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
this.mContext = context;
//set padding
setPadding(dp2px(10), dp2px(5), dp2px(10), dp2px(5));
//set in out animation
setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.shuffling_in));
setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.shuffling_out));
}
/**
* 添加轮播的view
*
* @param shufflingList
*/
public void addShufflingView(List<String> shufflingList) {
if (shufflingList != null && shufflingList.size() > 0) {
this.mShufflingList = shufflingList;
for (int i = 0; i < shufflingList.size(); i++) {
TextView textView = new TextView(mContext);
textView.setText(shufflingList.get(i));
textView.setSingleLine();
textView.setTextSize(15f);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setTextColor(Color.parseColor("#ff00ff"));
//设置tag,用于获取position
textView.setTag(i);
textView.setOnClickListener(this);
//add textView to ViewFlipper
ShufflingView.this.addView(textView,
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
}
@Override
public void onClick(View v) {
int position = (int) v.getTag();
String content = mShufflingList.get(position);
if (mShufflingChildClickListener != null) {
mShufflingChildClickListener.onShufflingChildClick(position, content);
}
}
/**
* 设置轮播时间
*
* @param time
*/
public void setInterval(int time) {
//set shuffling interval
setFlipInterval(time);
}
/**
* dp --> px
*
* @param dpValue
* @return
*/
private int dp2px(float dpValue) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpValue,
mContext.getResources().getDisplayMetrics());
}
public void setOnShufflingChildClickListener(onShufflingChildClickListener clickListener) {
this.mShufflingChildClickListener = clickListener;
}
interface onShufflingChildClickListener {
void onShufflingChildClick(int position, String content);
}
}
在xml中使用:
<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="com.example.getphoto.MainActivity">
<TextView android:id="@+id/main_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:text="小二快报:"
android:textSize="15sp"/>
<com.example.getphoto.ShufflingView
android:id="@+id/main_shuffling"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/main_tv"/>
</RelativeLayout>
在activity中使用:
ShufflingView mainShuffling = (ShufflingView) findViewById(R.id.main_shuffling);
mainShuffling.setInterval(2000);
List<String> list = Arrays.asList(
"震惊!老汉半夜不睡觉是为啥",
"突发!日本居然6月下大雪",
"不敢相信!她们竟敢这么做!",
"震惊!隔壁居然住的是老王",
"欢迎走进科学栏目...");
mainShuffling.addShufflingView(list);
mainShuffling.startFlipping();