一、实验要求和目的
- 掌握Android中ListView控件的使用;
- 掌握各种Adapter封装数据的方法;
- 能够熟练应用各种布局管理器和控件进行界面设计。
二、实验环境 - 部署有Android Studio和Android SDK的主机;
- 建议在机房的HelloWorld例子上完成。
三、上机操作参考步骤
1、在layout文件夹的布局文件activity_main.xml中设计如下界面。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/listview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击"
></Button>
</LinearLayout>
MainActivity.java
package com.example.shiyan4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import com.example.shiyan4.listview.ListViewActivity;
public class MainActivity extends AppCompatActivity {
private Button listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=findViewById(R.id.listview1);
listview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, ListViewActivity.class);
startActivity(intent);
}
});
}
}
ListViewActivity
package com.example.shiyan4.listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import androidx.annotation.Nullable;
import com.example.shiyan4.R;
public class ListViewActivity extends Activity {
private ListView mLv1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mLv1=findViewById(R.id.lv_1);
mLv1.setAdapter(new MyListAdapter(ListViewActivity.this));
}
}
layout_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<ImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:background="#000"
android:src="@drawable/shuixing">
</ImageView>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水星"
android:textSize="30sp"
android:textColor="#E91E63"
android:layout_toRightOf="@id/iv"
>
</TextView>
<TextView
android:id="@+id/tvcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="水星真不小奥,离太阳近wsx20177710229"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/iv"
android:layout_below="@+id/tv_title"
></TextView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/iv1"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:background="#000"
android:src="@drawable/jinxing"
android:layout_marginTop="110dp">
</ImageView>
<TextView
android:id="@+id/tv_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="金星"
android:textSize="30sp"
android:textColor="#E91E63"
android:layout_toRightOf="@id/iv1"
android:layout_marginTop="110dp"
>
</TextView>
<TextView
android:id="@+id/tvcontent1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="金星真不小奥,离太阳近wsx2017771029"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/iv1"
android:layout_below="@+id/tv_title1"
></TextView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/iv2"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:background="#000"
android:src="@drawable/diqiu"
android:layout_marginTop="220dp">
</ImageView>
<TextView
android:id="@+id/tv_title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="地球"
android:textSize="30sp"
android:textColor="#E91E63"
android:layout_toRightOf="@id/iv2"
android:layout_marginTop="220dp"
>
</TextView>
<TextView
android:id="@+id/tvcontent2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="后边太多了不想写了"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/iv2"
android:layout_below="@+id/tv_title2"
></TextView>
</RelativeLayout>
</RelativeLayout>
MylistAdapter.java
package com.example.shiyan4.listview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.shiyan4.R;
public class MyListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mLayouyInflater;
public MyListAdapter(Context context){
this.mContext=context;
mLayouyInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return 1;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHoder{
public ImageView imageView;
public TextView tv_title,tvcontent;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHoder holder=null;
if(convertView==null)
{
convertView=mLayouyInflater.inflate(R.layout.layout_list_item,null);
holder=new ViewHoder();
holder.imageView=convertView.findViewById(R.id.iv);
holder.tv_title=convertView.findViewById(R.id.tv_title);
holder.tvcontent=convertView.findViewById(R.id.tvcontent);
convertView.setTag(holder);
}
else {
holder=(ViewHoder) convertView.getTag();
}
return convertView;
}
}
activity_listview.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">
<ListView
android:id="@+id/lv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>