Android Gridview Smooth scrolling with button - java

I am currently making a part of my app to select multiple images from gallery using Gridview. The PROBLEM is that I need to put a button on the first item and the rest will be images (the 0,0 will be button and the rest well, imageview with check box) That button will be using the phone camera. I was able to make it, but in exchange, it is not scrolling smoothly, is there a way to fix this? below is my code:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view;
System.out.println(position);
if (position == 0) {
holder = new ViewHolder();
view = getLayoutInflater().inflate(R.layout.year_dropdownlayout, parent, false);
holder.mButton = (Button) view.findViewById(R.id.mfreakingBtn);
ViewGroup.LayoutParams params = holder.mButton.getLayoutParams();
params.height = 250;
params.width = 250;
holder.mButton.requestLayout();
} else {
view = getLayoutInflater().inflate(R.layout.select_photos_layout, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) view.findViewById(R.id.imageView1);
holder.mCheckBox = (CheckBox) view.findViewById(R.id.checkBox1);
holder.mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
holder.mButton = null;
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "You Clicked " + position, Toast.LENGTH_LONG).show();
}
});
imageLoader.displayImage("file://" + imageUrls.get(position), holder.imageView, options, new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
Animation anim = AnimationUtils.loadAnimation(MultiPhotoSelect.this, R.anim.fade_in);
holder.imageView.setAnimation(anim);
anim.start();
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
#Override
public void onProgressUpdate(String imageUri, View view, int current,
int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
}
);
holder.mCheckBox.setTag(position);
holder.mCheckBox.setChecked(mSparseBooleanArray.get(position));
}
return view;
}
Don't mind the names of the layouts. :) This what happens when frustration gets to ya. I have been looking for hours now. I found one who put a button on the end of the gridview this: Android - Different items in GridView not showing up
I hope someone can help me. Thanks in advance. :)

Your adapter...
private static final int ItemTypeButton = 0;
private static final int ItemTypeImage = 1;
#override
public int getViewTypeCount()
{
return 2; // 2 because you want two type of view. (One button and the rest is images)
}
#override
public int getItemViewType(int position)
{
/* it's better to use item view type
to determine what type of view
you want to display. This is used
for recycling views for optimization. */
return position == 0 ? ItemTypeButton : ItemTypeImage;
}
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
/* Check whether convertView intance is null. If null, then inflate.
Otherwise the view is recycled for optimization. */
if (convertView == null)
{
if (type == ItemTypeButton)
{
// inflate button view
}
else if (type == ItemTypeImage)
{
// inflate your view for image
}
}
// This is where you should update your view states.
if (type == ItemTypeImage)
{
/* ... */
holder.imageView = (ImageView) view.findViewById(R.id.imageView1);
holder.imageView.setImage(.....
}
else
{
/* ... */
}
}

Related

How to set different views dynamically in getView() of adapter in Android

