Andorid+ ScrollView+ListView动态添加TextView ,点下一步可以内容下移背景色可以变,同时点播放按钮可以自动播放且显示背景色。

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_height="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
                        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;
    }

}

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值