Could you please tell me why my image not change when I change my expand and collapse the list view .Both time I got same image ..I will show in below image when I expand it display (+) image .why ?
here is my code .
Main activity
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if(listDataHeader.get(groupPosition) != null)
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
/*List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");*/
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), null);
}
}
Adapter
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
if(this._listDataChild.get(this._listDataHeader.get(groupPosition))!=null)
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
return 0;
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
View ind = convertView.findViewById( R.id.explist_indicator);
if( ind != null ) {
ImageView indicator = (ImageView)ind;
if( getChildrenCount( groupPosition ) == 0 ) {
indicator.setVisibility( View.INVISIBLE );
} else {
indicator.setVisibility( View.VISIBLE );
int stateSetIndex = ( isExpanded ? 1 : 0) ;
Drawable drawable = indicator.getDrawable();
}
}
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#f4f4f4" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#00000000"
android:groupIndicator="#android:color/transparent"
/>
</LinearLayout>
list_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:background="#000000">
<ImageView android:src="#drawable/test"
android:id="#+id/explist_indicator"
android:layout_width="20px"
android:layout_height="20px"/>
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#f9f93d" />
</LinearLayout>
list_item.xml
![<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000000"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>][1]
I solve that problem by own .Like that
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
View ind = convertView.findViewById(R.id.explist_indicator);
if (ind != null) {
ImageView indicator = (ImageView) ind;
if (isExpanded) {
indicator.setImageResource(R.drawable.icon_3);
} else {
indicator.setImageResource(R.drawable.icon_4);
}
if (getChildrenCount(groupPosition) == 0) {
indicator.setVisibility(View.INVISIBLE);
} else {
indicator.setVisibility(View.VISIBLE);
}
}
return convertView;
}
change Image view like that
<ImageView
android:id="#+id/explist_indicator"
android:layout_width="20px"
android:layout_height="20px"
android:src="#drawable/icon_4" />
Related
I am new at app developing. I'm currently working on an app with tabview and an ExpandableListView in one of the tabs, thus why i'm trying to use an expandablelistview in a fragment.
My expandablelistview appears flawlessly when run directly from an activity, but when i'm trying to run it from a fragment, it appears blank.
HomeFragment.java
public class HomeFragment extends Fragment {
ExpandableListView expandableListView;
View homeFragmentView;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
homeFragmentView = inflater.inflate(R.layout.fragment_home, container, false);
expandableListView = (ExpandableListView) homeFragmentView.findViewById(R.id.exp_listview);
return homeFragmentView;
}
#Override
public void onViewCreated(View v, Bundle savedInstanceState){
List<String> Headings = new ArrayList<String>();
List<String> L1 = new ArrayList<String>();
List<String> L2 = new ArrayList<String>();
List<String> L3 = new ArrayList<String>();
HashMap<String, List<String>> ChildList = new HashMap<String, List<String>>();
String heading_items[] = getResources().getStringArray(R.array.header_titles);
String h1[] = getResources().getStringArray(R.array.h1_items);
String h2[] = getResources().getStringArray(R.array.h2_items);
String h3[] = getResources().getStringArray(R.array.h3_items);
for(String title : heading_items){
Headings.add(title);
}
for (String title : h1){
L1.add(title);
}
for (String title : h2){
L2.add(title);
}
for (String title : h3){
L3.add(title);
}
ChildList.put(Headings.get(0), L1);
ChildList.put(Headings.get(1), L2);
ChildList.put(Headings.get(2), L3);
ExpListViewAdapter expListViewAdapter = new ExpListViewAdapter(getActivity(), Headings, ChildList);
expandableListView.setAdapter(expListViewAdapter);
}
}
Adapter class:
public class ExpListViewAdapter extends BaseExpandableListAdapter {
private List<String> header_titles;
private HashMap<String, List<String>> child_titles;
private Context ctx;
ExpListViewAdapter(Context ctx, List<String> header_titles, HashMap<String, List<String>> child_titles){
this.ctx = ctx;
this.child_titles = child_titles;
this.header_titles = header_titles;
}
#Override
public int getGroupCount() {
return header_titles.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return child_titles.get(header_titles.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return header_titles.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return child_titles.get(header_titles.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String title = (String)this.getGroup(groupPosition);
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.parent_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.heading_item);
textView.setTypeface(null, Typeface.BOLD);
textView.setText(title);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String title = (String)this.getChild(groupPosition,childPosition);
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child_layout, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.child_item);
textView.setText(title);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
header (parent) 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="50dp"
android:background="#151515">
<TextView
android:id="#+id/heading_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World"
android:textColor="#ffffff"
android:textSize="25dp"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
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="50dp"
android:background="#252525">
<TextView
android:id="#+id/child_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World"
android:textColor="#f9f9f9"
android:gravity="center_vertical"
android:textSize="20dp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new HomeFragment(), "Home");
viewPagerAdapter.addFragments(new Group_Invites(), "Group invites");
viewPagerAdapter.addFragments(new New_Group(), "New group");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments = new ArrayList<>();
ArrayList<String> tabTitles = new ArrayList<>();
public void addFragments(Fragment fragments, String titles){
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm){
super (fm);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position){
return tabTitles.get(position);
}
}
I tried your code and it is working fine. Also share your activity code from where you have opened your fragment class.
I am using ExpandableListView in an Activity. I have a TextView and a CheckBox in the GroupView . When I click the CheckBox the corresponding list item expands or collapses depending upon its state. I don't want to change the state of a list item on clicking the CheckBox in the GroupView. How can I achieve this?
This is my layout :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomPanel"
android:dividerHeight="0.5dp"
android:drawSelectorOnTop="false"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorRight" />
<LinearLayout
android:id="#+id/bottomPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:id="#+id/bt_delete_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/delete_group" />
<Button
android:id="#+id/bt_add_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/add_group" />
<Button
android:id="#+id/bt_show_select"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/show_select" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
And this is my adapter :
This is my Adapter after edit :
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<RowBean>> expandableListDetail;
public static ArrayList<Integer> selected = new ArrayList<>();
private Realm realm = Realm.getDefaultInstance();
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<RowBean>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {
// for(RowBean r : expandableListDetail.get(expandableListTitle.get(listPosition))){
// Log.e("is select " , r.getTitle() + " " +r.isSelected() + " , " + listPosition + " ," +expandedListPosition);
// }
RowBean rowBean = (RowBean) getChild(listPosition, expandedListPosition);
RowBean rowBean1 = expandableListDetail.get(expandableListTitle.get(listPosition)).get(expandedListPosition);
// Log.e("rowbean " , rowBean1.getTitle() + " , " + rowBean1.isSelected() );
final String expandedListText = rowBean.getTitle();
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_holder, null);
}
TextView expandedListTextView = (TextView) convertView.findViewById(R.id.expandedListItem);
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.expandedListItemCheckbox);
expandedListTextView.setText(expandedListText);
checkBox.setChecked(rowBean.isSelected());
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)).size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(final int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final ViewGroup groupview = parent;
SharedPreferences pref = context.getSharedPreferences("MAIN_PREF", MODE_PRIVATE);
final SharedPreferences.Editor editor = pref.edit();
final String listTitle = (String) getGroup(listPosition);
boolean[] sel = {RealmController.getGroup(listTitle, realm).isSelect()};
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// convertView = layoutInflater.inflate(R.layout.list_group, null);
convertView = layoutInflater.inflate(R.layout.holder_layout, null);
}
// TextView listTitleTextView = (TextView) convertView
// .findViewById(R.id.listTitle);
TextView listTitleTextView = (TextView) convertView.findViewById(R.id.showTextHolder);
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxHolder);
// checkBox.setSelected(true);
// if (pref.getInt(listTitle, 0) == 1) {
// checkBox.setSelected(true);
// } else {
// checkBox.setSelected(false);
// }
final boolean[] st = new boolean[1];
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// if (!checkBox.isChecked()) {
// ExpandableListView gExpandableListView = (ExpandableListView) groupview;
// gExpandableListView.expandGroup(listPosition);
//// checkBox.setChecked(false);
// RealmResults<ObjectsInGroupRealm> objectsInGroupRealms = RealmController.getObjects(realm, listTitle);
// int position = -1;
// for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
// for (ObjectsInGroupRealm o : objectsInGroupRealms) {
// if (o.getName().equals(objectDefExtends.name)) {
// position = Singleton.getInstance().getListaODE().indexOf(objectDefExtends);
// Singleton.getInstance().getListaODE().get(position).visible = false;
// editor.putInt(Singleton.getInstance().getListaODE().get(position).id.toString(), 0);
// editor.apply();
// RealmController.updateGroup(o.getGroupName(), false, realm);
// RealmController.updateObjectsInGroupRealm(o.getGroupName(), realm, context, false);
// FragmentGroupObjectsActivity.getArgument(context);
// }
// }
// }
// selected.remove(Integer.valueOf(listPosition));
// } else {
// ExpandableListView gExpandableListView = (ExpandableListView) groupview;
// gExpandableListView.expandGroup(listPosition);
//// checkBox.setChecked(true);
// selected.add(listPosition);
// RealmResults<ObjectsInGroupRealm> objectsInGroupRealms = RealmController.getObjects(realm, listTitle);
// int position = -1;
// for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
// for (ObjectsInGroupRealm o : objectsInGroupRealms) {
// if (o.getName().equals(objectDefExtends.name)) {
// position = Singleton.getInstance().getListaODE().indexOf(objectDefExtends);
// Singleton.getInstance().getListaODE().get(position).visible = true;
// editor.putInt(Singleton.getInstance().getListaODE().get(position).id.toString(), 1);
// editor.apply();
// RealmController.updateGroup(o.getGroupName(), true, realm);
// st[0] = true;
// checkBox.setChecked(true);
// RealmController.updateObjectsInGroupRealm(o.getGroupName(), realm, context, true);
// FragmentGroupObjectsActivity.getArgument(context);
// }
// }
// }
// }
// FragmentGroupObjectsActivity.getArgument(context);
// FragmentAllObjectActivity.getArgument(context);
// notifyDataSetChanged();
}
});
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
// checkBox.setChecked(false);
RealmResults<ObjectsInGroupRealm> objectsInGroupRealms = RealmController.getObjects(realm, listTitle);
int position = -1;
for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
for (ObjectsInGroupRealm o : objectsInGroupRealms) {
if (o.getName().equals(objectDefExtends.name)) {
position = Singleton.getInstance().getListaODE().indexOf(objectDefExtends);
Singleton.getInstance().getListaODE().get(position).visible = false;
editor.putInt(Singleton.getInstance().getListaODE().get(position).id.toString(), 0);
editor.apply();
RealmController.updateGroup(o.getGroupName(), false, realm);
RealmController.updateObjectsInGroupRealm(o.getGroupName(), realm, context, false);
FragmentGroupObjectsActivity.getArgument(context);
}
}
}
selected.remove(Integer.valueOf(listPosition));
} else {
// checkBox.setChecked(true);
selected.add(listPosition);
RealmResults<ObjectsInGroupRealm> objectsInGroupRealms = RealmController.getObjects(realm, listTitle);
int position = -1;
for (ObjectDefExtends objectDefExtends : Singleton.getInstance().getListaODE()) {
for (ObjectsInGroupRealm o : objectsInGroupRealms) {
if (o.getName().equals(objectDefExtends.name)) {
position = Singleton.getInstance().getListaODE().indexOf(objectDefExtends);
Singleton.getInstance().getListaODE().get(position).visible = true;
editor.putInt(Singleton.getInstance().getListaODE().get(position).id.toString(), 1);
editor.apply();
RealmController.updateGroup(o.getGroupName(), true, realm);
st[0] = true;
checkBox.setChecked(true);
RealmController.updateObjectsInGroupRealm(o.getGroupName(), realm, context, true);
FragmentGroupObjectsActivity.getArgument(context);
}
}
}
}
FragmentGroupObjectsActivity.getArgument(context);
FragmentAllObjectActivity.getArgument(context);
notifyDataSetChanged();
}
});
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
checkBox.setChecked(RealmController.getGroup(listTitleTextView.getText().toString(), realm).isSelect());
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
I added EditText in ExpandableListView in the group item, after that when I'm clicking on the group item, list with children items doesn't appear. What can be the problem? Is it possible this list to be expanded in that case? Or it's forbidden to add EditText in group item in ExpandedListView?
Code of the adapter:
public class ClaimEqpExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
public List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ClaimEqpExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
public ClaimEqpExpandableListAdapter(Context context, List<String> listDataHeader) {
this._context = context;
this._listDataHeader = listDataHeader;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.claim_explist_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.claim_eq_list_group, null);
// convertView = infalInflater.inflate(R.layout.claim_list_group, null);
}
TextView tvWorkNumber = (TextView) convertView
.findViewById(R.id.tvWorkNumber);
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.tvClaimWorkName);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
int displayPosition=groupPosition+1;
tvWorkNumber.setText(String.valueOf(displayPosition));
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void updateData(List<String> groups,
HashMap<String, List<String>> children) {
this._listDataHeader = groups;
this._listDataChild = children;
}
}
claim_eq_list_group.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/tvWorkNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:layout_marginTop="10dp"
android:text="1"
android:textSize="#dimen/doc_title_font_size" />
<TextView
android:id="#+id/tvClaimWorkName"
android:layout_width="match_parent"
android:layout_height="26dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/tvWorkNumber"
android:textSize="#dimen/doc_title_font_size" />
<EditText
android:id="#+id/tvClaimEquip"
android:layout_width="80dp"
android:layout_height="26dp"
android:layout_marginRight="60dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/tvClaimWorkName"
android:inputType="number"
android:textSize="#dimen/doc_title_font_size" />
</RelativeLayout>
claim_explist_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:textSize="17dip" />
</LinearLayout>
I found three solution for this problem:
1) make EditText unfocusable first, and then make it focusable while typing text.
Code in the adapter:
editText.setFocusable(false);
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
editText.setFocusableInTouchMode(true);
return false;
}
});
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) { }
#Override
public void afterTextChanged(Editable s) {
editText.setFocusable(false);
editText.setFocusableInTouchMode(false);
}
2) Make custom Expandable indicator (Button), set on it onClickListener, which will expand and collapse ExpandableListView.
Code in the adapter:
btnIndicator.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ExpandableListView mExpandableListView = (ExpandableListView) mParetn;
if(mExpandableListView.isGroupExpanded(mGroupPosition)){
mExpandableListView.collapseGroup(mGroupPosition);
}else{
mExpandableListView.expandGroup(mGroupPosition);
}
}
});
3) Instead of EditText, make a TextView or Button. on pressing of which appears Alert Dialog with EditText, where user can insert input.
The editText inside the groupheader is focusable and is stealing all the click events. Try to set focusable to false:
android:focusable="false" or yourEditText.setFocusable(false);
set below properties in your xml Edittext.
android:focusable="false"
android:focusableInTouchMode="true"
android:descendantFocusability="blocksDescendants"
if Still not work then add in you listview
android:descendantFocusability="beforeDescendants
And in manifest in that activity add.
android:windowSoftInputMode="adjustPan"
I'm trying to populate a listview with the id android.id/list with some xml data.. The data is successfully requested and I get no errors when passing it to my Lazy Adapter and setting it to the listview.. Can somebody help me?
The request:
private void requestFeed() {
SimpleXmlRequest<StationList> simpleRequest = new SimpleXmlRequest<StationList>(Request.Method.GET, url, StationList.class,
new Response.Listener<StationList>()
{
#Override
public void onResponse(StationList response) {
list = response.getStationList();
buildFeed();
if(mSplashDialog != null) {
removeSplashScreen();
} else {
setReloadFeedButtonState(false);
}
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error Object
Log.d("TESTE", error.toString());
}
}
);
simpleRequest.setTag(SIMPLE_TAG);
queue = Volley.newRequestQueue(this);
queue.add(simpleRequest);
}
The buildFeed:
private void buildFeed() {
if(stations == null) {
stations = (ListView) findViewById(android.R.id.list);
adapter = new LazyAdapter(this, list);
stations.setAdapter(adapter);
}
adapter.notifyDataSetChanged();
}
The adapter:
public class LazyAdapter extends BaseAdapter {
private List<Station> stations;
private static LayoutInflater inflater = null;
public LazyAdapter(Activity activity, List<Station> stations) {
this.stations = stations;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return stations.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView == null)
vi = inflater.inflate(R.layout.list_row, null);
TextView name = (TextView) vi.findViewById(R.id.station_name);
TextView ct = (TextView) vi.findViewById(R.id.station_ct);
TextView cl = (TextView) vi.findViewById(R.id.station_cl);
Station tmp = stations.get(position);
name.setText(tmp.get_name());
ct.setText(tmp.get_ct());
cl.setText(tmp.get_lc().toString());
return vi;
}
}
the xml of the activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/MainActivityLL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me...MainActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:layout_weight="1"
android:listSelector="#drawable/list_row_selector" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="#string/emptyList"
android:gravity="center" />
I've checked the stations list that comes in the response and is not empty.. I have all the data I need, but the listview is always showing the empty message from the textview with id android.id/empty ..
Remove android:id="#android:id/list"
Replace it with android:id="#+id/list"
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:layout_weight="1"
android:listSelector="#drawable/list_row_selector" />
Try to Use ViewHolder design pattern when you make custom adapter :
public class LazyAdapter extends BaseAdapter {
private List<Station> stations;
private Context context;
public LazyAdapter(Context context, List<Station> stations) {
this.stations = stations;
this.context=context;
}
public int getCount() {
return stations.size();
}
public Object getItem(int position) {
return stations.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_row, null);
holder.name = (TextView) convertView.findViewById(R.id.station_name);
holder.ct = (TextView) convertView.findViewById(R.id.station_ct);
holder.cl = (TextView) convertView.findViewById(R.id.station_cl);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(stations.get(position).get_name());
holder.ct.setText(stations.get(position).get_ct());
holder.cl.setText(stations.get(position).get_lc().toString());
return convertView;
}
class ViewHolder{
TextView name;
TextView ct;
TextView cl;
}
}
I have created a navigation drawer with an expandable listview inside it. I want the expandable listview to expand one of its groups and select one of its child by default when the activity opens up. The user may change the selection according to his requirement.
Whatever I have created is not satisfying my requirement. The expandable list is allowing the user to select multiple options.
The MainActivity:
public class MainActivity extends Activity {
DrawerLayout mDrawerLayout;
ExpandableListView mDrawerList;
ExpandableListAdapter listAdapter;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
private ActionBar mActionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Action bar with collapse icon for drawer menu===================
mActionBar = getActionBar();
mActionBar.setHomeButtonEnabled(true);
mActionBar.setDisplayHomeAsUpEnabled(false);
// Action bar with collapse icon for drawer menu===================
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader,
listDataChild);
// setting list adapter
mDrawerList.setAdapter(listAdapter);
// Listview Group click listener
mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// // Listview Group expanded listener
// mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {
//
// #Override
// public void onGroupExpand(int groupPosition) {
// Toast.makeText(getApplicationContext(),
// listDataHeader.get(groupPosition) + " Expanded",
// Toast.LENGTH_SHORT).show();
// }
// });
//
// // Listview Group collasped listener
// mDrawerList.setOnGroupCollapseListener(new OnGroupCollapseListener()
// {
//
// #Override
// public void onGroupCollapse(int groupPosition) {
// Toast.makeText(getApplicationContext(),
// listDataHeader.get(groupPosition) + " Collapsed",
// Toast.LENGTH_SHORT).show();
//
// }
// });
// mDrawerList.expandGroup(2);
// mDrawerList.expandGroup(2, true);
// Listview on child click listener
mDrawerList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
mDrawerLayout.closeDrawer(mDrawerList);
// TODO Auto-generated method stub
// Toast.makeText(
// getApplicationContext(),
// listDataHeader.get(groupPosition)
// + " : "
// + listDataChild.get(
// listDataHeader.get(groupPosition)).get(
// childPosition), Toast.LENGTH_SHORT)
// .show();
v.setBackgroundColor(Color.BLUE);
makeAToast("Group: " + groupPosition + " Child: "
+ childPosition);
return false;
}
});
// To collapse previously opened group===========
mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {
int previousItem = -1;
#Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousItem)
mDrawerList.collapseGroup(previousItem);
previousItem = groupPosition;
}
});
// To collapse previously opened group===========
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding header data
listDataHeader.add("Today");
// Adding child data
List<String> today = new ArrayList<String>();
today.add("Tanushree Guha");
today.add("Prasenjit Roy");
today.add("Pan Singh Tomar");
today.add("Milka Singh");
today.add("Rohit Ramanujan");
today.add("Ramesh Bhatt");
today.add("Debjani Brahma");
listDataHeader.add("Tomorrow");
List<String> tomorrow = new ArrayList<String>();
tomorrow.add("Dipanjan Bhowmik");
tomorrow.add("Sarmistha Sinha");
tomorrow.add("Pranay Lalwani");
tomorrow.add("Mohit Shaw");
tomorrow.add("Lovelace Agarwal");
tomorrow.add("Tanmay Banerjee");
listDataHeader.add("Later");
List<String> later = new ArrayList<String>();
later.add("Yusuf Khan");
later.add("Jitendar Sharma");
later.add("Debashree Roy");
later.add("Mainak Ghosh");
later.add("Olivia Gomes");
listDataChild.put(listDataHeader.get(0), today); // Header, Child data
listDataChild.put(listDataHeader.get(1), tomorrow);
listDataChild.put(listDataHeader.get(2), later);
}
// method to show toast message
public void makeAToast(String str) {
Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
The activity_main.xml layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#999999"
android:dividerHeight="1dp"
android:background="#ffffff"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
The ExpandableListAdapter class:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
If anymore source code is required, I will provide it. What should I do to satisfy my requirement?
For the first part of the question:
I want the expandable listview to expand one of its groups and select
one of its child by default when the activity opens up
mExpandableListView.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
where :
groupPosition is the position of the group that should be expanded first
position of the child that should be selected by default *More info below
shouldExpandGroup is a boolean that should be set to true to expand the group
Info for 2 above.
Maintain a hashmap containing the group postion and the child position selected by the user like:
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(groupPosition, childPosition);
For every child selection simply put it into the hashmap, it would overwrite the value if present
Store this hashmap either in a file or somewhere where you can access it again (may be shared preference but it does not allow storage of objects)
When the activity starts get the value from the hashmap, and set the selection,
you could set the selection for all the groups but with shouldExpand to false for all
except the first. If the map is empty set default selection to 0.
I would recomment using SlideExpandableListView for Android:
(Well that screenshot is crap but you can guess the animation)
This is the official description of that library:
Not happy with the Android ExpandableListView android offers? Want something like the Spotify app. This library allows you to have custom listview in wich each list item has an area that will slide-out once the users clicks on a certain button.
Features
Provides a better ExpandableListView usable for normal ListView's
Animates by default
Easy to use
Check that repository at GitHub: https://github.com/tjerkw/Android-SlideExpandableListView/.