Scrollview - TextView - 最好的滚动技术是什么
Scrollview - TextView - what is the best technique of scrolling
我的应用程序正在记录文本信息,我使用的技术基于嵌入到包含完整字符串的 ScrollView 中的 TextView。
此方法的真正缺点是字符串会随着时间的推移变得非常大,您可以体验到的最好情况是性能下降,如果不是因为系统内存不足而导致崩溃的话。
我可以使用哪些其他技巧?
这是我想出的代码:
public class MainActivity extends AppCompatActivity {
boolean run = true;
String str = "", t;
int i = 0;
TextView txt;
ScrollView sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.txt);
Button btn = (Button) findViewById(R.id.btn);
sv = (ScrollView) findViewById(R.id.sv);
t = "this is the log for monday the 24th of july 2017 - number ";
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
start();
}
});
}
private void start(){
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
i++;
str = str + t + i + "\n";
txt.setText(str);
sv.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}, 0, 100);
}
}
和布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.narb.log.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Start"
/>
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_below="@id/btn"
>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the log number"
android:layout_margin="15dp"
/>
</ScrollView>
</RelativeLayout>
而不是 str=str+t+i+"\n"; 和 txt.setText(str);
尝试使用 str=t+i+"\n"; 和 txt.setText(txt.getText()+str);
或者你也可以简单地使用 txt.setText(txt.getText()+t+i+"\n");
我的应用程序正在记录文本信息,我使用的技术基于嵌入到包含完整字符串的 ScrollView 中的 TextView。
此方法的真正缺点是字符串会随着时间的推移变得非常大,您可以体验到的最好情况是性能下降,如果不是因为系统内存不足而导致崩溃的话。
我可以使用哪些其他技巧?
这是我想出的代码:
public class MainActivity extends AppCompatActivity {
boolean run = true;
String str = "", t;
int i = 0;
TextView txt;
ScrollView sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.txt);
Button btn = (Button) findViewById(R.id.btn);
sv = (ScrollView) findViewById(R.id.sv);
t = "this is the log for monday the 24th of july 2017 - number ";
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
start();
}
});
}
private void start(){
new Timer().schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
i++;
str = str + t + i + "\n";
txt.setText(str);
sv.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}, 0, 100);
}
}
和布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.narb.log.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Start"
/>
<ScrollView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_below="@id/btn"
>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the log number"
android:layout_margin="15dp"
/>
</ScrollView>
</RelativeLayout>
而不是 str=str+t+i+"\n"; 和 txt.setText(str);
尝试使用 str=t+i+"\n"; 和 txt.setText(txt.getText()+str);
或者你也可以简单地使用 txt.setText(txt.getText()+t+i+"\n");