|
我们今天主要讲的就是一个很简单的实例,这个就是QQ对话框弹出的效果,这个效果是一个非常友好的效果,所以今天拿出来和大家分享一下。也不多说废话了我们现在就来看看主要的代码吧:
我们先来看看mainactivity: package dev.android;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView talkView;
private ArrayList<DetailEntity> list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
talkView = (ListView)findViewById(R.id.list);
list = new ArrayList<DetailEntity>();
DetailEntity d1 = new DetailEntity("我","2010-04-26","你好!",R.layout.list_say_me_item);
list.add(d1);
DetailEntity d2 = new DetailEntity("金贝贝","2010-04-26","你好!",R.layout.list_say_he_item);
list.add(d2);
DetailEntity d3 = new DetailEntity("金贝贝","2010-04-26","你是哪里人?",R.layout.list_say_he_item);
list.add(d3);
DetailEntity d4 = new DetailEntity("我","2010-04-26","辽宁抚顺!",R.layout.list_say_me_item);
list.add(d4);
talkView.setAdapter(new DetailAdapter(MainActivity.this, list));
}
}
我们在来看看DetailAdapter: package dev.android;
import java.util.ArrayList;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;
public class DetailAdapter implements ListAdapter{
private ArrayList<DetailEntity> coll;
private Context ctx;
public DetailAdapter(Context context ,ArrayList<DetailEntity> coll) {
ctx = context;
this.coll = coll;
}
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int arg0) {
return false;
}
public int getCount() {
return coll.size();
}
public Object getItem(int position) {
return coll.get(position);
}
public long getItemId(int position) {
return position;
}
public int getItemViewType(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
DetailEntity entity = coll.get(position);
int itemLayout = entity.getLayoutID();
LinearLayout layout = new LinearLayout(ctx);
LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi.inflate(itemLayout, layout,true);
TextView tvName = (TextView) layout.findViewById(R.id.messagedetail_row_name);
tvName.setText(entity.getName());
TextView tvDate = (TextView) layout.findViewById(R.id.messagedetail_row_date);
tvDate.setText(entity.getDate());
TextView tvText = (TextView) layout.findViewById(R.id.messagedetail_row_text);
tvText.setText(entity.getText());
return layout;
}
public int getViewTypeCount() {
return coll.size();
}
public boolean hasStableIds() {
return false;
}
public boolean isEmpty() {
return false;
}
public void registerDataSetObserver(DataSetObserver observer) {
}
public void unregisterDataSetObserver(DataSetObserver observer) {
}
}
我们现在来看看DetailEntity: package dev.android;
public class DetailEntity {
private String name;
private String date;
private String text;
private int layoutID;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getLayoutID() {
return layoutID;
}
public void setLayoutID(int layoutID) {
this.layoutID = layoutID;
}
public DetailEntity() {
}
public DetailEntity(String name, String date, String text, int layoutID) {
super();
this.name = name;
this.date = date;
this.text = text;
this.layoutID = layoutID;
}
}
我们先来看看list_say_me_item.xml <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
androidrientation="horizontal"
>
<ImageView
android:layout_width="42px"
android:layout_height="42px"
android:layout_gravity="bottom"
android:id="@+id/messagegedetail_rov_icon"
android:background="@drawable/retouxiang"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="249px"
android:layout_height="wrap_content"
android:background="@drawable/incoming"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="22dip"
>
<TextView
android:id="@+id/messagedetail_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingTop="2px"
android:textSize="18dip"
/>
<TextView
android:id="@+id/messagedetail_row_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingTop="2px"
android:textSize="16dip"
android:layout_marginLeft="60dip"
/>
</LinearLayout>
<TextView
android:id="@+id/messagedetail_row_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="2px"
android:textColor="#0000DD"
android:textSize="24dip"
/>
</LinearLayout>
</LinearLayout>
我们在来看看list_say_he_item.xml代码: <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="10px"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="249px"
android:layout_height="wrap_content"
android:background="@drawable/outgoing"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="22dip"
>
<TextView
android:id="@+id/messagedetail_row_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingTop="2px"
android:textSize="18dip"
/>
<TextView
android:id="@+id/messagedetail_row_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingTop="2px"
android:textSize="16dip"
android:layout_marginLeft="60dip"
/>
</LinearLayout>
<TextView
android:id="@+id/messagedetail_row_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="2px"
android:textColor="#0000DD"
android:textSize="24dip"
/>
</LinearLayout>
<ImageView
android:layout_width="42px"
android:layout_height="42px"
android:layout_gravity="bottom"
android:id="@+id/messagegedetail_rov_icon"
android:background="@drawable/retouxiang"
/>
</LinearLayout>
最后我们来看看Androidmanifest.xml代码: <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.android"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest> |
|