I am creating a submit form by getting data from server, this form may have different views (EditText, CheckBox, RadioButton, RatingBar) according what the server suggest. I have successfully created that view in adapter but every time I scroll down or up the user's answer get cleared (like checked Item get unchecked automatically, EditText get cleared etc) and also the views get automatically exchanged their position from first to last or just get swapped. I don't know how to solve that, here is my getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.customratinglist, parent, false);
final ViewHolder holder = new ViewHolder();
holder.tvQuestion = (TextView) rowView.findViewById(R.id.txtquestion);
holder.tvQuestion.setTag(list.get(position));
holder.layOut = (LinearLayout) rowView.findViewById(R.id.lnQuestion);
holder.layOut.setTag(position);
rowView.setTag(holder);
} else {
rowView = convertView;
((ViewHolder) rowView.getTag()).layOut.setTag(position);
}
final ViewHolder viewHolder = (ViewHolder) rowView.getTag();
viewHolder.tvQuestion.setTypeface(font);
viewHolder.tvQuestion.setText(list.get(position).getQuestion());
viewHolder.layOut.removeAllViews();
switch (list.get(position).getAns_type_id()) {
case "1":
ratingBar = new RatingBar(context);
ratingBar.setNumStars(5);
ratingBar.setStepSize(0.1f);
ratingBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
viewHolder.layOut.addView(ratingBar);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
answerSetter((int) (viewHolder.layOut.getTag()), String.valueOf(rating));
}
});
break;
case "2":
editText = new EditText(context);
editText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
editText.setTextColor(Color.BLACK);
viewHolder.layOut.addView(editText);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
answerSetter((int) (viewHolder.layOut.getTag()), String.valueOf(s));
}
#Override
public void afterTextChanged(Editable s) {
}
});
break;
}
return rowView;
}
The solution is a using these two methods :
#Override
public int getItemViewType(int position) {
return Integer.parseInt(list.get(position).getAns_type_id());
}
#Override
public int getViewTypeCount() {
return 2;
}
After that you can use getItemViewType method inside of getView method with position which is passed as param in getView method.
For more info please take a look for this tutorial
Here is documentation for these methods.
u can try like this,
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = getActivity().getLayoutInflater();
View row;
switch (Maintain_List.get(position))
{
case 1:
row = inflater.inflate(R.layout.row, parent, false);
TextView tv = (TextView) row.findViewById(R.id.row_title);
tv.setText("" + sliding_menu_text.get(position));
ImageView iv1 = (ImageView) row.findViewById(R.id.row_image);
iv1.setImageResource(sliding_menu_icon.get(position));
break;
case 2:
row = inflater.inflate(R.layout.row2, parent, false);
break;
case 3:
row = inflater.inflate(R.layout.row3, parent, false);
TextView tv2 = (TextView) row.findViewById(R.id.tv_of_row3);
tv2.setText("" + sliding_menu_text.get(position));
break;
case 4:
row = inflater.inflate(R.layout.refresh_category, parent, false);
final Button b = (Button) row.findViewById(R.id.refresh_btn_of_refresh_category);
final WebView wv = (WebView) row.findViewById(R.id.wv_of_refresh_category);
b.setVisibility(View.VISIBLE);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
NetworkCheck nc = new NetworkCheck(getActivity());
isInternetPresent = nc.isConnectingToInternet();
if (!isInternetPresent)
{
// Ntwork not avilabale
Toast.makeText(getActivity(), "Connection Error !!", Toast.LENGTH_SHORT).show();
}
else
{
// Ntwork Avilable
wv.loadUrl("file:///android_asset/m.html");
wv.setBackgroundColor(Color.TRANSPARENT);
wv.setVisibility(View.VISIBLE);
b.setVisibility(View.INVISIBLE);
new get_List_of_Drawer().execute();
}
}
});
break;
default:
row = null;
break;
}
return row;
}
works fine for me...!!!

GridView setOnItemClickListener effects multiple items

When I click the GridView item multiple checks appear where I just want it to appear in the one. I'm pretty sure this has something to do with the recycled views, but I am not sure how to prevent it from happening. Any advice is appreciated.
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
ImageView check = (ImageView) view.findViewById(R.id.check);
if (check.getVisibility() == View.GONE) {
check.setVisibility(View.VISIBLE);
I
}
else {
check.setVisibility(View.GONE);
}
}
});
Here is the adapter
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_item, null);
} else {
grid = (View) convertView;
}
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
final ProgressBar pb = (ProgressBar) grid.findViewById(R.id.progressbar);
final ImageView check = (ImageView) grid.findViewById(R.id.check);
Ion.with(mContext).load(objects.get(position).getThumnailURL())
.progressBar(pb).withBitmap()
.error(R.drawable.ic_launcher).intoImageView(imageView).setCallback(new FutureCallback<ImageView>() {
#Override
public void onCompleted(Exception e, ImageView file) {
pb.setVisibility(View.GONE);
}
});
return grid;
}
You can do it like this:
put a boolean variable in your object that is passed to your adapter(ArrayList) and control the visibility of check boxes by this variable so if onitemclick is called toggle that variable and call notifydatasetchange. In the getView set each check boxes by the value of this variable. in this way if you scroll the gridview your value of your check boxes do not get lost.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder _holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_item, null);
_holder.imageView = (ImageView) grid.findViewById(R.id.grid_image);
_holder.pb = (ProgressBar) grid.findViewById(R.id.progressbar);
_holder.check = (ImageView) grid.findViewById(R.id.check);
convertView.setTag(_holder);
}
else {
_holder = (ViewHolder) convertView.getTag();
}
//here set your values
Ion.with(mContext).load(objects.get(position).getThumnailURL())
.progressBar(pb).withBitmap()
.error(R.drawable.ic_launcher).intoImageView(imageView).setCallback(new FutureCallback<ImageView>() {
#Override
public void onCompleted(Exception e, ImageView file) {
pb.setVisibility(View.GONE);
}
});
return grid;
}
class ViewHolder {
ImageView imageView,check;
ProgressBar pb;
}
Try this !!

