Andorid+ ScrollView+ListView动态添加TextView ,点下一步可以内容下移背景色可以变,同时点播放按钮可以自动播放且显示背景色。
2017-12-31 16:58
1056 查看
1.总体架构为:
2效果图为
3.代码展示
layout_code.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_code"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="xxxx"
android:textSize="20sp"
/>
</LinearLayout>
layout_res.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="300dp"
android:scrollbars="vertical"
android:scrollbarThumbVertical="@color/white"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ListView
android:id="@+id/lv_item"
android:layout_width="match_parent"
android:layout_height="280dp"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_hei
4000
ght="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/bu1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一步"
android:layout_marginLeft="100dp"
/>
<Button
android:id="@+id/bu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</LinearLayout>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="gray">#999999</color>
<color name="white">#FFFFFF</color>
<color name="HHH">#CCFFCC</color>
</resources>
Code.java
package android.yichun.com.lesson_6.fragment;
/**
* Created by Lenovo on 2017/12/30.
*/
public class Code {
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
CodeActivity.java
package android.yichun.com.lesson_6.fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.yichun.com.lesson_6.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Lenovo on 2017/12/30.
*/
public class CodeActivity extends AppCompatActivity {
private Code code;
private ListView lv_code;
private List<Code> codes;
private int currentRow = 0;
private CodeAdapter codeAdapter;
private Button btn_next, btn_play;
String[] str={"void insert_sqlist(sqlink sq, datatype x, int i)","{","int j = 1;","if(isfull_sqlist(sq))","{"," printf(\"sqlist is full!\\n\");"," return;"," }","if(i<1 || i>getlength_sqlist(sq)+1)","{"," printf(\"i is not in the range!\\n\");"," return;"," }","for(j = getlength_sqlist(sq)-1; j>=i-1; j--)","{","sq->data[j+1] = sq->data[j];"," }","sq->data[i-1] = x;","++sq->length;","}"};
boolean isRun = false;
boolean isPlay = false;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
codeAdapter.notifyDataSetChanged();
//Toast.makeText(CodeActivity.this, "currentRow=" + currentRow + ", str.length=" + str.length, Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_res);
getData(str);
initView();
}
private void getData(String[] arr) {
codes = new ArrayList<>();
for(int i = 0; i < arr.length; i++){
Code c = new Code();
c.setCode(arr[i]);
codes.add(c);
}
}
private void initView() {
lv_code = (ListView) findViewById(R.id.lv_item);
codeAdapter= new CodeAdapter(this, codes);
lv_code.setAdapter(codeAdapter);
lv_code.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int postion, long l) {
currentRow = postion;
codeAdapter.notifyDataSetChanged();
isPlay = false;
// handler.sendEmptyMessage(1);
}
});
btn_next = (Button) findViewById(R.id.bu1);
btn_play = (Button) findViewById(R.id.bu2);
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentRow++;
codeAdapter.notifyDataSetChanged();
isPlay = false;
}
});
btn_play.setOnClickListener(new View.OnClickListener() {
//int index=str.length;
@Override
public void onClick(View view) {
// handler.sendEmptyMessage(1);
isPlay = true;
if(isRun == false){
new Thread(){
@Override
<
d6e1
/span>public void run() {
super.run();
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if((currentRow < str.length) && (isPlay == true)){
currentRow++;
handler.sendEmptyMessage(1);
}
}
}
}.start();
isRun = true;
}
}
});
}
class CodeAdapter extends BaseAdapter {
private List<Code> codes;
private Context context;
public CodeAdapter(Context context, List<Code> codes){
this.context = context;
this.codes = codes;
}
@Override
public int getCount() {
return codes.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder vh = null;
if(convertView == null){
convertView = getLayoutInflater().from(context).inflate(R.layout.layout_code, null);
vh = new ViewHolder();
vh.tv_code = (TextView) convertView.findViewById(R.id.tv_code);
if(position==0){
vh.tv_code.setBackgroundColor(getResources().getColor(R.color.gray));
}
convertView.setTag(vh);
}else{
vh = (ViewHolder) convertView.getTag();
}
Code e = codes.get(position);
//赋值
vh.tv_code.setText(e.getCode());
if(position == currentRow){
vh.tv_code.setBackgroundColor(getResources().getColor(R.color.HHH));
}else{
vh.tv_code.setBackgroundColor(getResources().getColor(R.color.white));
}
return convertView;
}
}
class ViewHolder{
public TextView tv_code;
}
}
相关文章推荐
- textView 自动换行, 可以文本内容动态移动
- textView 弹出键盘上面添加完成按钮,并设置输入内容的格式。
- android ExpandableTextView-自定义可以动态展开/收缩显示长文本的TextView
- scrollView +listView 不匹配,内容显示不全的问题
- android listview scroll滑屏显示sqlite分页,类似聊天记录,listview动态添加sqlite分页数据
- 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色
- android listview添加footview,有的手机可以正常显示,有的则不显示
- 动态地往datagirdview中添加数据,当所添加的数据超过datagridview的显示范围后,滚动条就无法显示,程序出现假死状态,(scrollbars=both)
- Androi实战—EidtText和TextView显示指定行数,内容过长自动换行问题
- android中Textview 和图片同时显示时,文字省略号显示,图片自动靠到右边
- textview中自动换行显示文本内容
- textview 弹出键盘上面添加完成按钮,并设置输入内容的格式。
- TextView部分内容改变颜色,背景色,大小,字体,超链接,删除线,下划线,添加图片
- TextView限定行数,点击按钮显示所有内容
- 用TextView动态显示文件内容
- Android HorizontalScrollView 水平滑动 在listview上面动态添加图片
- TextView自动换行以及内容过长全部显示--终极解决办法
- [已解决]AutoCompleteTextView 不显示匹配的内容,因为将空的内容添加进去了
- 在ListView中动态添加EditText并对其中的数据进行保存和由于复用引起的Editext中的内容显示异常的解决方案
- 安卓TextView限定行数最大值,点击按钮显示所有内容