最近要在项目中实现这样一种效果,动态添加的数据类似ListView的显示效果,但是在一旦有一条数据时,那么才会把这条数据显示出来,在显示的同时上面的数据会自动向上滚动一行,来显示新添加的数据。思考了很久,想到ListView难以实现这样的效果,于是采用了ScrollView动态加载布局的方式。
思路:利用动态加载布局的方式实现滚动效果,需要一个垂直方向移动的动画文件。
思路:利用动态加载布局的方式实现滚动效果,需要一个垂直方向移动的动画文件。
布局:ScrollView里面嵌套一层LinearLayout,LinearLayout里面动态添加其他组件。
代码:下面是我写的一个Demo
public class MainActivity extends Activity {
private ScrollView scrollView;
Handler handler;
@SuppressLint("HandlerLeak")
LinearLayout layout;
Timer timer;
int str=111;
TextView tx1,tx2,tx3,tx4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.scroll_view);
layout = (LinearLayout) findViewById(R.id.layout);
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==11){
RelativeLayout relay = new RelativeLayout(MainActivity.this);
relay.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,50));
TextView tx = new TextView(MainActivity.this);
TextView tx1 = new TextView(MainActivity.this);
TextView tx2 = new TextView(MainActivity.this);
TextView tx3 = new TextView(MainActivity.this);
TextView tx4 = new TextView(MainActivity.this);
TextView tx5 = new TextView(MainActivity.this);
LayoutParams params = new LayoutParams(-1, -2);
// tx.setLayoutParams(params);
tx1.setTextSize(30);
tx2.setTextSize(30);
tx3.setTextSize(30);
tx4.setTextSize(30);
tx5.setTextSize(30);
tx.setPadding(0, 0, 0, 0);
tx1.setPadding(20, 0, 0, 0);
tx2.setPadding(100, 0, 0, 0);
tx3.setPadding(180, 0, 0, 0);
tx4.setPadding(260, 0, 0, 0);
tx5.setPadding(380, 0, 0, 0);
relay.addView(tx, params);
relay.addView(tx1, params);
relay.addView(tx2, params);
relay.addView(tx3, params);
relay.addView(tx4, params);
relay.addView(tx5, params);
tx.setText(str+" tx");
tx1.setText(str+" tx1");
tx2.setText(str+" tx2");
tx3.setText(str+" tx3");
tx4.setText(str+" tx4");
tx5.setText(str+" tx5");
ImageView image = new ImageView(MainActivity.this);
image.setBackgroundResource(R.drawable.im_dottend_line);
image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,1));
layout.addView(relay);
layout.addView(image);
str++;
TranslateAnimation anim = (TranslateAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.search);
if(scrollView.getScrollY()!=(layout.getHeight()-scrollView.getHeight())){
scrollView.setScrollY(layout.getHeight());
Log.i("", "str===="+str);
if(str>118){
layout.setAnimation(anim);
}
}
}
}
};
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(11);
}
},0, 1000);
}
@Override
protected void onDestroy() {
if(timer!=null){
timer.cancel();
}
super.onDestroy();
}
}
但是有个缺陷,数据多了,那么new出来的对象激增,占用内存,影响效果,而且到后面的时候,滚动效果也不明显。不过肯定可以删除消失的布局,这样对象减少了。
这是项目当中的代码片段,我把消失的组件已经删除了,其实每次加载数据的时候,整个scrollView中只有8条数据,因为我 只需要展示8条数据的空间。动画的速度始终保持一致,也不会出现卡顿的现象。
/**
* 搜索频段显示的数据 count是scrollView显示的总行数
*/
private void searchShow() {
count=layout.getChildCount();
if(count>6){//当显示到第3行,layout开始动画,把需要还未添加数据的区域绘制出来,但还未显示,代码执行到下面会把数据填充上去,同时数据也会随之滚动出来
TranslateAnimation anim = (TranslateAnimation) AnimationUtils.loadAnimation(AFAutoSearchActivity.this, R.anim.search);
layout.setAnimation(anim);
}
String symbol = "Kbps";
String Fre = "MHz";
String modulate= "QAM";
String num="";
RelativeLayout relay = new RelativeLayout(AFAutoSearchActivity.this);
relay.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
TextView symbolText = new TextView(AFAutoSearchActivity.this);
TextView FreqText = new TextView(AFAutoSearchActivity.this);
TextView modulationText = new TextView(AFAutoSearchActivity.this);
TextView tvNumText = new TextView(AFAutoSearchActivity.this);
TextView radioNumText = new TextView(AFAutoSearchActivity.this);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
symbolText.setTextSize(30);
FreqText.setTextSize(30);
modulationText.setTextSize(30);
tvNumText.setTextSize(30);
radioNumText.setTextSize(30);
symbolText.setTextColor(Color.WHITE);
FreqText.setTextColor(Color.WHITE);
modulationText.setTextColor(Color.WHITE);
tvNumText.setTextColor(Color.WHITE);
radioNumText.setTextColor(Color.WHITE);
relay.setPadding(0, 15, 0, 15);
symbolText.setPadding(0, 0, 0, 0);
symbolText.setPadding(150, 0, 0, 0);
modulationText.setPadding(335, 0, 0, 0);
tvNumText.setPadding(570, 0, 0, 0);
radioNumText.setPadding(680, 0, 0, 0);
relay.addView(symbolText, params);
relay.addView(FreqText, params);
relay.addView(modulationText, params);
relay.addView(tvNumText, params);
relay.addView(radioNumText, params);
symbolText.setText(symbolRate+symbol);
FreqText.setText(searchFreq/1000+Fre);
modulationText.setText(modulation+modulate);
if(tv_num > 0 ){
if (tunerState == KV_TUNER_STATUS_e.KV_TUNER_LOCKED.ordinal()) {
tvNum = tv_num - pretvNum ;
tvNumText.setText(tvNum+num);
pretvNum = tv_num;
} else if (tunerState == KV_TUNER_STATUS_e.KV_TUNER_LOST.ordinal()) {
tvNumText.setText("-");
}
}else{
tvNumText.setText("-");
}
if(radio_num > 0 ){
if (tunerState == KV_TUNER_STATUS_e.KV_TUNER_LOCKED.ordinal()) {
adNum = radio_num - preadNum ;
radioNumText.setText(adNum+num);
preadNum = radio_num;
} else if (tunerState == KV_TUNER_STATUS_e.KV_TUNER_LOST.ordinal()) {
radioNumText.setText("-");
}
}else{
radioNumText.setText("-");
}
//分割线
ImageView image = new ImageView(AFAutoSearchActivity.this);
image.setBackgroundResource(R.drawable.im_dottend_line);
image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,1));
layout.addView(relay);
layout.addView(image);
if(count>8){//当第一行消失时,就把他移除
layout.removeViewsInLayout(0, 2);
}
}