Custom ListView with multiple click in widgets with holder
Today I am sharing new tutorial for clicking multiple widgets on a single list view item.
- Create a new project in the activity_main.xml,copy the below code:
<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"
tools:context=".MainActivity"
android:background="@drawable/bg" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:cacheColorHint="#00000000"
android:divider="@android:color/transparent"
android:listSelector="@android:color/transparent" >
</ListView>
</RelativeLayout>
- Now create a new xml layout named : row_layout.xml. And copy the below code in that:
<?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" >
<RelativeLayout
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:background="@drawable/border_background" >
<TextView
android:id="@+id/textproduct_editdeals"
android:layout_width="match_parent"
android:maxLines="2"
android:layout_height="wrap_content"
android:layout_marginRight="80dp"
android:layout_centerVertical="true"
android:text="Medium Text"
android:textSize="15dp"
android:textColor="@android:color/black"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/imagedelete_editdeals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:background="@drawable/delete"
/>
<Button
android:id="@+id/imageedit_editdeals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/imagedelete_editdeals"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:background="@drawable/edit"
/>
</RelativeLayout>
</LinearLayout>
- Then move to the src folder in that make an adapter named CustomListAdapter.java ,in that class follow the below code:
package com.example.customlistviewblog;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class CustomListAdapter extends BaseAdapter{
Context mContext;
String[] mstrArray = new String[5];
public CustomListAdapter(Context applicationContext, String[] strArray) {
this.mContext = applicationContext;
this.mstrArray = strArray;
}
static class ViewHolder {
public TextView text;
public Button btn_view,btn_edit,btn_delete;
public RelativeLayout layout_parent;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mstrArray.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mstrArray[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, null);
holder = new ViewHolder();
holder.layout_parent = (RelativeLayout)convertView.findViewById(R.id.relative_layout);
holder.text = (TextView) convertView.findViewById(R.id.textproduct_editdeals);
holder.btn_delete = (Button)convertView.findViewById(R.id.imagedelete_editdeals);
holder.btn_edit = (Button)convertView.findViewById(R.id.imageedit_editdeals);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.text.setText(mstrArray[position]);
holder.layout_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"Layout>"+position,Toast.LENGTH_LONG).show();
}
});
holder.btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(mContext,"Delete>"+position,Toast.LENGTH_LONG).show();
}
});
holder.btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(mContext,"Edit>"+position,Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
- Next step is to move to the MainActivity.java, write the following code:
package com.example.customlistviewblog;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView list;
CustomListAdapter adapter;
String[] strArray = new String[]{"Product 0","Product 1","Product 2","Product 3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.listView1);
adapter = new CustomListAdapter(getApplicationContext(),strArray);
list.setAdapter(adapter);
}
}
- Now run the project.
No comments:
Post a Comment