I'm using ViewPager and TabLayout. And ViewPager is consisted with 4 fragments(for example, I'll call the fragments' name 1, 2, 3, 4). My 2(second fragment) has a ListView, and if I click a list-item, the 2(second fragment) will be changed another view. At that time, another view is not 1, 3 or 4. That is a new view.
It is purpose to make board's category(like, sports-soccer-midfielder). So when I select sport item, the view will show another list-item(like, soccer, baseball, basketball an so on). The category will be consisted with 3 step. So i will prepare second and third view of 2(second fragment).
But I don't know how to change view or fragment. I found some clue to solve this problem in google.
That is
FragmentManager fm = getFragmentManager();
FragmentTransaction fmt = fm.beginTransaction();
View2 v2 = new View2(); // View2 is another view
fmt.replace(R.id.view1, v2);
fmt.commit();
I used that code and it succeed to change view. But only a part of view is changed. What is the problem? How can I solve that?
Here is Pic of second fragment
and this is a view when I click list-item. only a part of view is changed.
(I can't attach picture directly, so someone help please ^^ )
And this is my code of second fragment.
public class MatchingFragment extends Fragment {
View2 v2;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.matching_fragment, container, false);
final MatchingListViewAdapter adapter = new MatchingListViewAdapter();
ListView listView = (ListView) view.findViewById(R.id.matching_list1);
listView.setAdapter(adapter);
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.lock), "LOCK", "This is lock icon");
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.setup), "SETUP", "This is setup icon");
adapter.addItem(ContextCompat.getDrawable(getActivity(), R.drawable.user), "USER", "This is user icon");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fmt = fm.beginTransaction();
v2 = new View2();
fmt.replace(R.id.view1, v2);
fmt.commit();
adapter.notifyDataSetChanged();
}
});
return view;
}
}
Thanks.
I think you need to start a activity when you click item on listview. because if you replace new fragment with viewpager how can you go back to previous fragment. and why you create View2() object, what is the purpose of that?
if you want to replace fragment with viewpager use this code for it, don't replace viewpager by viewpager
Fragment fragment = new FragmentOne();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.view, fragment);
ft.commit();
Related
i am trying to replace a fragment listview with a new fragment when i am use onclicklistener. However, when i click in one of the listview items change fragment but the listview appers below. Here my code:
changing listview fragment
ent_qtb_operaciones user = listaqtb_operaiones.get(pos);
qtb_operaciones_deta fragment = new qtb_operaciones_deta();
Bundle bundle = new Bundle();
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
bundle.putSerializable("usuario",user); // use as per your need
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.qtb_listview_layout,fragment);
fragmentTransaction.commit();
the fragment which receive inforamaition
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.qtb_operacion_base, container, false);
return vista;
}
As result, Second fragment superpose first one but listview of first fragment does not desappair
i am learning fragments and i am stuck in how to dynamically change layout in the main activity where i want to call different fragments according to the list text i choose from a listview.
This is the Fragment extension class. Do i have to do any if else here so that i can return my desired layout?Lets say i click another option in listview and i want Layout.arts instead of Layout.sports to be shown in the main activity, how can it be done?
public class MenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.sports, container, false);
}
}
here is the onclick listener for the listview
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Hello","Clicked");
// update the main content by replacing fragments
MenuFragment frag = new MenuFragment();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MenuFragment fragment = new MenuFragment();
fragmentTransaction.replace(R.id.drawer_layout, fragment, String.valueOf(position));
fragmentTransaction.commit();
mDrawerLayout.closeDrawer(mDrawerList);
}
}
If the business logic is same other than only layout, you can write the if - else condition in the onCreateView() of Fragment
If the business logic is different for sports and arts then create two different fragments for sports and arts and onItemClick() method replace the appropriate fragment.
I think you can do it with Layout Inflater:
public void changeView(int layoutId){
container.removeAllViewsInLayout();
inflater.inflate(layoutId, container, true);
}
container and inflater are from onCreateView.
I'm new on android developing, and I'm developing an app with SlidingMenu library from jfeinstein10, and i'm listing some top rated data on main screen...
Now i'm doing this by getting data from SQLite and putting on a ListView inside a fragment activity
HomeFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
new ManageSQLiteResponse((ListView)rootView.findViewById(R.id.lv_top_rate)).execute();
return rootView;
}
ManagerSQLiteResponse call:
public void updateList(ListView listView){
DietsListAdapter adapter = new DietsListAdapter(getActivity(), R.layout.single_top_rate, dietList);
listView.setAdapter(adapter);
}
And every time i switch the fragment by selecting on SlidingMenu, all the ListView is populated again, and it causes some lag, and the menu won't open or close smoothly...
So, is there some way to run AsyncTask.execute() and populate the ListView only once, and not every time the fragment is created? by this i think it will stop lagging the SlidingMenu
TVM
use this method to add fragment in your container layout.
public void replaceFragment(Fragment fragment) {
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
Fragment fragmentPopped = manager.findFragmentByTag(backStateName) ;
if ( fragmentPopped != null )
manager.popBackStack(backStateName, 0);
else
ft.replace(R.id.container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
}
and look make a condition in code like this, if
if ( adapter.getCount() > 0 )
{
// dont add listview.setadapter() method here.
}
elsse
{
// load data in listview.
}
I am using Android Studio and I created an Activity that contains a NavigationDrawerFragment.
One of the Fragments is loading the content of an SQLite database inside a ListView, using a custom ArrayAdapter with complex items.
My problem is that if I select another fragment in the drawer, then come back to this one, the onCreate() of the Fragment is called again, so is the onCreateView(), and the database is loaded once again.
How can I preserve everything without having to load the database nor populate the list again?
I don't have this problem when orientation changes, so I am a bit confused. Maybe it is because I have a layout for both Portrait and Landscape ?
here is the code of the onCreateView()
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().getActionBar().show();
View view = inflater.inflate(R.layout.fragment_papers, container, false);
allItems = new ArrayList<ItemData>();
getAllItemsFromDB("");
mAdapter = new ItemsList(getActivity(), allItems, this, mDBApi);
// Set the adapter
mListView = (AbsListView) view.findViewById(R.id.list);
mListView.setAdapter(mAdapter);
// Set OnItemClickListener so we can be notified on item clicks
mListView.setOnItemClickListener(this);
mListView.setOnScrollListener(this);
return view;
}
and the getAllItemsFromDB() contains something like
public void getAllItemsFromDB(String query){
Cursor c = sqldb.rawQuery("SELECT * FROM 'Table' " + query, null);
while (c.moveToNext()) {
allItems.add(parseSQL(sqldb, c));
}
c.close();
}
Set your array list static
private static List<ItemData> allItems;
Then check it for null and initialise it only once
if(allItems == null) {
allItems = new ArrayList<ItemData>();
getAllItemsFromDB("");
}
may be ur loading frament everytime as
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, new UrFragment()).commit();
Don't do that, create instance of fragment and use it.
// update the main content by replacing fragments
Fragment fragment = new UrFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
I use the Sliding Menu library in my app.
The sliding menu is a fragment.
The blue part is the sliding menu when opened.
The red part is static, it doesn't change, it's the main activity btw.
The yellow part is the fragment that change when the user clicks on an item of the sliding menu.
Here's what's wrong when I implement it :
In the sliding menu fragment, I listen for the OnItemClick event, and I create a new fragment depending on the position of the item clicked.
After that, I replace the yellow frame id, with the fragment.
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment frag = null;
switch (arg2) {
case 1:
frag = new ExpFragment();
break;
case 2:
frag = new FormFragment();
break;
case 3:
frag = new CompFragment();
break;
default:
frag = new ContactFragment();
break;
}
transaction.replace(R.id.fragment, frag);
transaction.commit();
}
Looks good huh? Well, no. Here's the logcat exception I got.
06-13 09:28:29.739: E/AndroidRuntime(15422): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
So, if anyone has a clue, or can tell me what to look at, that'd be awesome!
TL;DR : Have 2 fragments (bleu and yellow), the blue has to change the yellow. Gives me an exception.
Thanks,
EDIT: The layouts file:
The activity_main : https://gist.github.com/dommerq/5771887
One fragment item example : https://gist.github.com/dommerq/5771892
The answer was in my fragment java code.
I had :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(com.quentindommerc.flatme.R.layout.f_contact, container);
return v;
}
And I should have :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(com.quentindommerc.flatme.R.layout.f_contact, container, false);
return v;
}
So basically, in the inflate method, put "false" as third parameter.
Edit : Corrected spelling mistake.