I'm implementing a ListView and on quick scrolling in large lists this glitch occurs. I am using ViewHolder Pattern
[![UI Glitch in ListView][1]][1]
[1]:
getView :
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext())
.inflate(mResource, parent, false);
viewHolder = new ViewHolder();
viewHolder.titleView = (TextView) convertView.findViewById(R.id.sheet_name_text_view);
viewHolder.rowsCount = (TextView) convertView.findViewById(R.id.rows_count_text_view);
viewHolder.selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final ImportedTable item = getItem(position);
if (item != null) {
viewHolder.titleView.setText(item.getTableName());
int columnsCount = 0;
for (boolean isSelected : item.getSelectedColumns()) {
if (isSelected) {
columnsCount++;
}
}
viewHolder.rowsCount.setText(context.getString(R.string.label_rowscounttext, (item.getRecordsCount() - item.getHeaderRow()), columnsCount));
}
final CheckBox isSelectedForImport = (CheckBox) convertView.findViewById(R.id.import_table_list_toggle_button);
RelativeLayout checkboxLayout = (RelativeLayout) convertView.findViewById(R.id.import_table_list_toggle_layout);
checkboxLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isSelectedForImport.performClick();
}
});
final TextView selectMessage = (TextView) convertView.findViewById(R.id.row_info_text);
isSelectedForImport.setChecked(item.isSelected());
if (!isSelectedForImport.isChecked()) {
convertView.setClickable(true);
viewHolder.titleView.setTextColor(Color.parseColor("#8A000000"));
} else {
convertView.setClickable(false); viewHolder.titleView.setTextColor(Color.parseColor("#DE000000"));
}
isSelectedForImport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId()) && !isSelectedForImport.isChecked()) {
isSelectedForImport.setChecked(true);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog dialog;
builder.setMessage(context.getString(R.string.importapplication_appscreen_label_lookupwarningmessage));
builder.setPositiveButton(context.getString(R.string.ui_label_continue), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < item.getColumnType().size(); i++) {
removeLookups(item.getSheetId(), item.getTableId(), i);
}
isSelectedForImport.setChecked(false);
item.setSelected(false);
dialog.dismiss();
}
});
builder.setNegativeButton(context.getString(R.string.ui_label_dontcontinue), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.show();
notifyDataSetChanged();
} else if (!isSelectedForImport.isChecked()) {
item.setSelected(isSelectedForImport.isChecked());
notifyDataSetChanged();
} else {
if (isSelectedForImport.isChecked()) {
for (int i = 0; i < item.getColumnType().size(); i++) {
if (item.getSelectedColumns().get(i) && item.getColumnType().get(i).equals("SINGLE_LOOKUP")) {
enableParents(item.getSheetId(), item.getTableId(), i);
}
}
notifyDataSetChanged();
}
item.setSelected(isSelectedForImport.isChecked());
}
mCallback.onTableSelectionToggle();
}
});
if (((ImportActivity) context).getNewApplication().hasChildren(item.getSheetId(), item.getTableId())) {
Set<String> children = ((ImportActivity) context).getNewApplication().getChildTableNames(item.getSheetId(), item.getTableId());
Iterator<String> childIterator = children.iterator();
String childrenString = childIterator.next();
while (childIterator.hasNext()) {
childrenString += ", ";
childrenString += childIterator.next();
}
selectMessage.setText(context.getString(R.string.label_importlookupchildrentext) +" "+ childrenString);
selectMessage.setVisibility(View.VISIBLE);
} else {
selectMessage.setVisibility(View.GONE);
}
return convertView;
}
This does not occur all the time, and it doesn't occur for small lists. I have no other common problems described in other posts here (as long as this glitch doesn't occur, all views work correctly, and are in proper order).
XML Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/sheet_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#DE000000"
android:textSize="16sp"
tools:text="Sheet Name" />
<TextView
android:id="#+id/rows_count_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8A000000"
android:textSize="14sp"
tools:text="Rows : 3" />
<TextView
android:id="#+id/row_info_text"
android:textSize="12sp"
android:layout_width="match_parent"
android:ellipsize="end"
android:maxLines="1"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:visibility="gone"
tools:visibility="gone"
tools:text="This form is looked up by "/>
</LinearLayout>
<RelativeLayout
android:id="#+id/import_table_list_toggle_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="19dp"
android:paddingRight="19dp"
android:layout_alignParentRight="true">
<CheckBox
android:id="#+id/import_table_list_toggle_button"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingBottom="24dp"
android:paddingTop="24dp" />
</RelativeLayout>
As I mention problem is RelativeLayout layout.
try this...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/sheet_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:weightSum="10">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/sheet_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:textColor="#DE000000"
android:textSize="16sp"
tools:text="Sheet Name"/>
<TextView
android:id="#+id/rows_count_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8A000000"
android:textSize="14sp"
tools:text="Rows : 3"/>
<TextView
android:id="#+id/row_info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:ellipsize="end"
android:maxLines="1"
android:textSize="12sp"
android:visibility="gone"
tools:text="This form is looked up by "
tools:visibility="gone"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/import_table_list_toggle_layout"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingLeft="19dp"
android:paddingRight="19dp">
<CheckBox
android:id="#+id/import_table_list_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:paddingBottom="24dp"
android:paddingTop="24dp"/>
</RelativeLayout>
Related
I have a grid view and data updated in grid view using api calls and grid item loads a image from url using picasso library all are perfect but when i am scrolling down to up the grid items not scrolled and repeating the items(up to down scrolling was perfect).
Adapterclass.java
private class ProductAdapter extends BaseAdapter {
ViewHolder holder = null;
#Override
public int getCount() {
return mProducts != null ? mProducts.size() : 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
holder = new ViewHolder();
if (convertView == null) {
// convertView = LayoutInflater.from(Products_activity.this).inflate(R.layout.product_layout, null, false);
LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.product_layout, null);
holder.product_name = (TextView) convertView.findViewById(R.id.product_name);
holder.product_descr = (TextView) convertView.findViewById(R.id.product_desc);
holder.product_price = (TextView) convertView.findViewById(R.id.product_price);
holder.product_pic = (ImageView) convertView.findViewById(R.id.iv_division_img);
holder.product_share = (ImageView) convertView.findViewById(R.id.productshare_button);
holder.like_image=(ImageView) convertView.findViewById(R.id.like_image);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
if (mProducts != null) {
String url = "http://staggershop.pivalue.net/assets/product_images/"+mProducts.get(position).product_image;
String urls = url.replaceAll(" ", "%20");
Picasso.with(getApplicationContext()).load(urls).resize(300,300).noFade().error(R.drawable.home_brands).into(holder.product_pic);
holder.product_name.setText(mProducts.get(position).product_name);
//product_descr.setText(mProducts.get(position).pr);
// product_price.setText(mProducts.get(position).product_price);
/* Picasso
.with(getApplicationContext())
.setLoggingEnabled(true);
Picasso.with(getApplicationContext())
.load(urls)
.error(R.drawable.home_brands).memoryPolicy(MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_STORE)
.into(product_pic);*/
holder.product_descr.setText(mProducts.get(position).product_description);
if(!recently_beans.isEmpty()){
for(int i=0;i<recently_beans.size();i++) {
if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id))
{
holder.like_image.setImageResource(R.drawable.like);
}
}
notifyDataSetChanged();
}
}
holder.product_share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
}
});
holder.like_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
if (!recently_beans.isEmpty()) {
for (int i = 0; i < recently_beans.size(); i++) {
if (mProducts.get(position).prdid.equals(recently_beans.get(i).prod_id)) {
holder.like_image.setImageResource(R.drawable.dislike);
Inserfavorite(mProducts.get(position).prdid);
}
}
}
}
});
}
});
// notifyDataSetChanged();
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Inserrecent(mProducts.get(position).prdid);
Intent intent = new Intent(Products_activity.this, Product_description.class);
startActivity(intent);
}
});
return convertView;
}
}
public static class ViewHolder {
public TextView product_name;
public TextView product_descr;
public TextView product_price;
public ImageView product_pic ;
public ImageView product_share;
public ImageView like_image;
}
This my product.xml
<RelativeLayout
android:id="#+id/rl_divisioin_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/product_img">
<ImageView
android:id="#+id/iv_division_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/view"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:src="#drawable/home_brands" />
</LinearLayout>
<LinearLayout
android:id="#+id/prod_name_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#+id/product_img"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:fontFamily="#string/font_family_regular"
android:gravity="center"
android:padding="5dp"
android:maxLines="1"
android:text="product name"
android:textColor="#color/black"
android:textSize="10sp" />
<TextView
android:id="#+id/product_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="4dp"
android:fontFamily="#string/font_family_regular"
android:gravity="center"
android:maxLines="1"
android:padding="5dp"
android:text="product Description"
android:textColor="#color/black"
android:textSize="10sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/product_desc"
android:layout_gravity="center"
android:layout_marginTop="20dp">
<TextView
android:id="#+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:elevation="4dp"
android:text="100$"
android:textColor="#color/black"
android:textSize="12sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/productshare_button"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:elevation="4dp"
android:layout_marginRight="5dp"
android:src="#mipmap/symbol_dotss"
android:text="product name"
android:textColor="#color/black"
android:textSize="10sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:id="#+id/like_image"
android:layout_alignParentEnd="true"
android:src="#drawable/dislike" />
</RelativeLayout>
activty_product.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rltool"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar1"
android:background="#color/textColorPrimary"
app:titleTextColor="#color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/AppTheme.Toolbar"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/search1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/background_shape_buttons">
<!-- <EditText
android:id="#+id/searchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#D6D5C0"
android:hint="Search..................."
android:layout_gravity="center"
android:backgroundTint="#00000000" />-->
<SearchView
android:id="#+id/search"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textColorHint="#D6D5C0"
android:text="#string/search"
android:layout_gravity="center"
android:iconifiedByDefault="false">
<requestFocus />
</SearchView>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<View
android:id="#+id/shadow_view"
android:layout_width="match_parent"
android:layout_alignBottom="#+id/toolbar1"
android:visibility="visible"
android:layout_height="3dp"
android:background="#drawable/toolbar_shadow" />
</RelativeLayout>
<GridView
android:id="#+id/product_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scrollbars="vertical"
android:layout_margin="15dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp">
</GridView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
To avoid the unexpected behaviour , provide the implementations of
#Override
public Object getItem(int position) {
return mProducts.get(position);
}
#Override
public long getItemId(int position) {
//return position; or better to provide some unique id
// as there is one in your code, product id
return mProducts.get(position).prod_id;
}
Note : remove notifyDataSetChanged(); in getView because it will trigger the adapter to refresh all view while they are under creating which can cause this behaviour.
Recommended Read
What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?
I am new here so please bear with my naivety. I am working on a music player that will display a menu on clicking 'dots' buttton(given in code below) in listview item. Inside the menu there will be a button to delete. The listview is getting populated by a custom adapter. Here is the code of adapter...
private class hashmapAdapter extends BaseAdapter implements Filterable {
HashMap<Integer, NowPlayingActivity.Details> NewDetails = new HashMap<>();
private hashmapAdapter(HashMap<Integer, NowPlayingActivity.Details> hashMap) {
if(NewDetails.size()==0) {
NewDetails.putAll(hashMap);
}
}
#Override
public int getCount() {
return NewDetails.size();
}
#Override
public Object getItem(int position) {
return NewDetails.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View result;
if(convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item,parent,false);
} else {
result = convertView;
}
((TextView)result.findViewById(R.id.Name)).setText(NewDetails.get(position).getName());
((TextView)result.findViewById(R.id.artist)).setText(NewDetails.get(position).getArtist());
((TextView)result.findViewById(R.id.duration)).setText(NewDetails.get(position).getDuration()+" || ");
dots = (Button)result.findViewById(R.id.dots);
dots.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = (TextView)result.findViewById(R.id.Name);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View popup_view = inflater.inflate(R.layout.option_layout,null);
RelativeLayout item = (RelativeLayout) popup_view.findViewById(R.id.popup);
final float width= (getResources().getDimension(R.dimen.width_entry_in_dp));
final float height= getResources().getDimension(R.dimen.height_entry_in_dp);
final PopupWindow popupWindow = new PopupWindow(popup_view,Math.round(width),Math.round(height),true);
popupWindow.showAtLocation(item, Gravity.END, 0, 0);
popupWindow.showAsDropDown(dots);
play_next_btn = (Button)popup_view.findViewById(R.id.play_next);
add_to_playlist = (Button)popup_view.findViewById(R.id.add_to_playlist);
share = (Button)popup_view.findViewById(R.id.share);
delete = (Button)popup_view.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<NewDetails.size();i++) {
if(NewDetails.get(i).getName().equals(name.getText().toString())) {
for(int j=0;j<hashMap.size();j++) {
if(hashMap.get(j).getName().equals(name.getText().toString())) {
if (true) {
NewDetails.remove(i);
hashMap.remove(j);
}
}
}
}
}
populate1(NewDetails);
popupWindow.dismiss();
}
});
}
});
return result;}
Now the problem is that after deleting element when i call populate1(NewDetails), it gives a null pointer exception on NewDetails.get(position).getName(). So on repopulating listview i am getting NPE error. Following is the populate1() function code...
public void populate1(HashMap<Integer,NowPlayingActivity.Details> hashMap) {
adapter = new hashmapAdapter(hashMap);
library.setAdapter(adapter);
adapter.notifyDataSetChanged();
library.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = (TextView)view.findViewById(R.id.Name);
String SongName = name.getText().toString();
Intent intent = new Intent(getApplicationContext(),NowPlayingActivity.class);
intent.putExtra("SongName",SongName);
setResult(555,intent);
finish();
}
});
}
I have already tried notifydatasetchanged() and listview.invalidateviews(), both didnt work. File is getting deleted but the adapter is not refreshing the list to remove element. Here is the xml layout of single_item for listview....`
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="70dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:background="#drawable/element_shape_list"
android:descendantFocusability="blocksDescendants">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v11"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/art_work"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#mipmap/ic_launcher"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"/>
<TextView
android:id="#+id/Name"
android:textSize="17sp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_toRightOf="#id/art_work"
android:layout_toEndOf="#+id/art_work"
android:singleLine="true"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#+id/art_work"
android:layout_toEndOf="#id/art_work"
android:layout_below="#id/Name">
<TextView
android:id="#+id/duration"
android:textSize="12sp"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_height="20dp"
android:singleLine="true"
android:gravity="clip_horizontal"
android:ellipsize="end"/>
<TextView
android:id="#+id/artist"
android:textSize="12sp"
android:textColor="#000"
android:layout_toRightOf="#+id/duration"
android:layout_toEndOf="#+id/duration"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:gravity="fill_horizontal"
android:layout_height="20dp"
android:singleLine="true" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent">
<Button
android:id="#+id/dots"
android:layout_width="40dp"
android:textStyle="bold"
android:background="?android:attr/selectableItemBackground"
android:layout_height="70dp"
android:text="#string/vertical_ellipsis"
android:textSize="25sp" />
</FrameLayout>
</RelativeLayout>
I am having a list view inside a tabs fragments,
Now inside the list view i am having two views one view is already at gone visibility.when i scroll the list view both the view disappears.I am handling with the holders,but still I am facing the issue.
here is my xml:
<RelativeLayout
android:id="#+id/rl_maino"
android:layout_width="match_parent"
android:layout_height="76dp"
android:background="#color/white"
>
<RelativeLayout
android:id="#+id/rl_coloro"
android:layout_width="6dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="13dp"
android:background="#color/green"
>
</RelativeLayout>
<LinearLayout
android:id="#+id/ll_texto"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginTop="14dp"
android:layout_marginLeft="16dp">
<TextView
android:id="#+id/tv_subtitleo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="14sp"
android:text="Header" />
<TextView
android:id="#+id/tv_subo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/addr_col"
android:textSize="12sp"
android:layout_marginTop="3dp"
android:text="subtext subtext subtext subtext subtext subtexts ubtexts subtexts" />
</LinearLayout>
<TextView
android:id="#+id/tv_costo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:textColor="#color/black"
android:text="AED 12200" />
<LinearLayout
android:id="#+id/ll_addo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_costo"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="3dp"
android:layout_marginBottom="14dp"
android:layout_marginRight="16dp"
>
<ImageView
android:id="#+id/iv_minuso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/minus_selector"
android:visibility="gone"/>
<TextView
android:id="#+id/tv_numbero"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/black"
android:layout_margin="5dp"
android:gravity="center"
android:textSize="15sp"
android:text="0" />
<ImageView
android:id="#+id/iv_pluso"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus_selector" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_unavailo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_costo"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="3dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="16dp"
android:visibility="gone"
>
<TextView
android:id="#+id/tv_unavailo"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/addr_col"
android:layout_margin="2dp"
android:gravity="center_vertical|right"
android:textSize="12sp"
android:text="#string/c_unavail" />
</LinearLayout>
</RelativeLayout>
here is my adapter:
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.only_pro_items, null);
holder=new ViewHolder();
holder.rl_main=(RelativeLayout)convertView.findViewById(R.id.rl_maino);
holder.rl_color=(RelativeLayout)convertView.findViewById(R.id.rl_coloro);
holder.ll_unavail=(LinearLayout)convertView.findViewById(R.id.ll_unavailo);
holder.ll_text=(LinearLayout)convertView.findViewById(R.id.ll_texto);
holder.ll_add=(LinearLayout)convertView.findViewById(R.id.ll_addo);
holder.tv_subtitle=(TextView)convertView.findViewById(R.id.tv_subtitleo);
holder.tv_sub=(TextView)convertView.findViewById(R.id.tv_subo);
holder.tv_cost=(TextView)convertView.findViewById(R.id.tv_costo);
holder.tv_number=(TextView)convertView.findViewById(R.id.tv_numbero);
holder.tv_unavail=(TextView)convertView.findViewById(R.id.tv_unavailo);
holder.iv_minus=(ImageView)convertView.findViewById(R.id.iv_minuso);
holder.iv_plus=(ImageView)convertView.findViewById(R.id.iv_pluso);
convertView.setTag(holder);
} else {
Log.d("ttt", "3");
holder=(ViewHolder) convertView.getTag();
}
holder.tv_subtitle.setTypeface(gotham_book);
holder.tv_sub.setTypeface(gotham_light);
holder.tv_cost.setTypeface(gotham_book);
holder.tv_number.setTypeface(gotham_book);
holder.tv_unavail.setTypeface(gotham_light);
final Model_OnlyProduct model=arr_model.get(position);
holder.tv_subtitle.setText(model.getTv_name());
holder.tv_sub.setText(model.getTv_sub_name());
final int price[]={0};
if (!model.getTv_price().matches("")) {
holder.tv_cost.setText(Constants.currency+model.getTv_price());
price[0]=Integer.parseInt(model.getTv_price());
} else {
holder.tv_cost.setText("From "+Constants.currency+model.getFrom_price());
price[0]=Integer.parseInt(model.getFrom_price());
}
if (model.getInStock().matches("In Stock")) {
holder.ll_unavail.setVisibility(View.GONE);
}else {
holder.ll_unavail.setVisibility(View.VISIBLE);
holder.ll_add.setVisibility(View.GONE);
}
String food_class=model.getRl_color();
switch (food_class) {
case "Veg":
holder.rl_color.setBackgroundColor(activity.getResources().getColor(R.color.veg_green));
break;
case "Non Veg":
holder.rl_color.setBackgroundColor(activity.getResources().getColor(R.color.non_red));
break;
case "Egg":
holder.rl_color.setBackgroundColor(activity.getResources().getColor(R.color.egg_yellow));
break;
case "":
holder.rl_color.setBackgroundColor(activity.getResources().getColor(R.color.white));
break;
}
final int[] count={0};
if (count[0]==0) {
holder.tv_number.setVisibility(View.INVISIBLE);
}
holder.iv_plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (count[0]>=0&&count[0]!=99) {
Log.d("qqq", "ar1r="+model.getCustom_status());
if (model.getCustom_status().matches("1")) {
if (model.getCustom_type().matches("Combo")) {
Intent intent=new Intent(activity, Combo.class);
intent.putExtra(Constants.ITEM_ID, String.valueOf(model.getId()));
intent.putExtra(Constants.ITEM_NAME,model.getTv_name());
if (!model.getTv_price().matches("")) {
intent.putExtra(Constants.ITEM_TYPE, "price");
intent.putExtra(Constants.ITEM_PRICE, model.getTv_price());
} else {
intent.putExtra(Constants.ITEM_TYPE, "from");
intent.putExtra(Constants.ITEM_PRICE, model.getFrom_price());
}
intent.putExtra(Constants.ITEM_COUNT, String.valueOf(count[0]));
intent.putExtra(Constants.ITEM_SUBTEXT, model.getTv_sub_name());
intent.putExtra(Constants.ITEM_FROM, "menu");
intent.putExtra(Constants.VIEW_INDEX, ""+model.getView_index());
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.move_up, R.anim.stay);
} else {
Intent intent=new Intent(activity, Toppings.class);
intent.putExtra(Constants.ITEM_ID, String.valueOf(model.getId()));
intent.putExtra(Constants.ITEM_NAME,model.getTv_name());
if (!model.getTv_price().matches("")) {
intent.putExtra(Constants.ITEM_TYPE, "price");
intent.putExtra(Constants.ITEM_PRICE, model.getTv_price());
} else {
intent.putExtra(Constants.ITEM_TYPE, "from");
intent.putExtra(Constants.ITEM_PRICE, model.getFrom_price());
}
intent.putExtra(Constants.ITEM_COUNT, String.valueOf(count[0]));
intent.putExtra(Constants.ITEM_SUBTEXT, model.getTv_sub_name());
intent.putExtra(Constants.ITEM_FROM, "menu");
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.move_up, R.anim.stay);
}
}else
{
holder.tv_number.setVisibility(View.VISIBLE);
count[0]++;
Constants.cart_count++;
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
holder.tv_number.setText(String.valueOf(count[0]));
FragmentMenu.tv_count.setVisibility(View.VISIBLE);
FragmentMenu.tv_count.setText(String.valueOf(Constants.cart_count));
int total=price[0];
Constants.cart_amount+=total;
FragmentMenu.tv_cart_money.setText(Constants.currency+Constants.cart_amount);
}
});
Constants.count++;
String id=String.valueOf(Constants.count);
HashMap<String, String> map=new HashMap<String, String>();
map.put(Constants.ITEM_ID, String.valueOf(model.getId()));
map.put(Constants.ITEM_NAME, model.getTv_name());
map.put(Constants.ITEM_PRICE, model.getTv_price());
map.put(Constants.ITEM_PRICE, model.getTv_price());
map.put(Constants.ITEM_COUNT, String.valueOf(count[0]));
map.put(Constants.ID, id);
if (Constants.arr_global.size()>0) {
int count=Constants.arr_global.size();
boolean yettop=false;
int qwer=0;
for (int i = 0; i < count; i++) {
HashMap<String, String> map2=Constants.arr_global.get(i);
String item_id=map2.get(Constants.ITEM_ID);
if (item_id.matches(String.valueOf(model.getId()))) {
yettop=true;
qwer=i;
break;
}
else {
yettop=false;
}
}
if (yettop) {
Constants.arr_global.remove(qwer);
Constants.arr_global.add(map);
} else {
Constants.arr_global.add(map);
}
}else {
Constants.arr_global.add(map);
}
}
}
}
});
return convertView;
}
issue is:
if (model.getInStock().matches("In Stock")) {
holder.ll_unavail.setVisibility(View.GONE);
}else {
holder.ll_unavail.setVisibility(View.VISIBLE);
holder.ll_add.setVisibility(View.GONE);
}
in this loop the next item visibility get changes..
use this condition and check
if (model.getInStock().matches("In Stock")) {
holder.ll_add.setVisibility(View.VISIBLE);
holder.ll_unavail.setVisibility(View.GONE);
}else {
holder.ll_unavail.setVisibility(View.VISIBLE);
holder.ll_add.setVisibility(View.GONE);
}
I have searched for this issue a few days. Here's the structure of the app.
activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- The main content view -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<fragment android:id="#+id/fragment_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="ca.bluecross.ab.view.fragments.DrawerFragment"
tools:layout="#layout/abc_drawer_layout" />
</android.support.v4.widget.DrawerLayout>
Here's the layout of the fragment (some elements are not included to save space for post):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:id="#+id/scrollView"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
..
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="5dp"
android:background="#21c1c1c1">
..
</RelativeLayout>
..
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/expLVClaims"
/>
.
..
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TableRow
..
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
..
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
..
</TableRow>
</TableLayout>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="I have more to add"
android:id="#+id/btnAddMoreClaim"
android:layout_gravity="center_horizontal"
android:layout_span="2"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:background="#color/abc_blue"
android:textColor="#FFFFFF" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="I'm ready to submit"
android:id="#+id/btnReadyToSubmitClaim"
android:layout_span="2"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/btnAddMoreClaim"
android:background="#color/abc_blue"
android:textColor="#FFF" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/btnCancelEnterClaim"
android:layout_alignWithParentIfMissing="false"
android:layout_centerInParent="true"
android:background="#color/abc_blue"
android:textColor="#FFF" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Here's the layout for group header:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgIndicator"
android:src="#drawable/icon_collapse"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Provider name"
android:id="#+id/txtProviderName"
android:layout_toRightOf="#+id/imgIndicator" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgDelete"
android:src="#drawable/icon_cross_delete"
android:layout_toLeftOf="#+id/imgEdit" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/imgEdit"
android:layout_alignParentEnd="true"
android:src="#drawable/icon_edit" />
</RelativeLayout>
Here's the child layout:
<?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"
android:background="#59868686">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutServiceDate">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of service"
android:id="#+id/txtClaimDetailServiceDateLabel"
android:layout_alignParentStart="true"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feb 10, 2015"
android:id="#+id/txtClaimDetailServiceDate"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutProvider">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Provider"
android:id="#+id/txtClaimDetailProviderLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="provider's name"
android:id="#+id/txtClaimDetailProviderName"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayoutProduct">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product or Service"
android:id="#+id/txtProductLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product/service"
android:id="#+id/txtProduct"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutClaimAmount">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Claim amount($)"
android:id="#+id/txtClaimDetailClaimAmountLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:id="#+id/txtClaimDetailClaimAmount"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:id="#+id/relativeLayoutOtherAmount">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other amount($)"
android:id="#+id/txtClaimDetailOtherAmountLabel"
android:textStyle="bold"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00"
android:id="#+id/txtClaimDetailOtherAmount"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
Here's the adapter for the ExpandableListView:
public class EclaimsExpandableListAdapter extends BaseExpandableListAdapter {
private ArrayList<ClaimGroup> groups;
public LayoutInflater inflater;
public Activity activity;
float mDensity;
private static final int MAX_ITEMS_MEASURED = 15;
public EclaimsExpandableListAdapter(Activity act, ArrayList<ClaimGroup> groups){
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
mDensity = activity.getResources().getDisplayMetrics().density;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).getChildren().get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
//return 0;
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Log.d("EclaimsExpandableListAdapter :: getChildView() :: ", "Start");
ClaimDetail child = (ClaimDetail)getChild(groupPosition, childPosition);
if(convertView == null){
convertView = inflater.inflate(R.layout.eclaims_claim_detail_health_layout,null);
}
TextView txtServiceDate = (TextView)convertView.findViewById(R.id.txtClaimDetailServiceDate);
txtServiceDate.setText(child.getmServiceDate());
TextView txtProviderName = (TextView)convertView.findViewById(R.id.txtClaimDetailProviderName);
txtProviderName.setText(child.getProvider().getName());
TextView txtClaimAmount = (TextView)convertView.findViewById(R.id.txtClaimDetailClaimAmount);
txtClaimAmount.setText(child.getClaimAmount().toString());
TextView txtOtherAmout = (TextView)convertView.findViewById(R.id.txtClaimDetailOtherAmount);
if(child.getOtherAmount() == null){
txtOtherAmout.setVisibility(View.GONE);
}else{
txtOtherAmout.setText(child.getOtherAmount().toString());
}
return convertView;
}
#Override
public int getChildrenCount(int grpPostion) {
System.out.println("Group position :: " + grpPostion);
return groups.get(grpPostion).getChildren().size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Log.d("EclaimsExpandableListAdapter :: getGroupView() :: ", "Start");
if (convertView == null) {
convertView = inflater.inflate(R.layout.eclaims_claim_group_row, null);
}
/*TextView claimsLabel = (TextView)activity.findViewById(R.id.txtClaimItemsLabel);
if(groups.isEmpty()){
claimsLabel.setVisibility(View.GONE);
}else{
claimsLabel.setVisibility(View.VISIBLE);
}*/
ClaimGroup group = (ClaimGroup) getGroup(groupPosition);
ImageView indicator = (ImageView)convertView.findViewById(R.id.imgIndicator);
int nH = 0;
if(isExpanded){
indicator.setImageResource(R.drawable.icon_expand);
//nH = measureChildrenHeight(groupPosition);
}else{
indicator.setImageResource(R.drawable.icon_collapse);
//nH = convertView.getMeasuredHeight();
}
//parent.getLayoutParams().height = (int) (nH * mDensity);
//parent.invalidate();
TextView providerName = (TextView)convertView.findViewById(R.id.txtProviderName);
providerName.setText(group.getProvider().getName());
ImageView imgDelete = (ImageView)convertView.findViewById(R.id.imgDelete);
imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
String confirmation = activity.getString(R.string.eclaims_delete_claim_confirmation);
builder.setMessage(confirmation);
builder.setPositiveButton(R.string.alert_Cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
builder.setNegativeButton(R.string.alert_OK, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
groups.remove(groupPosition);
notifyDataSetChanged();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
ImageView imgEdit = (ImageView)convertView.findViewById(R.id.imgEdit);
imgEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
String confirmation = activity.getString(R.string.eclaims_edit_claim_confirmation);
builder.setMessage(confirmation);
builder.setPositiveButton(R.string.alert_Cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
builder.setNegativeButton(R.string.alert_OK, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// first: remove from the completed claims
Provider selectedProvider = ((ClaimGroup)getGroup(groupPosition)).getProvider();
ClaimGroup parent = groups.remove(groupPosition);
List<Product> prodList = parent.getProductList(); // retrieved the saved product list
ClaimDetail child = parent.getChildren().get(0); // there is only one child
Product selectedProduct = child.getProduct();
notifyDataSetChanged();
// second: populate the fields in claim detail block
TextView serviceDate = (TextView)activity.findViewById(R.id.txtServiceDate);
serviceDate.setText(child.getmServiceDate());
Spinner spinnerProvider = (Spinner)activity.findViewById(R.id.spinnerProviders);
ArrayAdapter<Provider> adapterProvider = (ArrayAdapter< Provider >)spinnerProvider.getAdapter();
int size = adapterProvider.getCount();
int pos_seletion = 0;
for(int i=0; i<size; i++){
Provider prvd = adapterProvider.getItem(i);
if(selectedProvider.equals(prvd)){
pos_seletion = i;
break;
}
}
System.out.println("Selected position for provider list :: " + pos_seletion);
spinnerProvider.setSelection(pos_seletion);
adapterProvider.notifyDataSetChanged();// do we need to notify?
Spinner spinnerProduct = (Spinner)activity.findViewById(R.id.spinnerProducts);
ArrayAdapter<Product> adapterProduct = (ArrayAdapter < Product >)spinnerProduct.getAdapter();
size = prodList.size();
pos_seletion = 0;
for(int j=0; j<size; j++){
Product prod = prodList.get(j);
if(selectedProduct.equals(prod)){
pos_seletion = j;
break;
}
}
System.out.println("Selected position for product list :: " + pos_seletion);
adapterProduct.clear();
adapterProduct.addAll(prodList);
spinnerProduct.setSelection(pos_seletion);
adapterProduct.notifyDataSetChanged(); // do we need to notify?
EditText claimAmt = (EditText)activity.findViewById(R.id.editClaimAmount);
claimAmt.setText(child.getClaimAmount().toString());
EditText otherAmt = (EditText)activity.findViewById(R.id.editOtherAmount);
if(child.getOtherAmount() != null){
otherAmt.setText(child.getOtherAmount().toString());
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
}
Here's the behavior: when clicking the group header, no child view is displayed. From LogCat, I can see that getViewGroup() in the adapter is called twice though, but getChildView() is not called at all. After searching the internet and trying them out, I still can't get the child view displayed when clicking on the group header, but when I tried setting the ExpanandableListView height with a fix height, say, 400dp, like this:
<ExpandableListView xmlns:
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#+id/expLVClaims"/>
the child view is then expanded and collapsed as expected. But the thing is that because the fixed height will makes the ExpandableListView occupy 400dp space vertically, even no data is in the adapter at all, which is unwanted behavior.
Can someone throw some hints?
Thanks in advance!
Shawn
First thing:
You should not use any scrolling component like expandable list, inside a scroll view here's why Listview inside ScrollView is not scrolling on Android.
Second thing:
Height of ExpandableListView should be match_parent.
I was also facing the same problem related to height, and making ExpandableListView height as match_parent resolved my problem. I think this will help you.
I want to change the button of android grid view row item, when I click in the first row, I can change the button (play) to (pause), then when I go to another row, also I can change the play to pause, but I need to change the previous row automatically to pause, because it is now still play.
I have tried the code below but not effect.
Main Grid 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"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:numColumns="1"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:clipToPadding="false"/>
</RelativeLayout>
Row XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewMain"
android:layout_width="120dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="#drawable/img4" />
<TextView
android:id="#+id/textViewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageViewMain"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/imageViewMain"
android:text="PRİNCESS BİRTHDAY"
android:textColor="#000"
android:textSize="24dp" />
<TextView
android:id="#+id/textViewYazar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="Yazar : "
android:layout_below="#+id/textViewMain"
android:layout_alignLeft="#+id/textViewMain"
android:layout_alignStart="#+id/textViewMain" />
<TextView
android:id="#+id/textViewYazarName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yazar adı"
android:textColor="#412204"
android:textSize="10sp"
android:layout_below="#+id/textViewMain"
android:layout_toRightOf="#+id/textViewYazar"
android:layout_toEndOf="#+id/textViewYazar"
android:layout_marginLeft="26dp"/>
<TextView
android:id="#+id/textViewSeslendiren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="Seslendiren : "
android:layout_below="#+id/textViewYazar"
android:layout_alignLeft="#+id/textViewMain"
android:layout_alignStart="#+id/textViewMain" />
<TextView
android:id="#+id/textViewSeslendirenName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seslendiren Adı"
android:textColor="#412204"
android:textSize="10sp"
android:layout_below="#+id/textViewYazarName"
android:layout_toRightOf="#+id/textViewYazar"
android:layout_alignStart="#+id/textViewYazarName"/>
<TextView
android:id="#+id/textViewSizeDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#412204"
android:textStyle="bold"
android:textSize="10sp"
android:text="120 MB / 130 Mins"
android:layout_below="#+id/textViewSeslendirenName"
android:layout_alignLeft="#+id/textViewMain"
android:layout_alignStart="#+id/textViewMain" />
<Button
android:id="#+id/buttonPlay"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_above="#+id/imageViewBottomLine"
android:layout_alignLeft="#+id/textViewMain"
android:background="#drawable/ic_action_play"/>
<ImageView
android:id="#+id/imageViewTL"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/tl_simgesi"
android:layout_alignTop="#+id/textViewPrice"
android:layout_toLeftOf="#+id/textViewPrice"/>
<TextView
android:id="#+id/textViewPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2.70"
android:textColor="#E85404"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginRight="50dp"
android:layout_marginBottom="10dp"
android:layout_above="#+id/imageViewBottomLine"
android:layout_alignParentRight="true" />
<Button
android:id="#+id/buttonAdd"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_action_basket_dark"
android:layout_above="#+id/imageViewBottomLine"
android:layout_alignParentRight="true"
android:onClick="onAddToCard"/>
<ImageView
android:id="#+id/imageViewBottomLine"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignBottom="#+id/imageViewMain"
android:scaleType="fitXY"
android:src="#drawable/top_line" />
</RelativeLayout>
View Adaptor GetView Code
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflator = activity.getLayoutInflater();
imageLoader = new ImageLoader(activity.getApplicationContext());
if (convertView == null) {
holder = new ViewHolder();
convertView = inflator.inflate(R.layout.gridrow2, null);
holder.imgViewMain = (ImageView) convertView.findViewById(R.id.imageViewMain);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textViewMain);
holder.txtViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.txtViewCast = (TextView) convertView.findViewById(R.id.textViewSeslendirenName);
holder.txtViewAuthor = (TextView) convertView.findViewById(R.id.textViewYazarName);
holder.txtViewSizeDuration = (TextView) convertView.findViewById(R.id.textViewSizeDuration);
holder.play = (Button) convertView.findViewById(R.id.buttonPlay);
holder.addcart = (Button) convertView.findViewById(R.id.buttonAdd);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
imageLoader.DisplayImage(urlList.get(position), holder.imgViewMain);
if (title.get(position).length() > 30) {
holder.txtViewTitle.setTextSize(20);
}
holder.txtViewTitle.setText(title.get(position));
holder.txtViewPrice.setText(price.get(position).replaceAll(" TL", ""));
holder.txtViewCast.setText(cast.get(position));
holder.txtViewAuthor.setText(author.get(position));
holder.txtViewSizeDuration.setText(size.get(position) + " / " + duration.get(position));
holder.imgViewMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(activity.getApplicationContext(), SubMenu.class);
in.putExtra("Title", title.get(position));
in.putExtra("Id", id.get(position));
in.putExtra("Author", author.get(position));
in.putExtra("Description", description.get(position));
in.putExtra("Img", urlList.get(position));
in.putExtra("Price", price.get(position));
in.putExtra("Size", size.get(position));
in.putExtra("Duration", duration.get(position));
in.putExtra("Publication", publication.get(position));
in.putExtra("Cast", cast.get(position));
activity.startActivity(in);
}
});
holder.play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.click == 0) {
stopPlayer(v.getContext(), position, previousPosition);
productId = id.get(position);
PlayExecute(v.getContext());
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_pause));
holder.click = 1;
previousPosition = position;
} else {
stopPlayer(v.getContext(), position, previousPosition);
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_play));
holder.click = 0;
}
}
});
holder.addcart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setBackground(v.getContext().getResources().getDrawable(R.drawable.ic_action_basket));
prfs = PreferenceManager.getDefaultSharedPreferences(activity);
productId = id.get(position);
AddExecute();
}
});
return convertView;
}
StopPlayer and change previous icon
public void stopPlayer(Context c, int current, int previous) {
System.out.println("current : " + current);
System.out.println("previous : " + previous);
if (mp != null && mp.isPlaying()) {
mp.stop();
}
if (current!=previous){
View v= this.getView(previousPosition, null, null);
Button b = (Button) v.findViewById(R.id.buttonPlay);
b.setBackground(c.getResources().getDrawable(R.drawable.ic_action_play));
this.notifyDataSetChanged();
mGridview.refreshDrawableState();
}
}
see the attached image please;
http://postimg.org/image/b3fbpc211/
Instead of doing this
View v= this.getView(previousPosition, null, null);
change your drawable on item click method
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Button b = (Button) view.findViewById(R.id.buttonPlay);
b.setBackground(c.getResources().getDrawable(R.drawable.ic_action_play));
your_adapter.notifyDataSetChanged();
}
});
I think , this will work for you :)
I think of you question as a partial refresh one. We all run into partial refresh problem when dealing with ListView or GridView. As for your case, partial refresh may be like this:
private void refreshPartially(int position){
int firstVisiblePosition = listview.getFirstVisiblePosition();
int lastVisiblePosition = listview.getLastVisiblePosition();
if(position>=firstVisiblePosition && position<=lastVisiblePosition){
View view = listview.getChildAt(position - firstVisiblePosition);
if(view.getTag() instanceof ViewHolder){
ViewHolder vh = (ViewHolder)view.getTag();
//holder.play.setBackgroundResource(resId);//Do something here.
...
}
}
}