Set onClickListener into custom adapter

Hello at all the community,
i've this adapter with 2 button and 2 textview.
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
holder = new Holder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_adapter, null);
holder.result = (TextView)view.findViewById(R.id.description);
holder.value = (TextView)view.findViewById(R.id.value);
holder.add = (Button)view.findViewById(R.id.add);
holder.subtract = (Button)view.findViewById(R.id.subtract);
myObject = getItem(position);
holder.result.setText(myObject.result);
holder.value.setText(myObject.value);
view.setTag(holder);
} else {
holder = (Holder)view.getTag();
}
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
}
});
return view;
}
Now my question is: if I want that when the user press on add button set the text of the textview with (for example) 5 how can i do this? If I put into the onCLick method
holder.result.setText("My text") set the text of the last textview and not the correspond textview of the selected row item (I disabled the click on the listview with):
#Override
public boolean isEnabled(int position) {
return false;
}
Is there any solution for my problem?
You should put your code in the getView method inside the adapter and remember to use references to the current Button/TextVeiw so that each Button would correspond to that specific TextView
P.S. I found something with your code check this out:
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
holder = new Holder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_adapter, null);
holder.result = (TextView)view.findViewById(R.id.description);
holder.value = (TextView)view.findViewById(R.id.value);
holder.add = (Button)view.findViewById(R.id.add);
holder.subtract = (Button)view.findViewById(R.id.subtract);
view.setTag(holder);
} else {
holder = (Holder)view.getTag();
}
myObject = getItem(position);
holder.result.setText(myObject.result);
holder.value.setText(myObject.value);
final TextView tv = holder.result;
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
tv.setText("bla bla");
}
});
return view;
}
You mixed something up, the getItem has to be below the view creation stuff. First create the view, then set the values.
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
holder = new Holder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.my_adapter, null);
holder.result = (TextView)view.findViewById(R.id.description);
holder.value = (TextView)view.findViewById(R.id.value);
holder.add = (Button)view.findViewById(R.id.add);
holder.subtract = (Button)view.findViewById(R.id.subtract);
view.setTag(holder);
} else {
holder = (Holder)view.getTag();
}
myObject = getItem(position);
holder.result.setText(myObject.result);
holder.value.setText(myObject.value);
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//What should happen here?
}
});
return view;
}

while scrolling, ListView Listview item changes the position.Why?

