How to set onItemclick in fragment - android

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;
}

Related

How to display detailed view activity inside Fragment on image button click

I am moving my code from Activity to use it under Fragment view. The grid has multiple images which gets successfully displayed inside the fragment.I am trying to display detailed view based on the image clicked. But I am unable to get to do this, tried different ways but resulting one or other error.Earlier I used to have setContentView which does not seem to work when used for Fragment. I am already inflating the Fragment under OnCreateView method but not sure how load with the detailed view upon the image click.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
CustomGrid adapter = new CustomGrid(view.getContext(), iconSize);
GridView gridview = (GridView) view.findViewById(R.id.grid);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(view.getContext(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
//setContentView(R.layout.list_item_main);
mListView = (ListView) view.findViewById(R.id.item_list_view);
Item.getRecipesFromDB(OneFragment.this, DBReference);
}
});
return view;
}
#Override
public void newDataReceived(ArrayList<Item> itemList) {
ItemAdapter adapter = new ItemAdapter(mListView.getContext(), itemList);
mListView.setAdapter(adapter);
}
Open a new Fragment when a gridView is clicked and then pass the position from the bundle parameters. Then in NewFragment show the listview and the data from database.
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
NextFragment nextFrag= new NextFragment();
Bundle bundle = new Bundle();
bundle.putInt("POSITION",position);
this.getFragmentManager().beginTransaction()
.replace(R.id.Layout_container, nextFrag,TAG_FRAGMENT)
.addToBackStack(null)
.commit();
}
});
In the NextFragment get the reference of listView and then update the data from database using proper adapter.

Redirecting to a blank page using a click event

I'm trying to redirect my current page to an empty page with an onItemClick event, but an error occurred.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_universiteler, container, false);
GridView listView = (GridView) rootView.findViewById(R.id.gridView);
((GridView) listView).setAdapter(new ImageAdapter(this));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Uri link = Uri.parse(Site[position]);
Intent tara = new Intent(Intent.ACTION_DEFAULT, link);
startActivity(tara);
}
});
return rootView;
}
Sir summary as not directed to the page I want a null is directed to a page without any error
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_universiteler, container, false);
GridView listView = (GridView) rootView.findViewById(R.id.gridView);
((GridView) listView).setAdapter(new ImageAdapter(this));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Uri link = Uri.parse(Site[position]);
Intent tara = new Intent(Intent.ACTION_DEFAULT, link);
startActivity(tara);
}
});
return rootView;
}

android listview under fragment onItemClick not working

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.

How to correctly display a gridview into a fragment

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()));
}

Can not get gridView on item click listener

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();
}*/
}});
}
}

Categories

Resources