您的位置:首页 > 移动开发 > Android开发

Android UI 实现老虎机详解及实例代码

2017-03-24 08:50 786 查看

Android UI 实现老虎机详解

  1. listview 的使用步骤
  2. 简单的listview老虎机实现

1.实现效果图

2.需要掌握的知识

  • listview的使用步骤
  • listview的Adapter接口的实现
  • listview中的MVC

3.知识详解

ListView 是一个控件,一个在垂直滚动的列表中显示条目的一个控件,这些条目的内容来自于一个ListAdapter 。EditText Button TextView ImageView Checkbox 五大布局。

1.布局添加Listview
2.找到listview
3.创建一个Adapter适配器继承BaseAdapter,封装4个方法,其中getcount,getview必须封装
getcount:告诉listview要显示的条目数
getview:告诉listview每个条目显示的内容。
4.创建Adapter的一个对象,设置给listview。
listview.setAdapter(ListAdapter adapter);

listview优化

adapter中getview方法会传进来一个convertView,convertView是指曾经使用过的view对象,可以被重复使用,但是在使用前需要判断是否为空,不为空直接复用,并作为getview方法的返回对象。

TextView view = null;
if(convertView != null){//判断converView是否为空,不为空重新使用
view = (TextView) convertView;
}else{
view = new TextView(mContext);//创建一个textView对象
}
return view;

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>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android UI 老虎机