When i am scrolling ListView position of the item changes.My ListView contain 2 Button and 2 TextView. These 2 Button are initially invisible. when user Long click on item i just set the visibility.Its working but when i scroll the ListView ,it changes the position.Why?
I am setting this visibility and invisibility code inside of my adapter class..
here is my code :::
#Override
public View getView(int position, View convertView, ViewGroup parent) {
holder = new ViewHolder();
if (convertView == null) {
convertView = lInflater.inflate(R.layout.message_row, null);
holder.messageDeleteButton = (Button) convertView
.findViewById(R.id.delete_button);
holder.messageTextView = (TextView) convertView
.findViewById(R.id.message);
holder.timeTextView = (TextView) convertView
.findViewById(R.id.time);
holder.copyMessageButton = (ImageButton) convertView
.findViewById(R.id.copy_bt);
holder.messageDeleteButton
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
System.out.println("Delete clicked");
}
});
convertView.setTag(R.layout.message_row, holder);
} else {
holder = (ViewHolder) convertView.getTag(R.layout.message_row);
}
convertView.setTag(position);
holder.messageDeleteButton.setTag(position);
if (mMessageData.get(position).isVisible())
holder.messageDeleteButton.setVisibility(View.VISIBLE);
else
holder.messageDeleteButton.setVisibility(View.GONE);
holder.messageTextView.setText(mMessageData.get(position)
.getMessage_text());
holder.messageTextView.setTypeface(Typeface.createFromAsset(
mContext.getAssets(), "Existence-Light.ttf"));
holder.timeTextView.setText(mMessageData.get(position)
.getMessage_date());
holder.timeTextView.setTypeface(Typeface.createFromAsset(
mContext.getAssets(), "Existence-Light.ttf"));
convertView.setOnTouchListener(mOnTouchListener);
convertView.setOnClickListener(mOnClickListener);
// convertView.setOnLongClickListener(mLongClick);
return convertView;
}
This is my OnTouchListiner code it will working fine.But when i scroll the ListView the buttons positions will changed..
class MyTouchListener implements OnTouchListener {
#SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(
new GestureListener());
#Override
public boolean onTouch(View v, MotionEvent event) {
int touch = 0;
ViewHolder holder = (ViewHolder) v.getTag(R.layout.message_row);
animation = new Animater();
int action = event.getAction();
int position = (Integer) v.getTag();
pos = position;
view = v;
switch (action) {
case MotionEvent.ACTION_DOWN:
action_down_x = (int) event.getX();
Log.d("action", "ACTION_DOWN - ");
touch = 0;
break;
case MotionEvent.ACTION_MOVE:
Log.d("action", "ACTION_MOVE - ");
action_up_x = (int) event.getX();
difference = action_down_x - action_up_x;
calcuateDifference(holder, position);
touch = 1;
break;
case MotionEvent.ACTION_UP:
Log.d("action", "ACTION_UP - ");
// calcuateDifference(holder, position);
action_down_x = 0;
action_up_x = 0;
difference = 0;
// touch = 2;
break;
}
return gestureDetector.onTouchEvent(event);
}
public GestureDetector getGestureDetector() {
return gestureDetector;
}
}
In this gestuer class when i tap on ListView just display one more Button.This also be changed the position
public class GestureListener extends SimpleOnGestureListener{
#SuppressLint("NewApi")
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
super.onLongPress(e);
final ViewHolder hold = (ViewHolder) view.getTag(R.layout.message_row);
hold.copyMessageButton.setVisibility(View.VISIBLE);
hold.copyMessageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
System.out.println("hello you click me..");
});
}
}
What is the wrong on my code..Any one please help me...
Android ListView use Adapters to recycle views that's why ListView position changes while scrolling to conserve memory.See the Google I/O presentation given by Romain Guy to understand how adapters work. http://www.youtube.com/watch?v=N6YdwzAvwOA
Also check the below links,
http://strangevikas.wordpress.com/tag/how-adapter-works-in-android-android-and-adapters/
http://www.edureka.in/blog/what-are-adapters-in-android/
You just need to do some modification in your code like:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = lInflater.inflate(R.layout.message_row, null);
holder.messageDeleteButton = (Button) convertView
.findViewById(R.id.delete_button);
holder.messageTextView = (TextView) convertView
.findViewById(R.id.message);
holder.timeTextView = (TextView) convertView
.findViewById(R.id.time);
holder.copyMessageButton = (ImageButton) convertView
.findViewById(R.id.copy_bt);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.messageDeleteButton.setTag(mMessageData.get(position));
holder.messageDeleteButton
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// message data can be retrieved here like
MessageData mData = (MessageData) v.getTag();
System.out.println("Delete clicked");
}
});
if (mMessageData.get(position).isVisible())
holder.messageDeleteButton.setVisibility(View.VISIBLE);
else
holder.messageDeleteButton.setVisibility(View.GONE);
holder.messageTextView.setText(mMessageData.get(position)
.getMessage_text());
holder.messageTextView.setTypeface(Typeface.createFromAsset(
mContext.getAssets(), "Existence-Light.ttf"));
holder.timeTextView.setText(mMessageData.get(position)
.getMessage_date());
holder.timeTextView.setTypeface(Typeface.createFromAsset(
mContext.getAssets(), "Existence-Light.ttf"));
return convertView;
}

Change ListView background - strange behaviour

