Before expand
After expand
İf height match_parent override other views
I WANT TO GET LİKE THİS EXPAND AND CLOSE
I WANT TO GET LİKE THİS EXPAND AND CLOSE OTHER EXPANDABLE LİST VİEWS
It is my xml file for showing expandable list etc.
<?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:orientation="vertical"
>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="180dp"
android:background="#drawable/sizeozel_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="SİZE ÖZEL"
android:textSize="24sp"
android:layout_marginTop="55dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="95dp"
/>
<ExpandableListView
android:id="#+id/exp_referans_linkim"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_marginTop="105dp"
android:layout_weight="1"
></ExpandableListView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="165dp"
/>
<ExpandableListView
android:id="#+id/exp_satislarim"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_marginTop="175dp"
android:layout_weight="1"
></ExpandableListView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="235dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginTop="245dp"
android:text="İş Ortaklarım"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="270dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginTop="280dp"
android:text="Sepetim"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="305dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginTop="315dp"
android:text="Alınan Ürünler"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="340dp"
/>
<ExpandableListView
android:id="#+id/exp_bilgilerim"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_marginTop="355dp"
android:layout_weight="1"
></ExpandableListView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/size_ozel_divider"
android:layout_marginTop="420dp"
/>
</RelativeLayout>
My adapter java file for expandable list view
package arabulkazan.albatros.com.arabulkazan.Adapters;
import android.content.Context;
import android.database.DataSetObserver;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
import arabulkazan.albatros.com.arabulkazan.MainActivity;
import arabulkazan.albatros.com.arabulkazan.R;
import static arabulkazan.albatros.com.arabulkazan.Fragments.SizeOzel.ref_childListMap;
import static arabulkazan.albatros.com.arabulkazan.Fragments.SizeOzel.ref_groupList;
public class Exp_Ref_List_Adapter implements ExpandableListAdapter {
Context context;
public Exp_Ref_List_Adapter(Context context) {
this.context = context;
}
#Override
public void registerDataSetObserver(DataSetObserver dataSetObserver) {
}
#Override
public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
}
#Override
public int getGroupCount() {
return ref_groupList.size();
}
#Override
public int getChildrenCount(int groupIndex) {
String group = ref_groupList.get(groupIndex);
List<String> childInfoList = ref_childListMap.get(group);
return childInfoList.size();
}
#Override
public Object getGroup(int groupIndex) {
return ref_groupList.get(groupIndex);
}
#Override
public Object getChild(int groupIndex, int childIndex) {
String group = ref_groupList.get(groupIndex);
List<String> childInfoList = ref_childListMap.get(group);
return childInfoList.get(childIndex);
}
#Override
public long getGroupId(int groupIndex) {
return groupIndex;
}
#Override
public long getChildId(int groupIndex, int childIndex) {
return childIndex;
}
#Override
public boolean hasStableIds() {
return true;
}
// This method will return a View object displayed in group list item.
#Override
public View getGroupView(int groupIndex, boolean isExpanded, View view, ViewGroup viewGroup) {
// Create the group view object.
LinearLayout groupLayoutView = new LinearLayout(context);
groupLayoutView.setOrientation(LinearLayout.HORIZONTAL);
// Create and add a textview in returned group view.
String groupText = ref_groupList.get(groupIndex);
TextView groupTextView = new TextView(context);
groupTextView.setText(groupText);
groupTextView.setTextSize(30);
groupLayoutView.addView(groupTextView);
return groupLayoutView;
}
// This method will return a View object displayed in child list item.
#Override
public View getChildView(int groupIndex, int childIndex, boolean isLastChild, View view, ViewGroup viewGroup) {
// First get child text/
Object childObj = this.getChild(groupIndex, childIndex);
String childText = (String)childObj;
// Create a TextView to display child text.
TextView childTextView = new TextView(context);
childTextView.setText(childText);
childTextView.setTextSize(20);
// childTextView.setBackgroundColor(Color.GREEN);
// Set child textview offset left. Then it will align to the right of the group image.
childTextView.setPadding(15,0,0,0);
return childTextView;
}
#Override
public boolean isChildSelectable(int groupIndex, int childIndex) {
return false;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEmpty() {
return false;
}
#Override
public void onGroupExpanded(int groupIndex) {
}
#Override
public void onGroupCollapsed(int groupIndex) {
}
#Override
public long getCombinedChildId(long groupIndex, long childIndex) {
return 0;
}
#Override
public long getCombinedGroupId(long groupIndex) {
return 0;
}
}
And my main fragment java code
public class Main extends android.support.v4.app.Fragment {
public static List<String> ref_groupList = null;
public static Map<String, List<String>> ref_childListMap = null;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root=inflater.inflate(R.layout.fragment_main_page, container, false);
this.referans_linkim("Referans Linkim","12345678","kobi45678");
Exp_Ref_List_Adapter exp_ref_list_adapter=new Exp_Ref_List_Adapter(SizeOzel.this.getActivity());
final ExpandableListView exp_ref = (ExpandableListView)root.findViewById(R.id.exp_referans_linkim);
DisplayMetrics metrics = new DisplayMetrics();//for determine indicator position
SizeOzel.this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
final float scale = getResources().getDisplayMetrics().density;
int elli= (int) (50 * scale + 0.5f);
int on= (int) (10 * scale + 0.5f);
exp_ref.setIndicatorBounds(width-elli,width-on);
exp_ref.setAdapter(exp_ref_list_adapter);
// Add on group expand listener.
exp_ref.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupIndex) {
// Get total group size.
int groupListSize = ref_groupList.size();
// Close other expanded group.
for(int i=0;i < groupListSize; i++) {
if(i!=groupIndex) {
exp_ref.collapseGroup(i);
}
}
}
});
// Referansım linkim data assign
private void referans_linkim(String name,String temsil_kod,String kobi_kod)
{
if(this.ref_groupList == null)
{
this.ref_groupList = new ArrayList<String>();
}
if(this.ref_childListMap == null)
{
this.ref_childListMap = new HashMap<String, List<String>>();
}
if(!this.ref_groupList.contains(name)) {
this.ref_groupList.add(name);
}
// Create child list.
List<String> childList = new ArrayList<String>();
childList.add("Temsilci Kodu: " + temsil_kod);
childList.add("Kobi Kodu: " + kobi_kod);
// Add child data list in the map, key is group name.
this.ref_childListMap.put(name, childList);
}
}
When i try to expand my expandable listview its expanded.But it compress itself to determine width and height.I want to float items that under expandable list.How can i do that? Thanks in advance
Related
I want to create an expandable list with viewpagers as subviews. The problem is now that with my code it calls the getChildView method twice and thus creates my viewpager twice.
I also tried it with the TextView (which is the sample code I posted here for convenience) to simplify the code but the same issue arose.
My best guess is it has something to do with the height property of the layouts but no matter how i changed it, I couldn't resolve the issue.
Please help. I am totally lost. I used a tutorial for this code and I seem to be the only who has that issue judging from the youtube comment section. (https://www.youtube.com/watch?v=jZxZIFnJ9jE)
(If you need any other part of the code please let me know.)
delete.xml file looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sukahead">
<TextView
android:id="#+id/suka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="suka"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingStart="?android:attr/expandableListPreferredChildPaddingLeft"
/>
</LinearLayout>
sublist.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="wrap_content"
android:padding="8dp" android:background="#color/white"
android:id="#+id/sublist">
<TextView
android:id="#+id/dict_entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingStart="?android:attr/expandableListPreferredChildPaddingLeft"
android:textSize="16sp"
android:textColor="#color/colorAccent"
android:text="test"
/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#color/colorPrimary">
<EditText
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:layout_margin="15dp"
android:padding="5dp"
app:layout_constraintTop_toTopOf="parent"
android:background="#color/white"
android:adjustViewBounds="true"
android:maxLines="1"
android:inputType="text"
android:onClick="lookup_word"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search_bar"
android:orientation="horizontal">
<ExpandableListView
android:id="#+id/search_result_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#333"
android:dividerHeight="1dp"
android:background="#color/white"
android:layout_margin="15dp"/>
</LinearLayout>
</RelativeLayout>
ExpandableListAdapter.java
package com.lunaticcoding.linguodict;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.view.ViewPager;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpendableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> data;
private HashMap<String, String[]> listHashMap;
private LayoutInflater inflater;
private String pageData[];
public ExpendableListAdapter(Context context, List<String> list, HashMap<String, String[]> hashMap) {
this.context = context;
this.data = list;
this.listHashMap = hashMap;
}
#Override
public int getGroupCount() {
return data.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listHashMap.get(data.get(groupPosition)).length;
}
#Override
public Object getGroup(int groupPosition) {
return data.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return listHashMap.get(data.get(groupPosition))[childPosition];
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.sublist, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.dict_entry);
textView.setText(data.get(groupPosition));
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String)getChild(groupPosition, childPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.delete, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.suka);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MainActivity (only OnCreate)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBar = (EditText) findViewById(R.id.search_bar);
searchResults = (ExpandableListView) findViewById(R.id.search_result_list);
list_results = new ArrayList<>();
examples_results = new HashMap<>();
list_results.add("test1");
list_results.add("test2");
list_results.add("test3");
String pageData1[] = new String[]{"si", "siiii"};
String pageData2[] = new String[]{"jifasfa", "sfasdfasiii"};
String pageData3[] = new String[]{"jifasfa", "sfasdfasiii"};
examples_results.put(list_results.get(0), pageData1);
examples_results.put(list_results.get(1), pageData2);
examples_results.put(list_results.get(2), pageData3);
searchResultsAdapter = new ExpendableListAdapter(this, list_results, examples_results);
searchResults.setAdapter(searchResultsAdapter);
lastExpandedPosition = -1;
searchResults.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if(lastExpandedPosition != -1 && (lastExpandedPosition != groupPosition)){
searchResults.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
}
In the sub_list.xml the layout_height is "wrap_content". Change it to a fixed height (some dp) or match_parent and it will work.
Reason:
wrap_content has to calculate the height of the layout content it has to match thus reloading the layout afterwards -> creating the list more than once.
I want to display select all as default text in first spinner and for this I used custom adapter but I am unable to do this.
I set code of adapter to bind data in spinner like this:
private void getClientGroup() {
final APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
final FilterModel filterModel = new FilterModel("G", String.valueOf(clientid), String.valueOf(loginid), "A", String.valueOf(userrole));
Call<FilterModel> call1 = apiInterface.createClientGroup(filterModel);
call1.enqueue(new Callback<FilterModel>() {
#Override
public void onResponse(Call<FilterModel> call, Response<FilterModel> response) {
FilterModel filterResponse = response.body();
Log.e("Getclientgroup:", "success");
for (int i = 0; i < filterResponse.getT0().size(); i++) {
int groupId = filterResponse.getT0().get(i).getID();
String groupText = filterResponse.getT0().get(i).getTEXT();
Log.e("Groupid==", String.valueOf(groupId));
Log.e("GroupText==", String.valueOf(groupText));
groupTextList.add(groupText);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, android.R.id.text1, groupTextList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spGroup.setAdapter(
new NothingSelectedSpinner(
dataAdapter,
R.layout.nothing_selected_spinner_item, 0,
getActivity()));
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(R.layout.nothing_selected_spinner_item);
// attaching data adapter to spinner
spGroup.setAdapter(dataAdapter);
}
}
#Override
public void onFailure(Call<FilterModel> call, Throwable t) {
Log.e("Error is==", t.getMessage());
}
});
I am using Nothingselected adapter for this:
public class NothingSelectedSpinner implements SpinnerAdapter, ListAdapter {
protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
public NothingSelectedSpinner(SpinnerAdapter spinnerAdapter,
int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
this.adapter = spinnerAdapter;
this.context = context;
this.nothingSelectedLayout = nothingSelectedLayout;
this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
layoutInflater = LayoutInflater.from(context);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return null;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
// This provides the View for the Selected Item in the Spinner, not
// the dropdown (unless dropdownView is not set).
if (position == 0) {
return getNothingSelectedView(parent);
}
return adapter.getView(position - EXTRA, null, parent); // Could re-use
// the convertView if possible.
}
protected View getNothingSelectedDropdownView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
}
#Override
public int getItemViewType(int position) {
return 0;
}
#Override
public int getViewTypeCount() {
return 1;
}
#Override
public long getItemId(int position) {
return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
}
#Override
public boolean hasStableIds() {
return adapter.hasStableIds();
}
#Override
public boolean isEmpty() {
return adapter.isEmpty();
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
adapter.registerDataSetObserver(observer);
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
adapter.unregisterDataSetObserver(observer);
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return position != 0; // Don't allow the 'nothing selected'
// item to be picked.
}
protected View getNothingSelectedView(ViewGroup parent) {
return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}
}
and layout for this adapter is:
<TextView
android:id="#+id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:padding="#dimen/dp10"
android:textSize="#dimen/dp30"
android:textColor="#color/colorBlack"
android:text="Select all" />
and main layout of spinner is:
<LinearLayout
android:id="#+id/ll_filter_report"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp20"
android:layout_marginTop="#dimen/dp20"
android:layout_marginRight="#dimen/dp20"
android:background="#drawable/rounded_layout_white"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll_sub_group"
android:layout_width="match_parent"
android:layout_height="#dimen/dp80"
android:orientation="horizontal"
android:padding="#dimen/dp20"
android:weightSum="1">
<TextView
android:id="#+id/lbl_sub_group"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center"
android:text="#string/str_sub_group"
android:textSize="#dimen/dp20" />
<Spinner
android:id="#+id/spn_sub_group"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"></Spinner>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_client"
android:layout_width="match_parent"
android:layout_height="#dimen/dp80"
android:layout_marginTop="#dimen/dp10"
android:orientation="horizontal"
android:padding="#dimen/dp20"
android:weightSum="1">
<TextView
android:id="#+id/lbl_client"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center"
android:text="#string/str_client"
android:textSize="#dimen/dp20" />
<Spinner
android:id="#+id/spn_client"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"></Spinner>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_date"
android:layout_width="match_parent"
android:layout_height="#dimen/dp80"
android:layout_marginTop="#dimen/dp10"
android:orientation="horizontal"
android:padding="#dimen/dp20"
android:weightSum="1">
<TextView
android:id="#+id/lbl_date"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="left|center"
android:text="#string/str_as_on"
android:textSize="#dimen/dp20" />
<EditText
android:id="#+id/edt_date"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:hint="02/01/2019"
android:padding="#dimen/dp10"></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dp20"
android:orientation="horizontal"
android:padding="#dimen/dp10"
android:weightSum="1">
<Button
android:id="#+id/btn_Search"
android:layout_width="#dimen/dp0"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#color/colorLightBlue"
android:text="#string/str_search"
android:textAllCaps="false"
android:textColor="#color/colorWhite" />
<Button
android:id="#+id/btn_reset"
android:layout_width="#dimen/dp0"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/dp10"
android:layout_weight="0.5"
android:background="#color/colorLightBlue"
android:text="#string/str_reset"
android:textAllCaps="false"
android:textColor="#color/colorWhite" />
</LinearLayout>
</LinearLayout>
I want to display all the items in second spinner if I select default text select all at index 0 of first spinner how I do this?
How to place an Expandable ListView inside a RecyclerView ListView how to do this?
Like this
without Expanding ExpandingListView
with expanding Expanding ExpandableListView
used Expandable Listview inside RecyclerView.
all are working fine but while expanding we cannot view the child items when given height as wrap content in Recycler view item height.
to view the same given a fixed height for Recyclerview item height, but it is not efficient.
any other method to do this?
Thanks in advance
Main RecyclerView Adapter
public class MySnatchitsListAdapterNew extends RecyclerView.Adapter<MySnatchitsListAdapterNew.SnatchitViewHolder> {
Context context;
ArrayList<Promotion> snatchitslist = new ArrayList<>();
LayoutInflater inflater;
AdapterInterface adapterInterface;
boolean isworkinginbackground = false;
SimpleTooltip tooltip;
public void setIsworkinginbackground(boolean isworkinginbackground) {
this.isworkinginbackground = isworkinginbackground;
}
public MySnatchitsListAdapterNew(Context context, ArrayList<Promotion> snatchitslist, AdapterInterface adapterinterface) {
this.context = context;
this.snatchitslist = snatchitslist;
this.inflater = LayoutInflater.from(context);
this.adapterInterface = adapterinterface;
}
#Override
public SnatchitViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.my_snatchit_list_item_new, null, false);
return new SnatchitViewHolder(view);
}
#Override
public void onBindViewHolder(final SnatchitViewHolder holder, int position) {
Promotion item = snatchitslist.get(position);
CommonUtils.loadProductImage(context, item.getPromotionImage(), R.drawable.no_product_image, R.drawable.no_product_image, holder.img_product);
holder.swtch_continue.setOnCheckedChangeListener(null);
holder.swtch_continue.setChecked((item.getIsDiscontinued() == 0));
holder.txt_view_count.setText(item.getViewcount() + "");
holder.txt_purchase_count.setText(item.getPurchasecount() + "");
holder.adapter = new ProductExpandableListAdapter(context,snatchitslist.get(position));
holder.exp_products.setAdapter(holder.adapter);
if (item.isdiscontinueinprogress()) {
holder.pb_discontinue.setVisibility(View.VISIBLE);
holder.swtch_continue.setVisibility(View.INVISIBLE);
} else {
holder.pb_discontinue.setVisibility(View.INVISIBLE);
holder.swtch_continue.setVisibility(View.VISIBLE);
holder.swtch_continue.setTag(position);
if (item.isdeleting() || (isworkinginbackground)) {
holder.swtch_continue.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(!isChecked);
CommonUtils.showToast(context, context.getString(R.string.operation_in_progress));
}
});
} else {
holder.swtch_continue.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = (int) buttonView.getTag();
adapterInterface.doAction(MySnatchitsFragment.DISCONTINUE_PROMOTION, pos, isChecked);
}
});
}
}
holder.llyt_my_snatchit.setTag(position);
holder.llyt_my_snatchit.setTag(R.id.key_holder, holder);
holder.llyt_my_snatchit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isworkinginbackground) {
int pos = (int) v.getTag();
SnatchitViewHolder holder = (SnatchitViewHolder) v.getTag(R.id.key_holder);
getQRCodeDetails(holder, pos);
} else {
CommonUtils.showToast(context, context.getString(R.string.operation_in_progress));
}
}
});
holder.btn_post.setTag(position);
holder.btn_post.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (int) v.getTag();
Promotion item = snatchitslist.get(pos);
if ((isworkinginbackground) || (item.isdeleting()) || (item.isdiscontinueinprogress())) {
CommonUtils.showToast(context, context.getString(R.string.operation_in_progress));
} else {
adapterInterface.doAction(MySnatchitsFragment.POST_PROMOTION, pos);
}
}
});
public void hideToolTip() {
if ((tooltip != null) && (tooltip.isShowing())) {
tooltip.dismiss();
}
}
public boolean istooltipshowing() {
boolean isshowing = false;
if ((tooltip != null) && (tooltip.isShowing())) {
isshowing = true;
}
return isshowing;
}
public class SnatchitViewHolder extends RecyclerView.ViewHolder {
ImageView img_product;
Switch swtch_continue;
Button btn_post;
LinearLayout llyt_my_snatchit;
ProgressBar pb_discontinue;
TextView txt_view_count, txt_purchase_count;
ImageView img_view_count, img_purchase_count;
ExpandableListView exp_products;
ProductExpandableListAdapter adapter ;
public SnatchitViewHolder(View itemView) {
super(itemView);
img_product = (ImageView) itemView.findViewById(R.id.img_product);
swtch_continue = (Switch) itemView.findViewById(R.id.swtch_continue);
btn_post = (Button) itemView.findViewById(R.id.btn_post);
llyt_my_snatchit = (LinearLayout) itemView.findViewById(R.id.llyt_my_snatchit);
pb_discontinue = (ProgressBar) itemView.findViewById(R.id.pb_discontinue);
txt_view_count = itemView.findViewById(R.id.txt_view_count);
txt_purchase_count = itemView.findViewById(R.id.txt_purchase_count);
img_view_count = itemView.findViewById(R.id.img_view_count);
img_purchase_count = itemView.findViewById(R.id.img_purchase_count);
exp_products = itemView.findViewById(R.id.exp_products);
}
}
}
layout - my_snatchit_list_item_new
<?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">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:animateLayoutChanges="true"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="8dp"
app:cardElevation="4dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llyt_my_snatchit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="horizontal"
android:padding="8dp">
<ImageView
android:id="#+id/img_product"
android:layout_width="#dimen/product_image_width"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/no_product_image" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:animateLayoutChanges="true"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/exp_products"
android:layout_width="match_parent"
android:groupIndicator="#null"
android:layout_height="wrap_content">
</ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginTop="4dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/img_view_count"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/camera" />
<com.integral.app.snatchit.ui.FontTextView
android:id="#+id/txt_view_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textColor="#color/form_text_color"
android:textAppearance="#style/TextAppearance.AppCompat.Medium" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="#+id/img_purchase_count"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/bag" />
<com.integral.app.snatchit.ui.FontTextView
android:id="#+id/txt_purchase_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textColor="#color/form_text_color"
android:textAppearance="#style/TextAppearance.AppCompat.Medium" />
</LinearLayout>
</LinearLayout>
<com.integral.app.snatchit.ui.FontButton
android:id="#+id/btn_post"
android:layout_width="wrap_content"
android:layout_height="#dimen/button_height_small"
android:background="#drawable/post_background"
android:elevation="2dp"
android:minWidth="0dp"
android:paddingBottom="4dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="4dp"
android:text="#string/post"
android:textColor="#color/clickable_white_text_color" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:visibility="gone"
android:animateLayoutChanges="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:animateLayoutChanges="true"
>
<Switch
android:id="#+id/swtch_continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:checked="false"
android:gravity="center_vertical"
android:textOff="no"
android:textOn="yes"
android:thumb="#drawable/swith_thumb"
android:track="#drawable/swith_track"
android:visibility="visible" />
</LinearLayout>
<ProgressBar
android:id="#+id/pb_discontinue"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:animateLayoutChanges="true"
android:padding="2dp"
android:visibility="invisible" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Inner Expandable ListView Adapter - ProductExpandableListAdapter
package com.integral.app.snatchit.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.integral.app.snatchit.R;
import com.integral.app.snatchit.model.Product;
import com.integral.app.snatchit.model.Promotion;
import com.integral.app.snatchit.util.CommonUtils;
/**
* Created by sanoop on 23-01-2018.
*/
public class ProductExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
Promotion promotion;
public ProductExpandableListAdapter(Context context, Promotion promotion) {
this.context = context;
this.promotion = promotion;
}
#Override
public int getGroupCount() {
return 1;
}
#Override
public int getChildrenCount(int groupPosition) {
int childcount = (promotion.getProducts().size() - 2);
return (childcount<0)? 0 : childcount;
}
#Override
public Object getGroup(int groupPosition) {
return promotion;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return promotion.getProducts().get(childPosition + 2);
}
#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) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.my_snatchit_product_item_header, null, false);
ProductHeaderViewHolder holder = new ProductHeaderViewHolder(convertView);
convertView.setTag(holder);
}
ProductHeaderViewHolder holder = (ProductHeaderViewHolder) convertView.getTag();
if (promotion.getProducts().size() > 0) {
Product firstproduct = promotion.getProducts().get(0);
holder.txt_product_name_1.setText(firstproduct.getProductName());
String currencysymbol = CommonUtils.getCurrencySymbol(promotion.getCurrencyCode());
holder.txt_local_price_1.setText(currencysymbol + firstproduct.getProductPriceLocal());
holder.txt_international_price_1.setText(currencysymbol + firstproduct.getProductPriceInternational());
}
if (promotion.getProducts().size() > 1) {
holder.llyt_product_2.setVisibility(View.VISIBLE);
Product firstproduct = promotion.getProducts().get(1);
holder.txt_product_name_2.setText(firstproduct.getProductName());
String currencysymbol = CommonUtils.getCurrencySymbol(promotion.getCurrencyCode());
holder.txt_local_price_2.setText(currencysymbol + firstproduct.getProductPriceLocal());
holder.txt_international_price_2.setText(currencysymbol + firstproduct.getProductPriceInternational());
} else {
holder.llyt_product_2.setVisibility(View.GONE);
}
if (promotion.getProducts().size() > 2) {
holder.img_show_more.setVisibility(View.VISIBLE);
holder.img_show_more.setImageResource(isExpanded ? R.drawable.drop_up : R.drawable.drop_down);
} else {
holder.img_show_more.setVisibility(View.GONE);
}
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.my_snatchit_product_item_child, null, false);
ProductChildViewHolder holder = new ProductChildViewHolder(convertView);
convertView.setTag(holder);
}
ProductChildViewHolder holder = (ProductChildViewHolder) convertView.getTag();
Product childproduct = promotion.getProducts().get(childPosition+2);
holder.txt_product_name.setText(childproduct.getProductName());
String currencysymbol = CommonUtils.getCurrencySymbol(promotion.getCurrencyCode());
holder.txt_local_price.setText(currencysymbol + childproduct.getProductPriceLocal());
holder.txt_international_price.setText(currencysymbol + childproduct.getProductPriceInternational());
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public class ProductHeaderViewHolder {
TextView txt_product_name_1, txt_local_price_1, txt_international_price_1, txt_product_name_2, txt_local_price_2, txt_international_price_2;
FrameLayout flyt_remove;
ProgressBar pb_remove;
LinearLayout llyt_product_2;
ImageView img_show_more;
public ProductHeaderViewHolder(View itemview) {
txt_product_name_1 = itemview.findViewById(R.id.txt_product_name_1);
txt_product_name_2 = itemview.findViewById(R.id.txt_product_name_2);
txt_local_price_1 = itemview.findViewById(R.id.txt_local_price_1);
txt_local_price_2 = itemview.findViewById(R.id.txt_local_price_2);
txt_international_price_1 = itemview.findViewById(R.id.txt_international_price_1);
txt_international_price_2 = itemview.findViewById(R.id.txt_international_price_2);
flyt_remove = itemview.findViewById(R.id.flyt_remove);
pb_remove = itemview.findViewById(R.id.pb_remove);
llyt_product_2 = itemview.findViewById(R.id.llyt_product_2);
img_show_more = itemview.findViewById(R.id.img_show_more);
}
}
public class ProductChildViewHolder {
TextView txt_product_name, txt_local_price, txt_international_price;
public ProductChildViewHolder(View itemview) {
txt_product_name = itemview.findViewById(R.id.txt_product_name);
txt_local_price = itemview.findViewById(R.id.txt_local_price);
txt_international_price = itemview.findViewById(R.id.txt_international_price);
}
}
}
ExpandableListAdapter view not able to include because of body size limitaion.
but it is simple textviews not much complicated ones.
compile 'net.cachapa.expandablelayout:expandablelayout:2.9.1' use this library for expandable layout.
My problem is, that I have an activity with one big TextView and one ExpandableListView underneath the TextView. Unfortunately the text of the TextView is too long, so I can't see the details when I expand the ExpandableListView.
So my question is, how can I use CWAC-Merge in my Code, to get one big scrollable Activity.
I would like to have, that everything in the activity_contact.xml from scrollView1 to the bottom is scrollable! I already copied the necessary libs (merge-1.0.1.jar and sacklist-1.0.0.jar) to my Project, but I don't know how to apply the MergeAdapter. I would really appreciate, if someone could tell me where and how I have to use the MergeAdapter!
This is my activity_contact.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/titlebar" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:fillViewport="true" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/contact"
android:textSize="#dimen/textSize" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ExpandableListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
</ExpandableListView>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
This is my Contact.java class:
public class Contact extends Activity {
private SparseArray<Group> groups = new SparseArray<Group>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
createData();
ExpandableListAdapter adapter = new ExpandableListAdapter(this, groups,
false, null);
listView.setAdapter(adapter);
}
private void createData() {
Group group1 = new Group(getString(R.string.stringGroup));
group1.children.add(getString(R.string.stringGroup2));
groups.append(0, group1);
}
}
This is my ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
private LayoutInflater inflater;
private Activity activity;
private boolean setOnClickListener;
private Class<?> onClickClass;
public ExpandableListAdapter(Activity act, SparseArray<Group> groups,
boolean setOnClickListener, Class<?> onClickListenerClass) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
this.setOnClickListener = setOnClickListener;
this.onClickClass = onClickListenerClass;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
TextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (TextView) convertView.findViewById(R.id.copyrightTextView);
text.setText(Html.fromHtml(children));
if (setOnClickListener) {
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, onClickClass);
intent.putExtra("name", children);
activity.startActivity(intent);
}
});
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
Group group = (Group) getGroup(groupPosition);
((CheckedTextView) convertView).setText(group.string);
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
The listrow_details.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/copyrightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="15dp"
android:gravity="center_vertical"
android:linksClickable="true"
android:padding="10dp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/black" />
</LinearLayout>
MergeAdapter is a ListAdapter, not an ExpandableListAdapter. You cannot use MergeAdapter to solve your problem. You are welcome to read through the source code to MergeAdapter and attempt to figure out if there is a way to create a MergeExpandableListAdapter or something like that.
Or, come up with a UI that does not have a long TextView after the ExpandableListView.
Trying to create an ExpandableListView. The initial view with the groups shows up fine. However, when I click the list item, my arrow does not change. See the images below.
How can I change the arrow's direction?
The layout XML:
<ExpandableListView
android:id="#+id/expandable_list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:divider="#null"
android:background="#ffffff"
android:groupIndicator="#drawable/settings_selector"
android:transcriptMode="alwaysScroll" />
settings_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/arrow_down"
android:state_empty="true"/>
<item
android:drawable="#drawable/arrow_right"
android:state_expanded="true"/>
</selector>
</animation-list>
expandable listview
<ExpandableListView
android:id="#+id/expandable_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="#drawable/group_indicator"
android:transcriptMode="alwaysScroll" />
setindicator here iam useing setindicator code like this this working nice
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
public int GetPixelFromDips(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
res/drawable/group_indicator
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/arrow_right" android:state_empty="true"> </item>
<item android:drawable="#drawable/arrow_down" android:state_expanded="true"></item>
<item android:drawable="#drawable/arrow_right"></item>
</selector>
Try that for your settings_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/arrow_right"
android:state_expanded="true" />
<item
android:drawable="#drawable/arrow_down" />
</selector>
I had gone the way below: decide the left/right drawable for your groupView based on isExpanded flag.
By that way, it is easier for us to customize the padding/background and other things of the indicator drawable.
Hope it helps.
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = (TextView) mLayoutInflater.inflate(R.layout.menu_group, null);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, isExpanded ? 0 : android.R.drawable.ic_menu_more, 0);
textView.setText(getGroup(groupPosition).toString());
return textView;
}
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class MyActivity extends Activity {
private ExpandableListView mExpandableList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
mExpandableList.setGroupIndicator(null);
ArrayList<Parent> arrayParents = new ArrayList<Parent>();
ArrayList<String> arrayChildren = new ArrayList<String>();
//here we set the parents and the children
for (int i = 0; i < 10; i++){
//for each "i" create a new Parent object to set the title and the children
Parent parent = new Parent();
parent.setTitle("Parent " + i);
arrayChildren = new ArrayList<String>();
for (int j = 0; j < 10; j++) {
arrayChildren.add("Child " + j);
}
parent.setArrayChildren(arrayChildren);
//in this array we add the Parent object. We will use the arrayParents at the setAdapter
arrayParents.add(parent);
}
//sets the adapter that provides data to the list.
mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));
}
public class Parent {
private String mTitle;
private ArrayList<String> mArrayChildren;
public String getTitle() {
return mTitle;
}
public void setTitle(String mTitle) {
this.mTitle = mTitle;
}
public ArrayList<String> getArrayChildren() {
return mArrayChildren;
}
public void setArrayChildren(ArrayList<String> mArrayChildren) {
this.mArrayChildren = mArrayChildren;
}
}
public class MyCustomAdapter extends BaseExpandableListAdapter implements OnClickListener{
private LayoutInflater inflater;
private ArrayList<Parent> mParent;
public MyCustomAdapter(Context context, ArrayList<Parent> parent){
mParent = parent;
inflater = LayoutInflater.from(context);
}
#Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
#Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i) {
return mParent.get(i).getArrayChildren().size();
}
#Override
//gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getTitle();
}
#Override
//gets the name of each item
public Object getChild(int i, int i1) {
return mParent.get(i).getArrayChildren().get(i1);
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i1) {
return i1;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
}
view.findViewById(R.id.button).setTag(i);
view.findViewById(R.id.button).setOnClickListener(this);
TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
//"i" is the position of the parent/group in the list
textView.setText(getGroup(i).toString());
//return the entire view
return view;
}
#Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
textView.setText(mParent.get(i).getArrayChildren().get(i1));
//return the entire view
return view;
}
#Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
}
/* (non-Javadoc)
* #see android.view.View.OnClickListener#onClick(android.view.View)
* #since Mar 20, 2013
* #author rajeshcp
*/
#Override
public void onClick(View v) {
if(mExpandableList.isGroupExpanded((Integer)v.getTag()))
{
mExpandableList.collapseGroup((Integer)v.getTag());
}else
{
mExpandableList.expandGroup((Integer)v.getTag());
}
}
}
}
Change your MyActivity like this and let me know what else you want ?
subject:
int width = getResources().getDisplayMetrics().widthPixels;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
listView.setIndicatorBounds(width - getPixelValue(40), width - getPixelValue(10));
} else {
listView.setIndicatorBoundsRelative(width - getPixelValue(40), width - getPixelValue(10));
}
and helper method:
public static int getPixelValue(int dp) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
Just Create a view/Imageview where ever u want in the xml of group item example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/expandable_icon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="6dp"
android:src="#drawable/group_icon_not_expanded" />
<TextView
android:id="#+id/group_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_thin"
android:textColor="#android:color/black"
android:textSize="17sp" />
And then for your ExpandableListView use a GroupClickListener to change the image for ImageView programmatically, example :
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
parent.smoothScrollToPosition(groupPosition);
if (parent.isGroupExpanded(groupPosition)) {
ImageView imageView = v.findViewById(R.id.expandable_icon);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.group_icon_not_expanded));
} else {
ImageView imageView = v.findViewById(R.id.expandable_icon);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.group_icon_expanded));
}
return false ;
}
});
I am a bit late to the party but my requirement was something similar to this but something was different.
I had 3 requirements:
if a group have child then show down_arrow (V)
if a group doesn't have a child then no arrow/image
if a group with child expanded then show up_arrow (^)
To achieve something similar to this:
Your ExpandableListView will look something similar to this with android:groupIndicator null
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/darker_gray"
android:dividerHeight="0.5dp"
android:groupIndicator="#null" />
Your group/header layout will look something like this with text and image at the end:
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ivDrawerHeaderItemIcon"
android:layout_width="#dimen/dp_30"
android:layout_height="#dimen/dp_30"
android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/dp_10"
android:layout_marginEnd="#dimen/dp_10"
android:src="#drawable/switch" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/lblListHeader"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dp_5"
android:layout_weight="1"
android:fontFamily="#font/roboto_condensed_regular"
android:gravity="center_vertical"
android:padding="#dimen/dp_10"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="#dimen/sp_16" />
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/ivIcon"
android:layout_width="#dimen/dp_15"
android:layout_height="#dimen/dp_15"
android:layout_gravity="center_vertical"
android:layout_marginStart="#dimen/dp_10"
android:layout_marginEnd="#dimen/dp_10" />
In your BaseExpandableListAdapter's getGroupView() method, use
if (Objects.requireNonNull(expandableListDetail.get(expandableListTitle.get(listPosition))).size() > 0) {
if (isExpanded) {
ivIcon.setImageResource(R.drawable.arrow_up);
} else {
ivIcon.setImageResource(R.drawable.arrow_down);
}
}
Where, expandableListTitle is group and expandableListDetail is its children