Android知识

Android UI 老虎机

2017-03-13  本文已影响0人  琼Kevin

今天完成的任务


1.作品展示

Screenshot_2017-03-12-14-10-43.png

2.需要掌握的知识


3.知识详解



4.项目代码

public class MainActivity extends AppCompatActivity {
    //1,声明控件LISTVIEW
    private ListView listView1;
    private ListView listView2;
    private ListView listView3;
    private Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //2,获取控件的id
        listView1 = (ListView)findViewById(R.id.list_item1);
        listView2 = (ListView)findViewById(R.id.list_item2);
        listView3 = (ListView)findViewById(R.id.list_item3);
        mContext = this;
        //3,进行绑定
        MyAdapter myAdapter = new MyAdapter();
        listView3.setAdapter(myAdapter);
        listView2.setAdapter(myAdapter);
        listView1.setAdapter(myAdapter);
    }
    //创建适配器类实现接口
    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return 50;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            //声明一个textview对象
            TextView view0 = null;
            //进行判断是否能过复用
            if(view != null){
                view0 = (TextView)view;
            }else {
                view0 = new TextView(mContext);
            }
            view0.setTextSize(40);
            Random random = new Random();
            int num = random.nextInt(100);
            if(num<20){
                view0.setText("桃");
                view0.setTextColor(Color.parseColor("#ff00ff"));
            }else if(num<40){
                view0.setText("梨");
                view0.setTextColor(Color.YELLOW);
            }else if(num<60){
                view0.setText("枣");
                view0.setTextColor(Color.RED);
            }else if(num<80){
                view0.setText("橘");
                view0.setTextColor(Color.parseColor("#d4824f"));
            }else{
                view0.setText("杏");
                view0.setTextColor(Color.parseColor("#00ff00"));
            }


            return view0;
        }
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.wenkai.tigerlistview.MainActivity">

    <ListView
        android:id="@+id/list_item1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <ListView
        android:id="@+id/list_item2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <ListView
        android:id="@+id/list_item3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

5.总结反思

上一篇 下一篇

猜你喜欢

热点阅读