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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
back_gradient.xml
----------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#EFEFEF"
android:endColor="#989898"
android:type="linear"
android:angle="270"/>
</shape>
add the following line with the rows in a listView
rowView.setBackgroundResource(R.drawable.back_gradient);
--------------------
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
MainActivity.java
--------------------
package com.example.custom_listview;
public class MainActivity extends Activity {
ListView lv;
private String[] ItemId={"Item1","Item2","Item3","Item4","Item5","Item6","Item7","Item8","Item9","Item10"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView1);
lv.setAdapter(new MyImageAdapter(this,ItemId));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,long id) {
// TODO Auto-generated method stub
String s=ItemId[position];
if(s.equalsIgnoreCase("Item1")){
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyImageAdapter.java
---------------------------
package com.example.custom_listview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
public class MyImageAdapter extends BaseAdapter {
private Context context;
private String[] itemId;
public MyImageAdapter(Context context, String[] itemId) {
this.context=context;
this.itemId=itemId;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemId.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.image_view, parent, false);
//rowView.setBackgroundResource(R.drawable.back_gradient);
//rowView.setBackgroundResource(R.drawable.back_gradient);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
textView.setText(itemId[position]);
String s = itemId[position];
if (s.startsWith("Item1")) {
imageView.setImageResource(R.drawable.img1);
}
if (s.startsWith("Item2")) {
imageView.setImageResource(R.drawable.img2);
}
if (s.startsWith("Item3")) {
imageView.setImageResource(R.drawable.img3);
}
if (s.startsWith("Item4")) {
imageView.setImageResource(R.drawable.img4);
}
if (s.startsWith("Item5")) {
imageView.setImageResource(R.drawable.img5);
}
if (s.startsWith("Item6")) {
imageView.setImageResource(R.drawable.img6);
}
if (s.startsWith("Item7")) {
imageView.setImageResource(R.drawable.img7);
}
if (s.startsWith("Item8")) {
imageView.setImageResource(R.drawable.img8);
}
if (s.startsWith("Item9")) {
imageView.setImageResource(R.drawable.img9);
}
if (s.startsWith("Item10")) {
imageView.setImageResource(R.drawable.img10);
}
/* else {
imageView.setImageResource(R.drawable.ic_launcher);
}*/
return rowView;
}
}
image_view.xml
--------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_marginTop="18dp"
android:src="@drawable/img1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginLeft="28dp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
</RelativeLayout>
back_gradient.xml
----------------------
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#EFEFEF"
android:endColor="#989898"
android:type="linear"
android:angle="270"/>
</shape>
add the following line with the rows in a listView
rowView.setBackgroundResource(R.drawable.back_gradient);
No comments:
Post a Comment