main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
itemlist.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="vertical" >
<LinearLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
static final String TAG_NAME = "name";
static final String TAG_ID = "id";
static String selected = "";
ListView listView;
HashMap<String, String> map;
ArrayList<HashMap<String,String>> myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = new ArrayList<HashMap<String,String>>();
listView = (ListView) findViewById(R.id.listView1);
fillListView();
}
private void fillListView() {
myList.clear();
map = new HashMap<String, String>();
map.put(TAG_ID,"1");
map.put(TAG_NAME, "Name1");
myList.add(map);
map = new HashMap<String, String>();
map.put(TAG_ID,"2");
map.put(TAG_NAME, "Name2");
myList.add(map);
map = new HashMap<String, String>();
map.put(TAG_ID,"3");
map.put(TAG_NAME, "Name3");
myList.add(map);
map = new HashMap<String, String>();
map.put(TAG_ID,"4");
map.put(TAG_NAME, "Name4");
myList.add(map);
listView.setAdapter(new MyListAdapter(this, R.layout.itemlist, myList));
}
private class MyListAdapter extends BaseAdapter
{
private List<HashMap<String,String>> List;
private LayoutInflater mInflater;
private int layoutresource;
public MyListAdapter(Context context,int resource,List<HashMap<String, String>> myList) {
List = myList;
mInflater = LayoutInflater.from(context);
layoutresource = resource;
}
@Override
public int getCount() {
return List.size();
}
@Override
public Object getItem(int position) {
return List.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(layoutresource,null);
}
final View rootView = convertView.findViewById(R.id.root);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
final TextView name = (TextView) convertView.findViewById(R.id.textView1);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.profile));
name.setTextColor(Color.parseColor("#ffffff"));
name.setText(List.get(position).get(TAG_NAME));
if ( List.get(position).get(TAG_ID).equals(selected))
name.setTextColor(Color.parseColor("#ff0000"));
else
name.setTextColor(Color.parseColor("#ffffff"));
rootView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selected = List.get(position).get(TAG_ID);
fillListView();
}
});
name.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rootView.performClick();
}
});
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rootView.performClick();
}
});
return convertView;
}
}
}