本控件将实现类似淘宝头条的向上滚动的效果,同时可以自定义Animation实现更多的动画效果,支持自定义布局,我例子中的是一个Button和一个TextView,见下图。
一.SwitchView.java
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ViewSwitcher;
public class SwitchView extends ViewSwitcher implements ViewSwitcher.ViewFactory {
private int layoutId;
private ViewBuilder viewInterface;
private boolean enable = true;
public SwitchView(Context context) {
this(context, null);
}
public SwitchView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public View makeView() {
return LayoutInflater.from(getContext()).inflate(layoutId, null);
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
//加载新界面
if (enable) {
viewInterface.initView(getNextView());
setInAnimation(getContext(), R.anim.slide_in);
setOutAnimation(getContext(), R.anim.slide_out);
showNext();
handler.sendEmptyMessageDelayed(0, 2000);
}
}
};
public void initView(int layoutId, ViewBuilder viewInterface) {
this.layoutId = layoutId;
this.viewInterface = viewInterface;
//设置ViewFactory后会调用makeView()
setFactory(this);
//发送启动消息
handler.sendEmptyMessage(0);
}
public void enable(boolean enable) {
this.enable = enable;
if (enable)
handler.sendEmptyMessage(0);
}
interface ViewBuilder {
void initView(View view);
}
}
二.MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity1 extends AppCompatActivity {
SwitchView sv;
Button btn;
String[] s = new String[]{"白日依山尽", "黄河入海流", "欲穷千里目", "更上一层楼",
"离离原上草", "一岁一枯荣", "野火烧不尽", "春风吹又生",
"锄禾日当午", "汗滴禾下土", "谁知盘中餐", "粒粒皆辛苦",
"鹅鹅鹅", "曲项向天歌", "白毛浮绿水", "红掌拨清波"};
int i = 0;
boolean enable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
sv = (SwitchView) findViewById(R.id.scrolltv);
btn = (Button) findViewById(R.id.btn_enable);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sv.enable(enable);
enable = !enable;
}
});
sv.initView(R.layout.switchview, new SwitchView.ViewBuilder() {
@Override
public void initView(View view) {
TextView tv = (TextView) view.findViewById(R.id.time);
tv.setText(System.currentTimeMillis() + "");
final Button button = (Button) view.findViewById(R.id.btn);
button.setText(s[i % 10]);
button.setTag(i);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity1.this, s[(int) button.getTag()], Toast.LENGTH_LONG).show();
}
});
i++;
if (i == s.length) {
i = 0;
}
}
});
}
}
三.activity_main1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.zkt.demo.MainActivity">
<com.zkt.demo.SwitchView
android:id="@+id/scrolltv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="250dp"
android:background="#0f0"
android:paddingBottom="5dp"
android:paddingTop="5dp"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_enable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停/开始" />
</android.support.constraint.ConstraintLayout>
四.switvhview.xml
<?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"
android:padding="10dp">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn"
android:layout_gravity="center_horizontal"
android:id="@+id/time"
android:textSize="16sp" />
</RelativeLayout>
五.slide_in.xml和slide_out.xml
1.Vertical
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0"
android:toYDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
2.Horizontal
只需要将上面的fromYDelta和toYDelta中的Y改成X即可实现横向滚动。
可以看到使用的时候非常简单,只需要find之后实现initView(int layoutId,ViewBuilderbuilder方法即可,其中layoutId是要滚动的布局,viewbuilder内实现滚动布局的实例化操作。
你只需要控制position进行布局数据的更新。当界面切换时,你可以通过enable(boolean enable)控制滚动的暂停/播放。