I have a problem with changing the background of a view in a ListView.
What I need:
Change the background image of a row onClick()
What actually happens:
The background gets changed (selected) after pressing e.g. the first entry. But after scrolling down the 8th entry is selected too.
Scroll back to the top the first isn't selected anymore. The second entry is selected now.
Continue scrolling and it continues jumping...
What i'm dong in the Code:
I have channels, and onClick() I toggle an attribute of channel boolean selected
and then I change the background.
I'm doing this only onClick() thats why I don't get why it's actuelly happening on other entries too.
One thing I notices is: It seems to be only the "drawing"-part because the item which get selected "by it self" has still the selected value on false
I think it seems to have something to do with the reuse of the views in the custom ListAdapters getView(...)
Code of onClick() in ListActivity:
#Override
protected ViewHolder createHolder(View v) {
// createHolder will be called only as long, as the ListView is not
// filled
TextView title = (TextView) v
.findViewById(R.id.tv_title_channel_list_adapter);
TextView content = (TextView) v
.findViewById(R.id.tv_content_channel_list_adapter);
ImageView icon = (ImageView) v
.findViewById(R.id.icon_channel_list_adapter);
if (title == null || content == null || icon == null) {
Log.e("ERROR on findViewById",
"Couldn't find Title, Content or Icon");
}
ViewHolder mvh = new MyViewHolder(title, content, icon);
// We make the views become clickable
// so, it is not necessary to use the android:clickable attribute in
// XML
v.setOnClickListener(new ChannelListAdapter.OnClickListener(mvh) {
public void onClick(View v, ViewHolder viewHolder) {
// we toggle the enabled state and also switch the the
// background
MyViewHolder mvh = (MyViewHolder) viewHolder;
Channel ch = (Channel) mvh.data;
ch.setSelected(!ch.getSelected()); // toggle
if (ch.getSelected()) {
v.setBackgroundResource(R.drawable.row_blue_selected);
} else {
v.setBackgroundResource(R.drawable.row_blue);
}
// TESTING
Log.d("onClick() Channel", "onClick() Channel: "
+ ch.getTitle() + " selected: " + ch.getSelected());
}
});
return mvh;
}
Code of getView(...):
#Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder holder;
// When view is not null, we can reuse it directly, there is no need
// to reinflate it.
// We only inflate a new View when the view supplied by ListView is
// null.
if (view == null) {
view = mInflater.inflate(mViewId, null);
// call own implementation
holder = createHolder(view);
// TEST
// we set the holder as tag
view.setTag(holder);
} else {
// get holder back...much faster than inflate
holder = (ViewHolder) view.getTag();
}
// we must update the object's reference
holder.data = getItem(position);
// <EDIT SOLUTION>
if(getItem(position).get_id() == channelList.get(position).get_id()){
if(getItem(position).getSelected())
{
view.setBackgroundResource(R.drawable.row_blue_selected);
}
else{
view.setBackgroundResource(R.drawable.row_blue);
}
}
// </EDIT SOLUTION>
// call the own implementation
bindHolder(holder);
return view;
}
I really would appreciate any idea how to solve this! :)
If more information is needed please tell me.
Thanks in advance!
Let me show you the code that I use for every ListView and properly controlling the click event for changing the background and doing anything further
public class Offices extends Activity {
private ListView listView;
/* selectedListItem will contain the number of items to be selected.
* Your list item OnOlickListener will simply change this variable
* to the position of the clicked item. The Adapter will do the rest
* because you need to refresh the ListView.
*/
private int selectedListItem = -1;
private Handler mHandler = new Handler();
private Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.officeslayout);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("Five");
data.add("Six");
data.add("Seven");
data.add("Eight");
data.add("Nine");
data.add("Ten");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.officeslistitemlayout, null);
holder = new ViewHolder();
holder.backgroundView = (ImageView) convertView
.findViewById(R.id.OfficesBackground);
holder.officesTitle = (TextView) convertView
.findViewById(R.id.OfficesName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position == selectedListItem) {
holder.backgroundView.setBackgroundResource(R.drawable.and_gray_bg_listing_selected);
} else {
holder.backgroundView.setBackgroundResource(R.drawable.and_gray_bg_listing);
}
holder.officesTitle.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView officesTitle;
ImageView backgroundView;
}
}
officeslistitemlayout.xml file will be like following add drawable and design it according to you put the following code in RelativeLayout
<ImageView android:id="#+id/OfficesBackground" android:layout_width="fill_parent"
android:layout_height="45dip"
android:layout_alignParentTop="true"
android:background="#drawable/and_gray_bg_listing"
android:scaleType="fitXY"
></ImageView>
<TextView android:id="#+id/OfficesName" android:layout_width="wrap_content"
android:text="Offices Name"
android:textColor="#000000" android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_marginLeft="5dip"
></TextView>
Hope it will help :)

Categories

Resources