Don't know why the onItemClick event not working...
public class FragmentList extends Fragment {
View rootView;
ListView list;
public FragmentList (){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_list, container, false);
list=(ListView)rootView.findViewById(R.id.list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Log.i("test", String.valueOf(position));
}
});
return rootView;
}
}
Anyone has idea on this. thanks so much
If any row item of list contains focusable or clickable view then OnItemClickListener won't work.
The row item must have a param like android:descendantFocusability="blocksDescendants".
Click here for more information.
Related
Here is my code which is not working
I'm getting data from server using `AsyncTask` and set data to the `listview` using `baseadapter` but using `convertview.setonclicklistner` working in adapter class and below code contains in `Fragmentclass(import android.support.v4.app.Fragment;)` so I think it is not problematic
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_restaurants, container, false);
restaurant_list = (ListView)view.findViewById(R.id.restatrant_lv);
no_restaturants=(TextView)view.findViewById(R.id.no_restaturants);
// listening to single list item on click
restaurant_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.e("temp","temppppp");
}
});
//set layout slide listener
rowItems = new ArrayList<>();
history_list = new ArrayList<HashMap<String, String>>();
list_adapter = new ViewAdapter(getActivity(),rowItems);
restaurant_list.setAdapter(list_adapter);
}
What is the solution for this ?
add id to your parent layout for custom_list_item.xml
holder.parentLayout.setOnClickListener(new OnClickItem(position));
private class OnClickItem implements View.OnClickListener{
private int mPostion;
public OnClickItem(int position){
mPostion = position;
}
#Override
public void onClick(View v) {
YourActivity yourActivity = (YourActivity) mContext;
yourActivity.onItemClicked(mPostion);
}
}
android:descendantFocusability="blocksDescendants"
If any ListView contains focusable or clickable view then OnItemClickListener won't work. In that case we need to use this.
Try to set your setOnClickListener after setting the adapter:
Like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.fragment_restaurants, container, false);
restaurant_list = (ListView)view.findViewById(R.id.restatrant_lv);
no_restaturants=(TextView)view.findViewById(R.id.no_restaturants);
//set layout slide listener
rowItems = new ArrayList<>();
history_list = new ArrayList<HashMap<String, String>>();
list_adapter = new ViewAdapter(getActivity(),rowItems);
restaurant_list.setAdapter(list_adapter);
// listening to single list item on click
restaurant_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.e("temp","temppppp");
}
});
}
I am trying to call a swipeview when the image from my fragment gridview is clicked but I am not able to se it properly .earlier I have connected the my gridview with the swipe view in mainactivity but here I am getting error
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_one, container, false);
GridView gridView=(GridView)view.findViewById(R.id.gridView);
gridView.setAdapter(new MyAdapter(view.getContext()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),"Pic"+(position)+"Selected",Toast.LENGTH_SHORT).show();
Intent intent= new Intent(view.getContext(),Main2Activity.class);
intent.putExtra("pic",position);
startActivity(intent);
}
});
return view;
}
I am getting error in getbaseContext and I may be wrong in setting MyAdapter(view.getContext() so plz help in solving the error
Try to use getActivity() instead of view.getcontext() and for swipe view i think u can add that functionality by adding left to right animation. Actually i dont understand what do u mean by calling swipeview when image is clicked
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_one, container, false);
GridView gridView=(GridView)view.findViewById(R.id.gridView);
gridView.setAdapter(new MyAdapter(view.getContext()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(),"Pic"+(position)+"Selected",Toast.LENGTH_SHORT).show();
Intent intent= new Intent(getContext(),Main2Activity.class);
intent.putExtra("pic",position);
startActivity(intent);
}
});
return view;
}
this is my first post on stackoverflow, so please excuse any shortcomings I might be having with norms.
I'm trying to have a tabbed view on an application, and have each tabbed view (just a normal fragment) be a gridview. I thought I could just put the initialization code for the gridview in the onCreateView, but ive been at this problem for a couple hours now, and the gridview is returning null in the findviewbyid call. Here's the relevant code (I'm trying not make this question overwhelming , so I won't post all the code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View rootView = inflater.inflate(
// R.layout.fragment_main_ingredient_dummy, container, false);
GridView gridview = (GridView) inflater.inflate(R.id.gridview, container);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
}
});
gridview.setAdapter(new ImageAdapter(getActivity()));
return gridview;
}
As I said. the fragment is an ordinary fragment, and its being "managed" by a FragmentPagerAdapter. However, I also have an extension of a BaseAdapter, for teh content inside the actual tab.
Thank you in advance!
You can't inflate a view like that. The inflate call needs an R.layout. reference, not an R.id. reference.
You should only create the view in onCreateView. Then do the other work in onViewCreated.
private GridView mGridView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {;
mGridView = (GridView) inflater.inflate(R.layout.frag_grid, container);
return mGridView;
}
#Override
public void onViewCreated(){
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
}
});
mGridView.setAdapter(new ImageAdapter(getActivity()));
}
My onListItemClick is never call when i click on item, the class is extends fragment not listfragment, because i have other view items in this fragment which is not list, so how to implement onlistitemclick in class extends fragment?
class
public class MainFiles extends Fragment
{
ArrayList<String> items;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.files, container, false);
Button button_up = (Button) view.findViewById(R.id.button_up);
items = new ArrayList<String>();
MyAdapter adapter = new MyAdapter(getActivity(), R.layout.row, items);
ListView myList = (ListView) view.findViewById(R.id.list);
myList.setAdapter(adapter);
return view;
}
public void onListItemClick(ListView l, View v, int position, long id)
{
}
}
Explicitly add the OnItemClickListener to your ListView
myList.setOnItemClickListener(this);
You must also make sure that your Fragment implements the OnItemClickListener type:
public class MainFiles extends Fragment implements OnItemClickListener
Another way is to create a dedicated subclass of OnItemClickListener to pass to the ListView:
myList.setOnItemClickListener(new MyOnItemClickListener());
/* ... */
private class MyOnItemClickListener implements OnItemClickListener {
/* ... */
}
You forget to set the setOnItemClickListener
after myList.setAdapter(adapter); add this:
myList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position,long id){
new File(items.get(position));
fileList(path.get(position));
showPath(current_path);
}
});
I have implement listener in fragment like this
public class DetailsFragment extends Fragment implements OnItemClickListener {
private ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_details, container, false);
// list view
listView = (ListView) root.findViewById(R.id.listView);
listView.setListAdapter(new DetailsAdapter(getActivity(), list));
listView.setOnItemClickListener(this);
return root;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// handle click here
}
}
Your fragment has to extend ListFragment, you can have more things in your layout other than a list if your layout has a listview with id=android:id/list
I can not get setOnItemClickListener of gridView in Fragment. What can be the problem?
Here is my code::
public class MainMenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main_menu_fragment, container, false);
itemsGridViewObj = (GridView) view.findViewById(R.id.itemsGridView);
itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d(TAG, "--> onItemClick listener..."); // Can not getting this method.
/*if(position == 1) {
FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
fragmentTransaction.commit();
}*/
}
});
return view;
}
}`
You may need to set the following in your ButtonView.
android:focusable="false"
android:focusableInTouchMode="false"
see adding CheckBox to list row loses my onItemClick events?
When using Fragments the initialisation of the view occurs over two stages.
The view is only inflated (and therefore accessible) after the onCreateView method. This method is only for inflating a view and returning it to the Fragment.
Therefore, any logic to do with finding views and setting up onClickListeners should be done in the onActivityCreated() function as this is the first point at which you can access the inflated view.
Have a look at the Google docs at http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
Below is you code adjusted to comply to what I have described above:
public class MainMenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.main_menu_fragment, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GridView itemsGridViewObj = (GridView) findViewById(R.id.itemsGridView);
itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d(TAG, "--> onItemClick listener..."); // You should see this now
/*if(position == 1) {
FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
fragmentTransaction.commit();
}*/
}});
}
}