Reputation: 825
I've a problem with getChildFragmentManager method.
I used it to create several nested fragments in viewpager. This Viewpager is in PopupWindow. In this way, fragments are created and I can see them in UI. Fragments contain a gridview.
Unfortunately, with android 4.3(API 18) or less, I can't select item of gridview. In the same time, with android 4.4 (API 19) I can do it!
To intercept item click I used callbacks to Activity, and Activity onAttach method is correct.
I think that problems are caused by getChildFragmentManager, but I'm not sure.
If someone had this problem, can he explain me his solution? Thanks!
Upvotes: 1
Views: 437
Reputation: 825
I solved my problem.
With Adroid 4.4 I didn't have problem so I didn't change nothing to my code. In this way, I add a listener in the gridview.
gridView.setOnItemClickListener(this);
... // class extends Listener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(DEBUG) Log.d("EmojiconGridFragment","entered");
if (mOnEmojiconClickedListener != null) {
if(DEBUG) Log.d("EmojiconGridFragment","itemClicked");
mOnEmojiconClickedListener.onEmojiconClicked((Emojicon) parent.getItemAtPosition(position));
}
}
With android 4.3 and early, I must add code to handle click item because click isn't intercept with normal listener. So, I get it for each item, like below:
//in the adapter class for grid view insert this code
if(!Utils.hasKitKat()) {
holder.icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Emoicons", " clicked");
view.setSelected(true);
mOnEmojiconClickedListener.onEmojiconClicked(emoji);
}
});
}
where Utils.HasKitkat() is a method to check if it run in android kitkat or not.
public static boolean hasKitKat() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}
I hope this solution is useful for someone.
Upvotes: 1
Reputation: 2931
I have run into a very similar problem recently however my scenario was a little different in that I was trying to embed an EditBox within an android ListView (GridView would make no difference).
The problem is that the GridViewItem attempts to gain focus instead of the ImageView so it causes unexpected problems with which control gets focus.
Think of it like this... instead of the ImageView being selected (as you may want) the GridViewItem is getting focus instead and that is what is causing your problem.
I solved this by removing the listview and implementing a vertically orientated LinearView contained inside of a ScrollViewer, I would imagine that you may need to think about doing the same thing...
The alternative to this is to place the code that would usually be handled by the ImageView click inside the ItemClickListener on the grid view and not on each ImageView.
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
};
That would probably be an easier solution for you.
Upvotes: 0