最近討論了一個(gè)項(xiàng)目需求,在ListView的Item中放置了一個(gè)類(lèi)似電話的圖標(biāo),點(diǎn)擊圖標(biāo)可以將號(hào)碼調(diào)到撥號(hào)界面。實(shí)現(xiàn)起來(lái)很是容易,原理也易懂,較為實(shí)用,項(xiàng)目中有需要的可以直接引入。 我模擬了一個(gè)簡(jiǎn)單的demo.代碼如下:
1.ListAdapter.java:
package com.example.listviewphone;
//省略import
public class ListAdapter extends BaseAdapter {
private List<Test> tests;
private Context context;
LayoutInflater layoutInflater;
public ListAdapter(Context context,List<Test> tests){
this.tests=tests;
this.context=context;
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return tests.size();
}
@Override
public Object getItem(int position) {
return tests.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
if(convertView==null){
viewHolder=new ViewHolder();
convertView=layoutInflater.inflate(R.layout.item_list, null);
viewHolder.mTitleLisTextView=(TextView)convertView.findViewById(R.id.tv_title_list);
viewHolder.mPhoneTextView=(TextView)convertView.findViewById(R.id.tv_phone_list);
convertView.setTag(viewHolder);
}else {
viewHolder=(ViewHolder) convertView.getTag();
} viewHolder.mTitleLisTextView.setText(tests.get(position).getTitle_lost());
viewHolder.mPhoneTextView.setText(tests.get(position).getPhone_lost());
viewHolder.mPhoneTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+tests.get(position).getPhone_lost())); //直接撥打電話,較為暴利,慎用!
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+tests.get(position).getPhone_lost())); //跳轉(zhuǎn)到用戶界面較為溫和,推薦使用! context.startActivity(intent);
}
});
return convertView;
}
class ViewHolder {
private TextView mPhoneTextView;
private TextView mTitleLisTextView;
ViewHolder() {
}
}
}
ListAdapter 繼承自BaseAdapter,構(gòu)造方法傳入上下文對(duì)象context和數(shù)據(jù)List-tests。覆寫(xiě)了getCount方法,返回?cái)?shù)據(jù)個(gè)數(shù),覆寫(xiě)了getItem返回指定下表對(duì)象,覆寫(xiě)了getItemId返回指定欄目下標(biāo)。覆寫(xiě)了getView方法,返回view,在getView方法中實(shí)現(xiàn)了子欄目的單擊事件監(jiān)聽(tīng),結(jié)合系統(tǒng)的Intent.ACTION_DIAL,進(jìn)行電話功能的調(diào)取。
2.javabean—Test.java:
package com.example.listviewphone;
public class Test
{
private String content_test;
private String phone_test;
private String title_test;
private String username;
public String getContent_test() {
return content_test;
}
public void setContent_test(String content_test) {
this.content_test = content_test;
}
public String getPhone_test() {
return phone_test;
}
public void setPhone_test(String phone_test) {
this.phone_test = phone_test;
}
public String getTitle_test() {
return title_test;
}
public void setTitle_test(String title_test) {
this.title_test = title_test;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
為了便于操作,封裝了Test類(lèi)。
3.MainActivity.java:
package com.example.listviewphone;
//省略了import
public class MainActivity extends Activity {
private ListView mListView;
private ListAdapter adapter;
private List<Test> tests;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mListView=(ListView)findViewById(R.id.listview);
initDatas();
adapter=new ListAdapter(this, tests);
mListView.setAdapter(adapter);
}
private void initDatas() {
tests=new ArrayList<Test>();
for (int i = 0; i < 30; i++) {
Test test =new Test();
test.setTitle_test("電話");
test.setPhone_test("123456789"+i);
tests.add(test);
}
}
}
下面是簡(jiǎn)單的兩個(gè)布局文件:
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" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
2.item_list.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="wrap_content"
android:padding="10dp"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_title_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電話"
android:singleLine="true"
>
</TextView>
<TextView
android:id="@+id/tv_phone_list"
android:textSize="12sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@color/green"
android:text="138024249542"
android:padding="4dp"
android:layout_marginLeft="140dp"
android:drawableLeft="@drawable/icon_photo" >
</TextView>
</LinearLayout>
運(yùn)行實(shí)例如下:
點(diǎn)擊一個(gè):
成功跳轉(zhuǎn)到撥號(hào)界面,并將對(duì)應(yīng)的電話號(hào)碼傳了過(guò)來(lái)。