<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context="com.example.pengtuanyuan.tigerdemo.MainActivity">
<ListView
android:id="@+id/tiger1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
</ListView>
<ListView
android:id="@+id/tiger2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
</ListView>
<ListView
android:id="@+id/tiger3"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
---------------------------------------------------------
package com.example.pengtuanyuan.tigerdemo;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this;
ListView tiger1= (ListView) findViewById(R.id.tiger1);
ListView tiger2= (ListView) findViewById(R.id.tiger2);
ListView tiger3= (ListView) findViewById(R.id.tiger3);
TigerAdapter tigerAdapter = new TigerAdapter();
tiger1.setAdapter(tigerAdapter);
tiger2.setAdapter(tigerAdapter);
tiger3.setAdapter(tigerAdapter);
}
class TigerAdapter extends BaseAdapter{
@Override
public int getCount() {
return 200;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view=null;
if (convertView!=null){
view=(TextView)convertView;
}else {
view = new TextView(mContext);
}
Random random=new Random();
int number = random.nextInt(100);
if (number<20){
view.setTextColor(Color.YELLOW);
view.setText("大象");
}else if(number<40){
view.setTextColor(Color.WHITE);
view.setText("老鼠");
}else if(number<60){
view.setTextColor(Color.RED);
view.setText("猫");
}else if(number<40){
view.setTextColor(Color.GRAY);
view.setText("豹子");
}else {
view.setTextColor(Color.GREEN);
view.setText("狮子");
}
view.setTextSize(25);
return view;
}
}
}
网友评论