android - Buttons within a ListFragment not clickable -
i have activity lays out listfragments side side (as many user wants) in horizontalscrollview upon choosing option in actionbar.
each listfragment item contains textviews , button. simpleadapter populates data each item in every listfragment.
the issue facing buttons in each list item not respond clicks. layout can summarized follows: button inside listfragment inside fragmentactivity, going innermost child element parent @ root view.
i have spent many hours on problem , unable arrive @ solution make buttons respond clicks. of approaches have used include obtaining button's view , attaching onclicklisteners, 2) implementing onclicklistener interface listfragment. aware of onintercepttouchevent method viewgroup class, lack of experience android prevents me arriving @ solution. guidance or direction towards solving problem appreciated.
here code listfragment:
package com.example.androidlistfragmenttest; import java.util.arraylist; import java.util.hashmap; import android.os.bundle; import android.support.v4.app.listfragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.button; import android.widget.simpleadapter; public class myfragment extends listfragment { private arraylist<hashmap<string,string>> arraylist; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view view = inflater.inflate(r.layout.fragment_layout, container, false); button button = (button) view.findviewbyid(r.id.button); button.setonclicklistener(new onclicklistener(){ //this not print in logcat. button not respond clicks. @override public void onclick(view arg0) { log.v("godzilla","atomic breath"); } }); return view; } @override public void onactivitycreated(bundle savedinstancestate){ super.onactivitycreated(savedinstancestate); arraylist = datagenerator(); simpleadapter adapter = new simpleadapter(getactivity().getapplicationcontext(), arraylist, r.layout.fragment_layout,new string[]{"key"},new int[]{r.id.text_id}); setlistadapter(adapter); } /* * method populate adapter's data list. */ public arraylist<hashmap<string,string>> datagenerator(){ hashmap<string,string> hashmap1 = new hashmap<string,string>(); hashmap1.put("key", "a"); hashmap<string,string> hashmap2 = new hashmap<string,string>(); hashmap2.put("key", "b"); hashmap<string,string> hashmap3 = new hashmap<string,string>(); hashmap3.put("key", "c"); hashmap<string,string> hashmap4 = new hashmap<string,string>(); hashmap4.put("key", "d"); hashmap<string,string> hashmap5 = new hashmap<string,string>(); hashmap5.put("key", "e"); arraylist<hashmap<string,string>> arraylist = new arraylist<hashmap<string,string>>(); arraylist.add(hashmap1); arraylist.add(hashmap2); arraylist.add(hashmap3); arraylist.add(hashmap4); arraylist.add(hashmap5); return arraylist; } } //end of myfragment
and code activity containing fragment(s):
package com.example.androidlistfragmenttest; import java.util.arraylist; import java.util.hashmap; import java.util.stack; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.app.navutils; import android.view.menu; import android.view.menuitem; public class mainactivity extends fragmentactivity { private stack<string> tagstack; private integer last_tag_number; public mainactivity(){ last_tag_number = new integer("0"); tagstack = new stack<string>(); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.add_fragment: addcolumn(); return true; case r.id.remove_column: removecolumn(); return true; case android.r.id.home: // id represents home or button. in case of // activity, button shown. use navutils allow users // navigate 1 level in application structure. // more details, see navigation pattern on android design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // navutils.navigateupfromsametask(this); return true; } return super.onoptionsitemselected(item); } /* * method adds fragment screen */ public void addcolumn(){ fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); myfragment fragment = new myfragment(); fragmenttransaction.add(r.id.fragment_activity, fragment,taggenerator()); fragmenttransaction.commit(); } /* * method removes fragment screen */ public void removecolumn(){ if(tagstack.size() != 0){ fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragment fragment = fragmentmanager.findfragmentbytag(tagstack.pop()); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.remove(fragment); fragmenttransaction.commit(); } } /* * function generates tags each fragment displayed on screen * tags pose unique identifiers each fragment */ public string taggenerator(){ integer tag_number; if(last_tag_number.intvalue() == 0){ tag_number = last_tag_number; int temp = last_tag_number.intvalue(); temp+=1; last_tag_number = integer.valueof(temp); } else{ tag_number = new integer(last_tag_number.intvalue()); int temp = last_tag_number.intvalue(); temp+=1; last_tag_number = integer.valueof(temp); } string tag = tag_number.tostring(); tagstack.push(tag); return tag; } } //end of mainactivity
as layouts fragmentactivity:
<?xml version="1.0" encoding="utf-8"?> <horizontalscrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:id="@+id/fragment_activity" android:layout_width="fill_parent" android:layout_height = "fill_parent" android:orientation = "horizontal" android:gravity="center" > </linearlayout> </horizontalscrollview>
and listfragment:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="match_parent" android:orientation="vertical" android:layout_weight="1" android:layout_margin="5dp" android:descendantfocusability="blocksdescendants" > <listview android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_gravity="center" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/label" android:layout_gravity="start" /> <textview android:id="@+id/text_id" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="end" /> </linearlayout> <linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="horizontal" > <button android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/button" android:layout_margin="2dp" android:layout_marginleft="2dp" android:layout_marginright="2dp" /> </linearlayout> </linearlayout>
the problem listitem consuming click , not getting passed button below.
you need set list items inactive , handle clicks yourself. in custom adapter add following methods disable items (they override methods baseadapter):
@override public boolean areallitemsenabled() { return false; } @override public boolean isenabled(int position) { return false; }
edit: here related question may offer better solution, depends on design.
Comments
Post a Comment