最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈
前一部分学习了引入布局和自定义控件(点击打开链接),这部分我们主要介绍一下比较常用的自定义控件Listview的基本使用。本文源码示例连接
1,主要内容
这部分主要介绍一下,使用自定义控件ListView的基本使用以及其子项为自定义布局时的使用。
使用ListView需要注意一下几个要素点:
- (1)ListView子项:可以是系统自带的最简单的只展示一条文本的布局,也可以是自定义布局(主要)
- (2)适配器(Adapter):主要用于加载数据到布局上,可以理解为建立起数据层和显示层关系的媒介
- (3)android中MVC设计模式:不要诧异,我觉得ListView的使用很生动的反映了这样一种模式,自定义适配器类在创建实例时接收数据(Model),并完成子项自定义布局的加载(View),然后再在Activity当中实现数据显示的控制(Control);这其实就是Android当中的MVC设计模式的一个例子。
- (4)如何实现ListView子项中的点击事件:我们在上面也说到了,适配器是建立自定义布局和数据的媒介,所以很显然需要在适配器中来实现,我们自定义适配器都会重写一个getView()方法,在里面获取自定义布局的示例,在获取初始化适配器时传进来的数据,这样,当ListView显示到某一个子项时,对应的getView()方法就会执行从而实现子项布局的动态加载。
- (5)提升ListView运行效率:因为ListView中子项的数据通常全部或者大部分都是固定的,这样在滑动ListView时如何还要实时不停的执行加载布局以及其中的控件的操作会很浪费资源;于是,就提出了缓存相应数据来提升ListView的运行效率。这个时候就需要我们在获取子项自定义布局实例前进行一下判断,然后再在自定义适配器类中添加一个内部类来保存子项控件实例。
其实这些东西可能在初期并没有实现来的重要,因为那毕竟是基础;但是思想上的东西在很多地方都是统一有规律的,个人觉得对于学习来说要重要的多。
2,示例代码
说了很多,感觉还是很模糊,但是所说的绝对都是很重要的东西,下面就给出具体的实现。其实我还想说内容有点杂,建议下载源码学习。
FirstListViewActivity.java代码:
package com.hfut.operationlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FirstListViewActivity extends AppCompatActivity {
private ListView listView;
private String[] data={"why","jr","Tom","Jerry","why","jr","Tom","Jerry",
"why","jr","Tom","Jerry","why","jr","Tom","Jerry",
"why","jr","Tom","Jerry", "why","jr","Tom","Jerry",
"why","jr","Tom","Jerry", "why","jr","Tom","Jerry"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(FirstListViewActivity.this,android.R.layout.simple_list_item_1,data);
listView=findViewById(R.id.item_list);
listView.setAdapter(adapter);
}
}
JerryActivity.java代码:
package com.hfut.operationlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class JerryActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jerry);
webView=findViewById(R.id.jerry_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://baike.baidu.com/item/%E7%8C%AB%E5%92%8C%E8%80%81%E9%BC%A0/313885");
}
}
JrActivity.java代码:
package com.hfut.operationlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class JrActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jr);
webView=findViewById(R.id.jr_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://baike.baidu.com/item/%E7%BE%8E%E5%A5%B3/109596?fr=aladdin");
}
}
SecondListViewActivity.java代码:
package com.hfut.operationlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
import java.util.ArrayList;
public class SecondListViewActivity extends AppCompatActivity {
private List<Student> studentClass=new ArrayList<Student>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initStudents(studentClass);
StudentAdapter adapter=new StudentAdapter(SecondListViewActivity.this,R.layout.studentlist,studentClass);
ListView studentList=findViewById(R.id.studentList);
studentList.setAdapter(adapter);
studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Student student=studentClass.get(position);
Toast.makeText(SecondListViewActivity.this,"你查看了"+student.getName()+"的信息",Toast.LENGTH_LONG).show();
}
});
}
private void initStudents(List<Student> studentClass) {
for (int i = 0; i < 8; i++) {
Student why = new Student("why", R.drawable.why);
Student jr = new Student("jr", R.drawable.jr);
Student tom = new Student("Tom", R.drawable.tom);
Student jerry = new Student("Jerry", R.drawable.jerry);
studentClass.add(why);
studentClass.add(jr);
studentClass.add(tom);
studentClass.add(jerry);
}
}
private ArrayList<Student> addItem(List<Student> studentClass,Student student) {
studentClass.add(student);
return (ArrayList<Student>) studentClass;
}
}
Student.java代码:
package com.hfut.operationlistview;
/**
* author:why
* created on: 2018/4/5 11:03
* description:
*/
public class Student {
String name;
int pictureID;
public Student(String name,int pictureID){
this.name=name;
this.pictureID=pictureID;
}
public String getName(){
return name;
}
public int getPictureID(){
return pictureID;
}
}
StudentAdapter.java代码:
package com.hfut.operationlistview;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* author:why
* created on: 2018/4/5 11:08
* description:
*/
public class StudentAdapter extends ArrayAdapter<Student> {
private static final String TAG = "StudentAdapter";
int resourceID;
Context context;
public StudentAdapter(@NonNull Context context, int resource, List<Student> list) {
super(context, resource, list);
//获取原来ArrayAdapter中textView对应的id
this.resourceID = resource;
this.context=context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final Student student = getItem(position);
View view;
final ViewHolder holder;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceID, parent, false);
//布局中控件的缓存处holder = new ViewHolder();
holder=new ViewHolder();
holder.imageView = view.findViewById(R.id.image);
holder.textView = view.findViewById(R.id.name);
holder.button=view.findViewById(R.id.ckeck);
view.setTag(holder);
} else {
//布局缓存处理
view = convertView;
holder = (ViewHolder) view.getTag();
}
holder.imageView.setImageResource(student.getPictureID());
holder.textView.setText(student.getName());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=student.getName();
Log.i(TAG, "onClick: "+name);
if(name.equals("why")){
Intent intent=new Intent(context,WhyActivity.class);
context.startActivity(intent);
}
else if (name.equals("jr")){
Intent intent=new Intent(context,JrActivity.class);
context.startActivity(intent);
}
else if(name.equals("Tom")){
Intent intent=new Intent(context,TomActivity.class);
context.startActivity(intent);
}
else{
Intent intent=new Intent(context,JerryActivity.class);
context.startActivity(intent);
}
}
});
return view;
}
class ViewHolder {
ImageView imageView;
TextView textView;
Button button;
}
}
TestActivity.java代码:
package com.hfut.operationlistview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class TestActivity extends AppCompatActivity {
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
public void firstListView(View view) {
intent = new Intent(this, FirstListViewActivity.class);
startActivity(intent);
}
public void secondListView(View view){
intent=new Intent(this,SecondListViewActivity.class);
startActivity(intent);
}
}
TomActivity.java代码:
package com.hfut.operationlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class TomActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tom);
webView=findViewById(R.id.tom_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://baike.baidu.com/item/%E6%B1%A4%E5%A7%86/28277?fr=aladdin&fromid=15983483&fromtitle=TOM");
}
}
WhyActivity.java代码:
package com.hfut.operationlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WhyActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_why);
webView=findViewById(R.id.why_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://baike.baidu.com/item/why/73467?fr=aladdin");
}
}
activity_jerry.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.hfut.operationlistview.JerryActivity">
<WebView
android:id="@+id/jerry_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</android.support.constraint.ConstraintLayout>
activity_jr.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.hfut.operationlistview.JrActivity">
<WebView
android:id="@+id/jr_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</android.support.constraint.ConstraintLayout>
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"
android:orientation="vertical"
tools:context="com.hfut.operationlistview.FirstListViewActivity">
<ListView
android:id="@+id/item_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
activity_second.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"
android:orientation="vertical"
tools:context="com.hfut.operationlistview.SecondListViewActivity">
<ListView
android:id="@+id/studentList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
activity_test.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"
android:orientation="vertical"
tools:context="com.hfut.operationlistview.TestActivity">
<Button
android:onClick="firstListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textSize="20dp"
android:textAllCaps="false"
android:text="简单的ListView"/>
<Button
android:onClick="secondListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:textAllCaps="false"
android:text="自定义ListView子项布局"/>
</LinearLayout>
activity_tom.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.hfut.operationlistview.TomActivity">
<WebView
android:id="@+id/tom_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</android.support.constraint.ConstraintLayout>
activity_why.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.hfut.operationlistview.WhyActivity">
<WebView
android:id="@+id/why_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</android.support.constraint.ConstraintLayout>
studentlist.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">
<ImageView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp" />
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="100dp"
android:id="@+id/name"
android:textSize="20dp"
android:gravity="center"
android:layout_width="80dp"
android:layout_height="80dp" />
<Button
android:layout_marginTop="10dp"
android:layout_marginLeft="80dp"
android:textSize="20dp"
android:text="查看信息"
android:id="@+id/ckeck"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
主配置文件AndroidManifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hfut.operationlistview">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".TestActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FirstListViewActivity" />
<activity android:name=".SecondListViewActivity" />
<activity android:name=".WhyActivity" />
<activity android:name=".JrActivity" />
<activity android:name=".TomActivity" />
<activity android:name=".JerryActivity"></activity>
</application>
</manifest>
3,运行结果
总结:这部分主要介绍了ListView的基本用法,记住自定义布局(studentlist.xml,其实叫studentitem.xml更好,哎),自定义数据类型(Student),自定义适配器StudentAdapter以及它们与活动的关系就很简单了,算了,我还是项画一个图来表达。
注:欢迎扫码关注