I am trying to implement list. Here is start.xml file which have a text box i which i am typing any word and a list of words will open containing this word:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/FrameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
for list view i am using this ListView.xml file:
<ImageView
android:id="#+id/image_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/abacus_thumbnail"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/imageView_color_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/blue_thumbnail" />
<TextView
android:id="#+id/title_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image_list"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/image_list"
android:gravity="center"
android:text="Abacus"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/textView_meaning_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title_list"
android:layout_below="#+id/title_list"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="25sp" />
i am using EfficientAdapetr to convert one View from one to another when my app start the EditBox will be empty and a background image . I have ClickListner on list item when a word is type a list opens and when click on item this word will open. Now problem is that when i click on item of list the background populate with word but listView not remove. on Item click i want to open my background with the word.
Here is my code:
listAdapter = new EfficientAdapter2(this);
setListAdapter(listAdapter);
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.ListView, null);
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
holder.word = (TextView) convertView.findViewById(R.id.title_list);
holder.meaning = (TextView) convertView
.findViewById(R.id.textView_meaning_list);
holder.image = (ImageView) convertView
.findViewById(R.id.image_list);
holder.image_color = (ImageView) convertView
.findViewById(R.id.imageView_color_list);
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
try {
"functionalty here"
} catch (IOException e) {
e.printStackTrace();
}
return convertView;
}
here is onItemClick:
protected void onListItemClick(ListView l, View v, int position, long id) {////////
super.onListItemClick(l, v, position, id);
//Log.i("Position",""+position);
changeEverything(position);// here i am converting my ListView to
Start.xml but its on happening
motherOfIndex = position;
}
first debug your app and check weather onListItemClick() event firing. this won't fire if you have clickable items in your listView. if that's the case you have to set isClickable=false for all the items in ListView.xml file. in order to dismiss the the listView, you have to set it's visibility to View.GONE
However if you want to popup a suggestion list when something type on a EditText, this is not the best practice to do that. you can use a AutoCompleteTextView and directly set your ListView adapter to it. they it will create a listView for your and will automatically handle the set selected text to the AutoCompleteTextView and will dismiss the listView
Related
I am making an app in which there are posts with comments and description. There are three buttons on each post. 1 description 2 comments and third is like button. I set a button click listener in getview method of custom adapter. When i click on description button the description of that post should be displayed under the button. But when i clicked the description button, the description of some other listview items also displayed. I just want to show the description of that post whose description button is clicked. Here is my code:
getview code:
public View getView(int position, View convertView, ViewGroup parent) {
a = getItem(position);
View view = convertView;
try
{
if (convertView == null) {
v = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
v.desc = (Button) convertView.findViewById(R.id.btn_desc);
v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);
convertView.setTag(v);
}
else {
v = (ViewHolder) convertView.getTag();
}
v.desc.setTag(position);
//Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring).into(v.imgView);
v.desc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1) {
Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
v.des.setText(""+a.getDescription());
v.ll.setVisibility(View.VISIBLE);
}
});
return convertView;
}catch (Exception e)
{
return null;
}
}
XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="35dp">
<ImageView
android:id="#+id/iv_list_image"
android:layout_width="match_parent"
android:layout_height="300dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="6">
<Button
android:id="#+id/btn_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Description"
/>
<Button
android:id="#+id/btn_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Comments"
/>
<Button
android:id="#+id/btn_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Likes"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:background="#ffffff"
android:visibility="invisible"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description:"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_list_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Default Desc"
android:textColor="#333"
/>
</LinearLayout>
</LinearLayout>
That's when you use an int flag for the list item's position as a conditional statement before updating the ListView's items.
So in your case, the Adapter class would look something like this:
...
private int mPosition = -1; // Int flag that doesn't take effect UNTIL it has been set
...
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
a = getItem(position);
View view = convertView;
try {
if (convertView == null) {
v = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
v.desc = (Button) convertView.findViewById(R.id.btn_desc);
v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
v.desc.setTag(position);
//Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring)
.into(v.imgView);
// Runs the following functionality if the positions match. Otherwise, hides the layout.
if (mPosition == position) {
Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
v.des.setText(""+a.getDescription());
v.ll.setVisibility(View.VISIBLE);
} else {
v.ll.setVisibility(View.INVISIBLE);
}
v.desc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1) {
mPosition = position; // Sets the flag to the position that was clicked
notifyDataSetChanged(); // Updates the data instantly
}
});
return convertView;
} catch (Exception e) {
return null;
}
}
Let me know if that works.
The best way is to implement the on click listener in adapter and set the tag on button. After that in onclick method perform the desired task through getting tag id of button, this will definitely work.
I have a list view every item in list contains textviews and button what I want is when I click on the button of any item in list I want to printout the position of button if i clicked the button in first line i want to print out 0 and go on but it doesn't work
this is my method to display view
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] {
PhonesDbAdapter.KEY_NAME,
PhonesDbAdapter.KEY_CONTINENT,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.continent,
R.id.name
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.phone_layout,
cursor,
columns,
to,
0);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
public void print(View v)
{
}
and here how my item look like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip"
android:background="#f0f0f0" >
<TextView
android:id="#+id/continent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#275a0d"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="TextView"
android:textColor="#000"/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher"/>
<Button
android:id="#+id/call"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button1"
android:background="#drawable/ic_launcher"
android:onClick="print"/>
</RelativeLayout>
and this the layout that contain the listview
<?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="#f0f0f0">
<EditText android:id="#+id/myFilter" android:layout_width="match_parent"
android:layout_height="wrap_content" android:ems="10">
</EditText>
<ListView android:id="#+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
i spent alot of time with this problem and i hope that u can help me and im really sorry for my bad english
Are you implementing the method getView() on your adapter?
That's where you want to add the OnClickListener to your Button
Once you do that you can set the position as the tag of the Button and retrieve it on the onClick method.
Something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.your_item_layout, parent, false);
Button myButton = (Button) row.findViewById(R.id. call);
myButton.setOnClickListener(new OnClickListener {
#Override
public void onClick(View view) {
int position = (int)view.getTag();
//Do whatever you want with the position here
}
});
myButton.setTag(position);
return row;
}
From last 3 three years I am not working on android so I am not sure my suggestion is correct or not. You need to test it for you purpose.
In your above case you cannot use onItemClickListner because it can only be used when you are tracking your complete list row. What I think is you can create a custom adapter and override its getView method. In getView method you will have your position and you can set button click listner for you button in getview. So In this way you can get your button position.
You try it once if you need some code then I can try to write it for you...
THIS IS HOW YOU CAN CREATE CUSTOM ADAPTER.
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
ArrayList<String> elements = null;
public my_custom_adapter(Context context, int type, ArrayList<String> elements)
{
super(context, type, elements);
this.elements = elements;
this.context = context;
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public Button cb;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (Button) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// IF YOUR BUTTON TAG HERE AND YOU CAN HAVE POSITION USING "position" PARAMETER
}
});
}
return rowView;
}
}
I have a custom Listview whose adapter extends BaseAdapter class. Inside every row in listview there is an invisible EditText. it get visible to only particular row which has been click and on rest of the row it remains invisible.Problem comes when I touch edittext to get keyboard, as soon as keyboard appears, listview gets recycle and edittext again become invisible.
I am looking for a solution in which either keyboard comes with focus on edittext as soon as that row is selected or edittext does not disapear when keyboard is poped-up.Following is the adapter I am using:
this is my adapter xml file
saved_option_adapter_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/lineItem"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<TextView
android:id="#+id/textLine"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="15sp"
android:layout_marginBottom="5sp"
android:textSize="14sp"
android:paddingTop="-5dp"
android:layout_width="wrap_content"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<EditText
android:id="#+id/edittext_qty"
android:visibility="invisible"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:singleLine="true"
android:maxLength="4"
android:textSize="14sp"
android:inputType="numberPassword"
android:focusable="true"
android:textColor="#android:color/black"
android:hint="cvv"/>
</LinearLayout>
</RelativeLayout>
and this is my getView Method:
public View getView(final int position, View convertView, final ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.saved_options_adapter_content, null);
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
holder.Edittext = (EditText) convertView.findViewById(R.id.edittext_qty);
editTextList.add(holder.Edittext);
convertView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(final View v)
{
selected_Txt="";
if(view==null || view!=v){
v.post(new Runnable() {
#Override
public void run() {
view=v;
ViewHolder holder = ((ViewHolder)v.getTag());
holder.Edittext.setVisibility(View.VISIBLE);
holder.Edittext.requestFocus();
holder.Edittext.setCursorVisible(true);
holder.Edittext.setFocusable(true);
if(selectedHolder != null ){
selectedHolder.Edittext.setVisibility(View.INVISIBLE);
}
selectedHolder = holder;
}
});
}
}
});
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
((ViewHolder)convertView.getTag()).Edittext.setTag(title.get(position));
}
convertView.setTag(holder);
return convertView;
}
I don't see listview in your code. Maybe ArrayAdapter can help you: http://developer.android.com/reference/android/widget/ArrayAdapter.html
I have a list (ExpandableListView), and every item of the list has a ToggleButton that shows/hides the child items when toggled (the child is always 1 and it's a sort of toolbar with two buttons). Thanks to this tutorial I could set a custom button instead of the expandablelistview's indicator and I made it so that I can do something else than showing the toolbar when clicking on a list item. Also, I used the answer to this question to automatically close an open toolbar when opening another one.
So, I need to collapse the currently expanded toolbar when touching anything on the screen that is not its ToggleButton (I would need to collapse it even when clicking on one of the toolbar's buttons, as long as its own "onClick" is sent anyway).
here's an image of the app:
Here's the ExpandableListView:
<ExpandableListView
android:id="#+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#drawable/toggle_button_selector" >
</ExpandableListView>
Here's the xml of the group item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="#+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:contentDescription="#string/icondescription" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="#+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="#+id/img"
android:layout_below="#+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ToggleButton
android:id="#+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/text"
android:layout_marginRight="15dp"
android:background="#drawable/toggle_button_selector"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="#android:color/transparent"
android:textOff=""
android:textOn="" />
The child item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/details" />
<TextView
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/send" />
</LinearLayout>
The relevant parts of the adapter:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
private final Integer[] imageId;
private int lastExpandedGroupPosition;
public CustomList(Activity context, List<Item> names, Integer[] imageId) {
this.context = context;
this.names = names;
this.imageId = imageId;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_single, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.text);
TextView txtData = (TextView) convertView.findViewById(R.id.counter);
ImageView imageView = (ImageView) convertView.findViewById(R.id.img);
ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
final ExpandableListView list = (ExpandableListView) parent.findViewById(R.id.normalList);
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
}
});
} else if(/*other conditions*/) {
imageView.setImageResource(imageId[2]);
toggle.setVisibility(View.GONE);
} else {
imageView.setImageResource(imageId[1]);
toggle.setVisibility(View.GONE);
}
return convertView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
}
TextView details = (TextView) convertView.findViewById(R.id.details);
TextView send = (TextView) convertView.findViewById(R.id.send);
details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something
}
});
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something else
}
});
return convertView;
}
#Override
public void onGroupExpanded(int groupPosition) {
ToggleButton button = (ToggleButton) ((ExpandableListView) context
.findViewById(R.id.normalList)).getChildAt(lastExpandedGroupPosition)
.findViewById(R.id.toggle);
if(groupPosition != lastExpandedGroupPosition && button.isChecked()) {
button.performClick();
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
}
And finally the important parts in the activity:
lv = (ExpandableListView)findViewById(R.id.normalList);
lv.setGroupIndicator(null);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
//do some things
return true; //this tells the list that it mustn't show the child items
}
});
adapter = new CustomList(myActivity.this, list, icons);
lv.setAdapter(adapter);
P.S.: The code to hide the open toolbars doesn't work properly, but I really can't understand why. But this is another story...
--- EDIT ---
About the P.S. above: now I know why that happens.
Your list looks like ExpandableListView. Did you try to use it? It has build it second level rows, expanding, hiding, etc. Simply add your pdf rows as group headers and then add tool rows as child rows. Then you can handle row clicks to show and hide rows you would like to show/hide. See: http://developer.android.com/reference/android/widget/ExpandableListView.html
And, btw., using additional rows with hiding is a bit wrong UX design. Maybe it would be better to use drop down lists or custom dialog windows? Both of them have built in functionality do disappear when touched outside.
You can simply put all your ToggleButton whenfilling the list in an ArrayList. This way you just have to force the click on each button of list toggled when one of them is clicked.
Ok I solved the problem.
Instead of toggling the buttons with "performClick()" or "setChecked" in some methods and managing the expand/collapse action in an OnCheckedChangedListener, I decided to store the status of the togglebuttons in an array, and to expand/collapse the child views in the "getGroupView" method depending on the state of the groupview's button, whose position in the array corresponds to the groupview's position in the list. To update the list, I call notifyDataSetChanged() on the adapter.
I added a FrameLayout around the ToggleButton to increase its clickable area.
Here's the code for clarity:
The relevant parts of the MainActivity:
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.
.
.
ExpandableListView lv = (ExpandableListView) findViewById(R.id.normalList);
CustomList adapter = new CustomList(MainActivity.this, list /*the list of Items, declared elsewhere*/, lv);
lv.setAdapter(adapter);
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.filesListDrawerLayout);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
uncheckButtons(mDrawerLayout);
//do some unrelated stuff
return true;
}
});
}
public void uncheckButtons(ViewGroup vg) {
for(int i = 0; i < adapter.buttons.length; ++i)
adapter.buttons[i] = false;
adapter.lastChecked = -1;
adapter.notifyList();
}
The relevant parts of the adapter:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
public boolean[] buttons;
public int lastChecked;
private final Integer[] imageId = new Integer[] { R.drawable.image1, R.drawable.image1, R.drawable.image3 };
private ExpandableListView list;
public CustomList(Activity context, List<Item> names, boolean forUpload, ExpandableListView list) {
this.context = context;
this.names = names;
this.list = list;
this.buttons = new boolean[names.size()];
this.lastChecked = -1;
}
private static class GroupHolder {
TextView txtTitle;
TextView txtData;
ImageView imageView;
ToggleButton toggle;
FrameLayout frame;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_single, null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.text);
holder.txtData = (TextView) convertView.findViewById(R.id.counter);
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
holder.frame.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(lastChecked != -1) {//if there's a checked button
buttons[lastChecked] = !buttons[lastChecked];//uncheck it
if(position == lastChecked)//if I clicked on the last checked button
lastChecked = -1;//there's no checked button
else {
buttons[position] = !buttons[position];//check the button
lastChecked = position;//and set it as the last checked one
}
notifyList();
}
return false;
}
});
holder.toggle.setChecked(buttons[position]);
if(holder.toggle.isChecked()) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
list.expandGroup(position);
else
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
} else if(/*other conditions*/) {
//do other stuff with the views that don't interest us
}
return convertView;
}
private static class ChildHolder {
TextView details;
TextView send;
TextView other;
}
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
holder = new ChildHolder();
holder.details = (TextView) convertView.findViewById(R.id.details);
holder.send = (TextView) convertView.findViewById(R.id.send);
holder.other = (TextView) convertView.findViewById(R.id.other);
} else {
holder = (ChildHolder) convertView.getTag();
}
//maybe I could manage the listeners more nicely and without redundance...
holder.details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
holder.send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
holder.other.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
return convertView;
}
public void notifyList() {
this.notifyDataSetChanged();
}
//I don't need OnGroupExpanded anymore
The ExpandableListView:
<ExpandableListView
android:id="#+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:groupIndicator="#drawable/toggle_button_selector" >
</ExpandableListView>
The xml of the group item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="#+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:contentDescription="#string/icondescription" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="#+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="#+id/img"
android:layout_below="#+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/text"
android:layout_marginTop="-15dp"
android:clickable="true"
android:paddingTop="10dp"
android:paddingRight="15dp"
android:paddingLeft="10dp" >
<ToggleButton
android:id="#+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/toggle_button_selector"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="#android:color/transparent"
android:textOff=""
android:textOn="" />
</FrameLayout>
</RelativeLayout>
The xml of the child item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/details" />
<TextView
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/send" />
<!-- this one is new -->
<TextView
android:id="#+id/other"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:background="#drawable/toolbar_selector"
android:clickable="true"
android:drawablePadding="-12dp"
android:drawableTop="#drawable/ic_action_other"
android:gravity="center"
android:text="#string/other" />
</LinearLayout>
There. I'm happy now.
Like the title say, i create list view which each item display image, three line text, and a button, but the view is so lagging even in samsung galaxy mega dual core, here my item view xml
<RelativeLayout
android:gravity="center_vertical"
android:padding="5.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:listPreferredItemHeight"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/compositeListViewItemThumbnailImage"
android:layout_width="60.0dip"
android:layout_height="60.0dip"
android:src="#drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:contentDescription="#string/_empty_strings" />
<LinearLayout
android:orientation="vertical"
android:id="#+id/compositeListViewItemFirstLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/compositeListViewItemThumbnailImage"
android:layout_alignTop="#+id/compositeListViewItemThumbnailImage"
android:layout_alignBottom="#+id/compositeListViewItemThumbnailImage"
android:layout_alignParentRight="true">
<TextView
android:textSize="16.0sp"
android:gravity="center_vertical"
android:id="#+id/compositeListViewItemFirstLineText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:singleLine="true"
android:ellipsize="marquee"
android:lines="1"
android:inputType="textNoSuggestions" />
<TextView
android:textSize="12.0sp"
android:id="#+id/compositeListViewItemSecondLineText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:singleLine="true"
android:ellipsize="marquee"
android:lines="1"
android:inputType="textNoSuggestions" />
<TextView
android:textSize="14.0sp"
android:singleLine="true"
android:ellipsize="marquee"
android:lines="1"
android:layout_gravity="center_vertical"
android:id="#+id/compositeListViewItemThirdLineText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:inputType="textNoSuggestions" />
</LinearLayout>
<Button
android:id="#+id/compositeListViewItemActionButton"
android:paddingLeft="5.0dip"
android:paddingTop="5.0dip"
android:paddingRight="5.0dip"
android:paddingBottom="5.0dip"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="30.0dip"
android:layout_marginTop="30.0dip"
android:layout_marginRight="0.0dip"
android:text="#string/hello_world"
android:layout_alignParentRight="true"
style="?android:attr/buttonStyleSmall" />
</RelativeLayout>
as it might / might not be affected by the adapter view, i also put it here
public class SimpleAdapter extends ArrayAdapter implements Serializable {
private ArrayList<?> items;
private int itemLayout;
private String[] itemFieldsName;
private int[] itemElements;
private Context ctx;
public SimpleAdapter(Context ctx, ArrayList<?> items, int itemLayout, int[] itemElements, String[] itemFieldsName) {
super(ctx, itemLayout, items);
this.items = items;
this.itemElements = itemElements;
this.itemLayout = itemLayout;
this.itemFieldsName = itemFieldsName;
this.ctx = ctx;
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(this.itemLayout, null);
}
Object item = this.items.get(position);
for (int i = 0; i < this.itemElements.length; i++) {
Object value = getValue(item, Character.toUpperCase(this.itemFieldsName[i].charAt(0)) + this.itemFieldsName[i].substring(1));
View elementView = null;
if (itemView != null) {
elementView = itemView.findViewById(this.itemElements[i]);
if ((elementView instanceof TextView)) {
((TextView) elementView).setText(value.toString());
} else if ((elementView instanceof SmartImageView)) {
((SmartImageView) elementView).setImageUrl(value.toString());
} else if ((elementView instanceof Button)) {
((Button) elementView).setText(value.toString());
}
}
}
return itemView;
}
private Object getValue(Object obj, String fieldName) {
ArrayList<String> fieldsName = new ArrayList<String>();
int length;
String str = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
Object value = new Object();
try {
value = obj.getClass().getMethod("get" + str, new Class[0]).invoke(obj);
} catch (Exception ex) {
value = fieldName;
}
return value;
}
private String trimString(String string, int length, boolean soft) {
if(string == null || string.trim().isEmpty()){
return string;
}
StringBuffer sb = new StringBuffer(string);
int actualLength = length - 3;
if(sb.length() > actualLength){
if(!soft)
return sb.insert(actualLength, "...").substring(0, actualLength+3);
else {
int endIndex = sb.indexOf(" ",actualLength);
return sb.insert(endIndex,"...").substring(0, endIndex+3);
}
}
return string;
}
}
Can someone tell me where i was wrong?,
tell me if you need more explanation / code resource
Finding an inner view inside an inflated layout is among the most common operations in Android. This is usually done through a View method called findViewById(). This method will recursively go through the view tree looking for a child with a given IDcode. Using findViewById() on static UI layouts is totally fine but, as you’ve seen, ListView calls the adapter’s getView() very frequently when scrolling. findViewById() might perceivably hit scrolling performance in ListViews—especially if your row layout is non-trivial.
The View Holder pattern is about reducing the number of findViewById() calls in the adapter’s getView(). In practice, the View Holder is a lightweight inner class that holds direct references to all inner views from a row. You store it as a tag in the row’s view after inflating it. This way you’ll only have to use findViewById() when you first create the layout. Here’s the code sample with View Holder pattern applied:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.your_layout, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = convertView.getTag();
}
holder.text.setText("Position " + position);
return convertView;
}
private static class ViewHolder {
public TextView text;
}
There is one known issue with android:singleLine where it kills performance on ListViews drastically. Please see this question Why does android:singleLine="true" make ListView scrolling very laggy?
Remove it from your layout and use maxLines instead.