I made a custom view for my ListView
My Custom View extends LinearLayout
, It acts as an expandable Item for a normal ListView
So the bug I am getting now is really weird, So I have my ListView
attributes set to
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
So the height is at wrap_content
lets take a note of that, as that will help us understand the issue.
So this is the video of how my ListVIew looks. The first Expandable
Item is visible but the others don't fill the screen, I am pretty confident this is because of wrap_content
but as soon as I expand one of the item it fill the whole screen. How can I resolve this? My guess is It's because the ListView isn't updating the other items? this is my CustomView code
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import java.util.ArrayList;
import java.util.List;
public class ExpandableInnerListView extends LinearLayout {
private ListAdapter adapter;
private DataChangeObserver dataChangeObserver;
private View mainView;
private List<View> reusableViews = new ArrayList<>();
private boolean viewsHidden = true;
int mDividerHeight = 1;
public ExpandableInnerListView(Context context) {
this(context, null);
}
public ExpandableInnerListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ExpandableInnerListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(VERTICAL);
}
public void setAdapter(ListAdapter adapter) {
if (this.adapter != null && dataChangeObserver != null) {
this.adapter.unregisterDataSetObserver(dataChangeObserver);
}
this.adapter = adapter;
}
public void setMainView(View mainView) {
this.mainView = mainView;
}
public void toggle() {
viewsHidden = !viewsHidden;
dataChangeObserver.onInvalidated();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (adapter != null) {
dataChangeObserver = new DataChangeObserver();
adapter.registerDataSetObserver(dataChangeObserver);
setupView();
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (adapter != null && dataChangeObserver != null) {
adapter.unregisterDataSetObserver(dataChangeObserver);
dataChangeObserver = null;
}
}
private void setupView() {
this.removeAllViews();
setupMainLayout();
setupInnerView();
}
public void setupMainLayout() {
if (mainView == null) {
throw new NullPointerException("Main View cannot be null!");
}
addView(mainView);
}
public void setupInnerView() {
if (viewsHidden) return;
int reusableCount = reusableViews.size();
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
View converView = null;
if (i < reusableCount) {
converView = reusableViews.get(i);
}
View view = adapter.getView(i, converView, this);
if (i >= reusableCount) {
reusableViews.add(view);
}
if(mDividerHeight > 0) {
View divider = new View(getContext());
divider.setBackground(new ColorDrawable(0xFFCCCCCC));
LinearLayout.LayoutParams dividerLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mDividerHeight);
divider.setLayoutParams(dividerLayoutParams);
addView(divider);
}
addView(view);
}
}
private class DataChangeObserver extends DataSetObserver {
@Override
public void onChanged() {
super.onChanged();
setupView();
}
@Override
public void onInvalidated() {
super.onInvalidated();
setupView();
}
}
}
and this is my whole Fragment View 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="match_parent"
android:background="#ffffff">
<include
android:id="@+id/back_button_container"
layout="@layout/kik_back_button" />
<FrameLayout
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="@dimen/native_topbar_height"
android:layout_alignParentTop="true"
android:layout_toEndOf="@id/back_button_container"
android:layout_toRightOf="@id/back_button_container" />
<include
android:id="@+id/nav_bar_shadow"
layout="@layout/navbar_underline"
android:layout_width="fill_parent"
android:layout_height="@dimen/navbar_underline_height"
android:layout_below="@id/topbar" />
<EditText
android:id="@android:id/edit"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@+id/topbar"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:layout_marginEnd="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:drawablePadding="15dp"
android:hint="Search Settings"
android:inputType="textNoSuggestions"
android:maxLines="1"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp"
android:textStyle="normal"
android:typeface="sans" />
<ImageView
android:id="@android:id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignEnd="@android:id/edit"
android:layout_alignRight="@android:id/edit"
android:layout_alignTop="@android:id/edit"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="12dp"
android:src="@drawable/delete_gray"
android:visibility="gone" />
<View
android:id="@+id/top_divider"
android:layout_width="match_parent"
android:layout_height="1.0px"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@android:id/edit"
android:layout_marginTop="15dp"
android:background="@color/list_divider_color" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/top_divider"
android:divider="@null"
android:dividerHeight="0dp" />
<View
android:id="@+id/bottom_divider"
android:layout_width="match_parent"
android:layout_below="@android:id/list"
android:layout_height="1.0px"
android:background="@color/list_divider_color" />
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="No Settings Found"
android:textSize="19.0sp"
android:visibility="gone" />
</RelativeLayout>
Would be really generous if someone can spot where the error happens and how to resolve it, and along side if any other error can happen at certain cases.