第一行代码Android学习:第三部分主要涉及到简单的自定义控件和优化后ListView的写法
- 1.activity_main.xml
<RelativeLayout 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=".MainActivity" >
<com.example.dyhdm_02_00uicustomviews.TitleLayout
android:id="@+id/tl_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.dyhdm_02_00uicustomviews.TitleLayout>
<ListView
android:id="@+id/lv_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tl_title" >
</ListView>
</RelativeLayout>
- 2.item_test.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="horizontal" >
<TextView
android:id="@+id/tv1"
android:layout_marginRight="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
- 3.title.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="horizontal" >
<Button
android:id="@+id/bt_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回" >
</Button>
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="20dp"
android:text="标题" />
<Button
android:id="@+id/bt_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编辑" >
</Button>
</LinearLayout>
- 4.MainActivity.java
package com.example.dyhdm_02_00uicustomviews;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lv_test;
private TestAdapter adapter;
private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
for (int i = 0; i < 5; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("arg1", "111");
map.put("arg2", "222");
list.add(map);
}
lv_test = (ListView) findViewById(R.id.lv_test);
adapter = new TestAdapter(this, R.layout.item_test, list);
lv_test.setAdapter(adapter);
lv_test.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
}
});
}
}
- 5.TestAdapter.java
/*
* @Title: TestAdapter.java
* @Description: TODO
* @author: 张志安
* @data: 2016-8-15 下午2:00:49
*
*/
package com.example.dyhdm_02_00uicustomviews;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
* TODO 测试用的Adapter
*
* @author 张志安
* @date: 2016-8-15 下午2:00:49
*/
public class TestAdapter extends ArrayAdapter {
private int resourceId;
private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
/**
* <默认构造函数>
*/
public TestAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
list = objects;
}
/**
* 重载方法
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHold viewHold;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHold = new ViewHold();
viewHold.tv1 = (TextView) view.findViewById(R.id.tv1);
viewHold.tv2 = (TextView) view.findViewById(R.id.tv2);
view.setTag(viewHold);
} else {
view = convertView;
viewHold = (ViewHold) view.getTag();
}
viewHold.tv1.setText(list.get(position).get("arg1"));
viewHold.tv2.setText(list.get(position).get("arg2"));
return view;
}
class ViewHold {
TextView tv1;
TextView tv2;
}
}
- 6.TitleLayout.java
/*
* @Title: TitleLayout.java
* @Description: TODO
* @author: 张志安
* @data: 2016-8-15 上午10:48:00
*
*/
package com.example.dyhdm_02_00uicustomviews;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* TODO 自定义的控件
* @author 张志安
* @data: 2016-8-15 上午10:48:00
*/
public class TitleLayout extends LinearLayout {
/**
* <默认构造函数>
*/
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
Button bt_back = (Button) findViewById(R.id.bt_back);
Button bt_edit = (Button) findViewById(R.id.bt_edit);
bt_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
bt_edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Edit", Toast.LENGTH_SHORT).show();
}
});
}
}