I have created a layout that contains a listView, the view is populated with multiple similar elements in the onCreateView function from a Fragment, the view is populated in the CustomAdapter extends BaseAdapter. In each element of the listView I have a button for which I want to implement a onClickListener. When the button is clicked I want to switch to another view, something similar to viewing comments on the GooglePlus mobile app.
I have tried doing so this way in the CustomAdapter:
public void setOnClickComment(View view) {
final ImageButton btn = (ImageButton)view.findViewById(R.id.addComment);
if (btn != null) {
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("ONCLICK", "I just clicked this shit" + btn.getTag());
manager.beginTransaction()
.replace(R.id.feedLayout, new CommentFragment()).commit();
}
});
} else {
Log.d("CLICK", "ESTE NULL");
}
}
The manager is a FragmentManager and it's passed from the fragment to the CustomAdapter constructor. I can see the Log in LogCat but that's it nothing else happens, the tag of the button is the position of the element.
This is the layout:
<?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:id="#+id/feedLayout"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
And in the fragment I inflate the above layout:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout linear = (LinearLayout) inflater.inflate(R.layout.swipe_screen_left, container, false);
view = (ListView) linear.getChildAt(0);
adapter = new CustomAdapter(getActivity(), data, getFragmentManager());
view.setAdapter(adapter);
return linear;
}
Related
Hello i have fragment and inside it viewpager with 2 tabs i need when i press button in the parent fragment refresh the child fragment inside viewpage
i used this for refresh the same fragment and it's working fine
FragmentTransaction ft1 = getFragmentManager().beginTransaction();
ft1.detach(this).attach(this).commit();
i tried the same and instead of this i put the childfragment.newInstance() put still not refreshed so any suggestion ??
Create a interface in your child fragment.
RefreshInterface
create a method in child fragment to assign a object to RefreshInterface.
intialiseRefreshInterface(RefreshInterface refreshInterface)
call the interface method inside the refresh click.
implement the child fragment Interface in Parent fragment and override the method.
before commit() the child fragment from parent fragment
1. create a object to child object
2. call the intialiseRefreshInterface(RefreshInterface refreshInterface)
3. commit() the child fragment.
your child fragment will refresh [ child fragment will be popBackStack() and re-create.
Below example code will help you in achieve your requirement
Parent Fragment
parentFragment.java
public class OneFragment extends Fragment implements TwoFragment.RefreshInterface {
TwoFragment child;
Button launch;
FrameLayout containerLayout;
// child fragment interface object
TwoFragment.RefreshInterface refreshInterface;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
launch = view.findViewById(R.id.fragment_launch);
containerLayout = view.findViewById(R.id.fragment_container);
// RefreshInterface. this is mandatory
refreshInterface = this;
launch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
launchChildFragment();
}
});
return view;
}
#Override
public void refresh_fragment() {
// as we added the child fragment to backstack. we will remove the fragment from backstack and add again
if (getActivity().getSupportFragmentManager().getBackStackEntryCount() > 0){
getActivity().getSupportFragmentManager().popBackStack();
}
launchChildFragment();
}
private void launchChildFragment(){
// creating the object for child fragment
child = new TwoFragment();
// intialising the RefreshInterface object
child.intialiseRefreshInterface(refreshInterface);
// calling the child fragment
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, child).addToBackStack(null).commit();
}
}
fragment_parent.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapplication.OneFragment">
<Button
android:layout_centerHorizontal="true"
android:id="#+id/fragment_launch"
android:text="Launch child fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_below="#id/fragment_launch"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
childFragment
public class TwoFragment extends Fragment {
Button refershBtn;
RefreshInterface refreshInterface;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
refershBtn = view.findViewById(R.id.btn_refersh);
refershBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
refreshInterface.refresh_fragment();
}
});
return view;
}
public void intialiseRefreshInterface(RefreshInterface refreshInterface){
this.refreshInterface = refreshInterface;
}
public interface RefreshInterface {
void refresh_fragment();
}
}
fragment_child.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.outthinking.myapplication.TwoFragment">
<!-- TODO: Update blank fragment layout -->
<EditText
android:id="#+id/refersh_tv"
android:textSize="24sp"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="REFRESH DATA" />
<Button
android:id="#+id/btn_refersh"
android:text="refersh"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I created a fragment connected to a list view to display a simple table view.
Here is my fragment:
public class ConfRoomListFragment extends Fragment {
ApplicationController activity;
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
"WebOS","Ubuntu","Windows7","Max OS X"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_conf_room_list, container, false);
activity = (ApplicationController) getActivity();
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
R.layout.activity_listview, mobileArray);
ListView listView = (ListView)view.findViewById(R.id.conf_room_directory);
listView.setAdapter(adapter);
return view;
}
}
Here is my fragment_conf_room_list.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="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/conf_room_directory"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Here is my activity_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
When I run my app, all that shows is a blank screen. I created the list view and added the adapter. There doesn't seem to be any variable issues, so I'm not sure what the problem is.
EDIT
I've already added my fragment to the activity:
ApplicationController.java
/* Instantiate all Fragments needed*/
directoryViewFragment = new DirectoryViewFragment();
directoryListFragment = new DirectoryListFragment();
officeViewFragment = new OfficeViewFragment();
officeListFragment = new OfficeListFragment();
confRoomListFragment = new ConfRoomListFragment();
settingsFragment = new SettingsFragment();
directionsFragment = new DirectionsFragment();
airportInfoFragment = new AirportInfoFragment();
publicationsFragment = new PublicationsFragment();
practiceGroupListFragment = new PracticeGroupListFragment();
practiceGroupFragment = new PracticeGroupFragment();
As you can see I instantiated my fragment.
This is where the selection occurs:
private class SlideMenuClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position, false);
System.out.println(position);
current_position = position;
invalidateOptionsMenu();
}
}
/**
* Diplaying fragment view for selected nav drawer list item
*/
private void displayView(int position, boolean firstClick) {
// update the main content by replacing fragments
if (firstClick) {
Fragment fragment = directoryViewFragment;
currentPanel = Panel.HomeView;
getFragmentManager().beginTransaction()
.replace(R.id.fragmentHolder, fragment)
.addToBackStack(null)
.commit();
getFragmentManager().executePendingTransactions();
} else {
new ViewSelectionListener(this).onItemClick(null, null, position, 0);
}
// update selected item and title, then close the drawer
setTitle(navMenuTitles[position]);
drawerLayout.closeDrawer(navList);
}
I know it works because the setTitle function works and the title of the fragment is Conference Rooms.
This is my ViewSelectionListener.java, where I update the Fragment manager to show my fragment:
} else if (position == 2) {
//Conference Rooms
activity.setCurrentPanel(Panel.ConfRoomDirectory);
FragmentTransaction transaction = activity.getFragmentManager().beginTransaction()
.setCustomAnimations(Tags.TRANSITION_IN, Tags.TRANSITION_OUT)
// TODO If there are other fragments to hide, put them here!
.hide(activity.getDirectoryViewFragment())
.hide(activity.getPublicationsFragment())
.hide(activity.getSettingsFragment())
.hide(activity.getAirportInfoFragment())
.hide(activity.getDirectionsFragment())
.hide(activity.getPracticeGroupListFragment())
.hide(activity.getPracticeGroupFragment())
.hide(activity.getOfficeViewFragment())
.show(activity.getConfRoomListFragment());
if (activity.isPhone) {
transaction.hide(activity.getDirectoryListFragment());
transaction.hide(activity.getOfficeListFragment());
} else if (activity.isPortrait()) {
transaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_left);
if (DirectoryViewFragment.leftPanelIsShowing) {
transaction.remove(activity.getDirectoryListFragment());
DirectoryViewFragment.leftPanelIsShowing = false;
}
if (OfficeViewFragment.leftPanelIsShowing) {
transaction.remove(activity.getOfficeListFragment());
OfficeViewFragment.leftPanelIsShowing = false;
}
if (PracticeGroupFragment.leftPanelIsShowing) {
transaction.remove(activity.getPracticeGroupListFragment());
PracticeGroupFragment.leftPanelIsShowing = false;
}
}
transaction.commit();
}
As you can see, I hide all of the other fragments and show the fragment that I wanted. However, only the title get updated, the fragment isn't shown. So the issue doesn't lie with the Activity, there's some other error that I don't see.
Check if onCreateView() method of your fragment is really called. If not then your fragment is not added to activity. Make sure to call
getFragmentManager().beginTransaction() .add(R.id.fragmentHolder, ConfRoomListFragment) .addToBackStack(null) .commit();
before you call FragmentTransaction's show(ConfRoomListFragment) method.
If ConfRoomListFragment's onCreate() method is called try to use next constructor of ArrayAdapter:
ArrayAdapter adapter = = new ArrayAdapter<>(getContext(), R.layout.activity_listview, mobileArray);
And change list item (activity_listview.xml) a little bit:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
activity = (ApplicationController) getActivity();
Remove above line, this line will never use in this fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_conf_room_list, container, false);
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, mobileArray);
ListView listView = (ListView)view.findViewById(R.id.conf_room_directory);
listView.setAdapter(adapter);
return view;
}
fragment_conf_room_list.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="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/conf_room_directory"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
You need to specify TextView layout in adapter if you want use your own layout.
public class ConfRoomListFragment extends Fragment {
ApplicationController activity;
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
"WebOS","Ubuntu","Windows7","Max OS X"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_conf_room_list, container, false);
activity = (ApplicationController) getActivity();
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
R.layout.activity_listview, R.id.label, mobileArray);
ListView listView = (ListView)view.findViewById(R.id.conf_room_directory);
listView.setAdapter(adapter);
return view;
}
}
And for your ListView height must be set to match_parent
<ListView
android:id="#+id/conf_room_directory"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
I have a ViewPager in which I am able to view between 4 different tabs (which are fragments) I created. In one of my fragments, I want to have a button that will, after I click a button on that page, replace that view with another fragment. Now I don't want to swipe to another tab, I just merely want to replace the view of the current fragment when I press that button. Here is my attempt, but I'm not sure why when I click my button, nothing happens. If anyone could help me that would be great. Thanks!
This is the Fragment I call to create the view and the button I want to press.
ManualSelection.java
public class ManualSelection extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_manual_selection, container, false);
intent = new Intent(getActivity(), ManualSelectionListView.class);
return rootView;
}
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button listViewGenerated = (Button) getActivity().findViewById(R.id.manual_generate);
listViewGenerated.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
});
}
}
And here is the Fragment I want to create
ManualSelectionListView.java
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ManualSelectionListView extends Fragment {
static final String[] MOBILE_OS =
new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.listview, container, false);
ListView lv = (ListView)rootView.findViewById(R.id.listview);
lv.setAdapter(new MobileArrayAdapter(getActivity(),MOBILE_OS));
return rootView;
}
}
And here are my XML files:
activity_manual_selection.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.ecs160.homestay.ManualSelection"
android:id="#+id/fragmentTest">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"
android:id="#+id/manual_generate"
android:layout_centerHorizontal="true"
android:onClick="generateListView"/>
And here is the XML file called listview.xml that contains R.id.listView_manual
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView_manual">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I get nothing displayed when I hit "Generate." Any ideas? Thanks!
In your Activity you should create this:
public void generateListView(View v){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
The problem is that you are trying to get the Button id from your main activity which will return null
Button listViewGenerated = (Button) getActivity().findViewById(R.id.manual_generate);
Button manual_generate is located in your activity_manual_selection layout not in your Main activity's layout you cant get view from different layout or else it will return null
solution:
Get the Button's id from your fragment's layout
public class ManualSelection extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_manual_selection, container, false);
intent = new Intent(getActivity(), ManualSelectionListView.class);
Button listViewGenerated = (Button) rootView.findViewById(R.id.manual_generate);
listViewGenerated.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.listView_manual, new ManualSelectionListView());
ft.commit();
}
});
return rootView;
}
remove onActivityCreated
I'm trying to use setRetainInstance() but it seems not working for me! =(.
I simplified the problem to the easiest version:
I have a fragment with a TextView and a Button.
The initial text of the TextView is "I'm waiting" when the button is pressed the text changes to "Hello!"
That is my code:
The fragment:
public class SayHelloFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.say_hello_layout, container);
final TextView text = (TextView) view.findViewById(R.id.textView1);
view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Hello!");
}
});
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
The Activity:
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The activity's layout:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/say_hello_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.example.retaintext.SayHelloFragment" />
The Fragment's layout:
<?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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm Waiting"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
But when I play it, press the button and change the device's orientation the text becomes reset.
What am I missing? How can I solve it? Thanks!
You are missing that onCreateView runs after the rotation again and inflates the default view with the default text again.
You need to restore the state of your views manually, e.g.:
public class SayHelloFragment extends Fragment {
private String mText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.say_hello_layout, container);
final TextView text = (TextView) view.findViewById(R.id.textView1);
if (mText != null)
text.setText(mText);
view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mText = "Hello!";
text.setText(mText);
}
});
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
I'm trying to show a ListView from a PopupWindow. but when I'm try to call ListView's setOnItemClickListener nothing to haapen. Here It Java file
PopupWindowActivity.java
public class PopupWindowActivity extends Activity {
String[] data = { "DATA 1", "DATA 2", "DATA 3", "DATA 4", "DATA 5", "DATA 6" };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
final Button btnOpenPopup = (Button) findViewById(R.id.openpopup);
btnOpenPopup.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.main, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ListView listView = (ListView) popupView.findViewById(R.id.listView1);
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,data));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("Item Clicked");
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(btnOpenPopup, 20, -5);
}
});
}
}
Here it is first xml file
a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/openpopup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Popup Window" />
</LinearLayout>
Here it inflate xml file
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/recording"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="30sp"
android:layout_marginLeft="30sp"
android:layout_marginRight="30sp"
android:layout_marginTop="100sp" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000" >
</ListView>
</RelativeLayout>
</LinearLayout>
What am I doing wrong?
Thanks
Just one minor change in your code and BOOOM your code will listen to you list click event
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
You forget to mention focusable setting true in PopupWindow constructor.
had the same problem, but in my case setFocusble(false) was required (and using ListPopupWindow was not a solution in my case as a lot of stuff in the project already used base PopupWindow's functionality including extending).
If someone in the same situation there is a kind of workaround based on bug discusson here (post #9)
The main idea is that ListView's hierarchy is still receives touch events so we can manually trigger onItemClick().
However this approach is not 100% identical to real ListView's touch handling (like there is no glow of selection while tapping a row) this done pretty well for me for the moment.
If someone has more precise solution of this problem, please share.
So, here is complete Adapter's code which can be used with ListView inside PopupWindow which is setFocusable(false):
private class CustomAdapter extends ArrayAdapter {
private LayoutInflater mInflater;
private ListView mOwningListView;
public CustomAdapter(Context context, List<String> objects, ListView listView) {
super(context, android.R.layout.simple_list_item_1, objects);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOwningListView = listView;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.font_pick_row, null);
}
// this is the key point of workaround
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*
* as every row is still receiving their touches
* we can use this to manually trigger onItemClick
* since it doesn't firing in popupWindow.setFocusable(false)
*/
mOwningListView.getOnItemClickListener().onItemClick(mOwningListView, v, position, getItemId(position));
}
});
//... other stuff
return convertView;
}
}
May this help you
.
Declare listview and list onClickListener outside the button ClickListener.