微信界面里面的联系人中,我们点击其中任意的Item就会出现不同的Activity。
这个需求的实现是在listView中的Adapter中实现的,在点击的事件被监听之后触发Intent携带一个值去启动一个新的Activity,携带的值决定了新的Activity的形态和属性。
总的来说,是Intent传值问题。
具体代码如下:
1、Adapter的java代码:
package com.example.app_weixin;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
/**
* Created by 尽途 on 2017/3/27.
*/
public class contactAdapter extends BaseAdapter {
private List<Map<String,Object>>contactdata;
private LayoutInflater minflater;
private Context context;
public contactAdapter(Context context,List<Map<String,Object>>list){
this.context=context;
this.minflater=LayoutInflater.from(context);
this.contactdata=list;
}
public final class ViewHoder{
public TextView userNameTextview;
public ImageView userImageview;
public RelativeLayout itemlayout;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getCount() {
return contactdata.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHoder hoder=null;
if (convertView==null){
hoder=new ViewHoder();
convertView=minflater.inflate(R.layout.item_contact,null);
hoder.userNameTextview=(TextView)convertView.findViewById(R.id.username_contact);
hoder.userImageview=(ImageView)convertView.findViewById(R.id.userImage_iv_contact);
hoder.itemlayout=(RelativeLayout)convertView.findViewById(R.id.item_contact);
convertView.setTag(hoder);
}else {
hoder=(ViewHoder)convertView.getTag();
}
hoder.userNameTextview.setText((String)contactdata.get(position).get("username"));
hoder.userImageview.setImageResource((Integer)contactdata.get(position).get("userimage"));
hoder.itemlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* 决定了新Activity的形态属性
*/
String strname=(String)contactdata.get(position).get("username");
switch (strname){
case "新的朋友":
Intent mintent=new Intent(context,Activity_addfriend.class);
mintent.putExtra("name",strname);
context.startActivity(mintent);
break;
case "群聊":
Intent qintent=new Intent(context,Activity_qunliao.class);
qintent.putExtra("name",strname);
context.startActivity(qintent);
break;
case "标签":
Intent lintent=new Intent(context,Activity_label.class);
lintent.putExtra("name",strname);
context.startActivity(lintent);
break;
case "公众号":
Intent pintent=new Intent(context,Activity_public.class);
pintent.putExtra("name",strname);
context.startActivity(pintent);
break;
default:
Intent cintent=new Intent(context,Activity_contact.class);
cintent.putExtra("name",strname);
context.startActivity(cintent);
break;
}
}
});
return convertView;
}
}
2、Adapter的Item的Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/item_contact"
android:clickable="true"
android:background="@drawable/find_background"
android:layout_width="fill_parent"
android:layout_height="55dp">
<ImageView
android:id="@+id/userImage_iv_contact"
android:scaleType="centerInside"
android:padding="8dp"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:src="@drawable/icon_addfriend"
android:layout_marginStart="5dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"/>
<LinearLayout
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_toEndOf="@+id/userImage_iv_contact">
<TextView
android:id="@+id/username_contact"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textColor="@color/colorBlack"
android:layout_gravity="center_vertical"
android:textSize="15sp"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
3、chatActivity的java代码
package com.example.app_weixin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class ChatActivity extends AppCompatActivity {
private ImageView backImageview;
private EditText editTextmessage;
private TextView textSendmessage;
private TextView nameTextview;
private ListView messageListview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
Intent mintent=getIntent();
String name=mintent.getStringExtra("username");
nameTextview.setText(name);
}
}
4、chatActivity中的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="fill_parent"
android:background="@color/colorLineBold"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/titleBar"
android:layout_width="match_parent"
android:background="@color/colorbackground"
android:layout_height="48dp"
android:orientation="horizontal">
<ImageView
android:padding="15dp"
android:id="@+id/back_iv"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="omBackground"
android:src="@drawable/icon_back"
android:background="@drawable/normalorpressed_background"
android:visibility="visible"/>
<TextView
android:id="@+id/name_tv"
android:layout_gravity="bottom"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="18sp"
android:textColor="#ffff" />
</LinearLayout>
</FrameLayout>
<ListView
android:layout_weight="1"
android:id="@+id/message_list"
android:divider="@null"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<LinearLayout
android:background="#ffff"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:textColor="@color/colorBlack"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/sendmessage"
android:clickable="true"
android:layout_gravity="center"
android:padding="12dp"
android:textSize="15sp"
android:background="@drawable/green_background"
android:text="发送"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>