When i replace fragment Listview place fragment Listview
I had trouble this in photo but i can't fix it .
i need to fix it,plz.
UI_screenshot
Code
#Override
public void onActivityCreated(Bundle savedInstanceState) {
final String[] titel = {"asdasd", "asda", "bdfg", "asdqweqwe"};
int idimg = R.mipmap.icon_main_list;
MyArrayAdapter adapter = new MyArrayAdapter(getActivity(), titel, idimg);
setListAdapter(adapter);
super.onActivityCreated(savedInstanceState);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
Anmalz anmalz=new Anmalz();
Main_List_Fragment main_list_fragment=new Main_List_Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, anmalz);
fragmentTransaction.remove(main_list_fragment).commit();
break;
Change your onCreateView() to -
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment_list, container, false);
}
Also, in onListItemClick(), you should not remove/replace the same fragment again and again. You should just refresh the list adapter.
Related
public class Fragment_01 extends ListFragment {
String string[]={"Contact 1","Contact 2","Contact 3","Contact 4","Contact 5"};
public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState) {
View myview = inflater.inflate(R.layout.fragment_fragment_01, container, false);
ListView listView= (ListView) myview.findViewById(android.R.id.list);
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,string);
listView.setAdapter(array);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment_03 f3 = new Fragment_03();
ft.replace(R.id.linearLayout2, f3);
ft.commit();
}
});
return myview;
}
}
Do not change a fragment inside another. Have a frame layout n the activity. On click of an item in the list view, send the event to the activity through an interface and in the activity replace the fragment. This is the right way of replacing fragments.
Fragment_03 f3 = new Fragment_03();
FragmentManager fragmentManager = ((FragmentActivity) getContext()).getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new Fragment_03())
.commit();
//I am using this code without listView and this is working fine then why the other is not working ??
public class Fragment_01 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState){
View myview=inflater.inflate(R.layout.activity_fragment_01,container,false);
Button btn = (Button) myview.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm =getFragmentManager();
FragmentTransaction ft =fm.beginTransaction();
Fragment_03 f3 = new Fragment_03();
ft.replace(R.id.linearLayout2,f3);
ft.commit();
}
});
return myview;
}
}
I have two fragments, FragmentCity and Fragmentnumber1. When screen orientation changes on fragmentnumber1, it goes back to FragmentCity. Does anyone know how to solve this? I appreciate it if you could show me how to solve this issue
FragmentCity
public class FragmentCity extends Fragment {
final String[] items = new String[]{"FC1", "FC2", "FC3", "FC4","FC5"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.haz, container, false);
ListView list = (ListView) view.findViewById(R.id.fir);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Fragment myFragment = null;
switch (arg2) {
case 0:
myFragment = new Fragmentnumber1();
break;
case 1:
myFragment = new Fragmentnumber2();
break;
case 2:
myFragment = new Fragmentnumber3();
break;
case 3:
myFragment = new Fragmentnumber4();
break;
case 4:
myFragment = new Fragmentnumber5();
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().addToBackStack("frag")
.replace(R.id.container2, myFragment)
.commit();
}
});
return view;
}
}
Fragmentnumber1
public class Fragmentnumber1 extends Fragment {
final String[] items = new String[]{"num1", "num2", "num3"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.haz, container, false);
ListView list = (ListView) view.findViewById(R.id.fir);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Fragment myFragment = null;
switch (arg2){
case 0:
myFragment = new num1();
break;
case 1:
myFragment = new num2();
break;
case 2:
myFragment = new num3();
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().addToBackStack(null)
.replace(R.id.container2, myFragment)
.commit();
}
});
return view;
}
}
You have not posted code of activity class but i am guessing that you have loaded FragmentCity fragment in onCreate of your activity
So when you change orientation it again call onCreate and load FragmentCity fragment so by putting proper condition you can manage it.
Try adding the following line to your containing activity (probably MainActivity) in the manifest
<activity android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
This will allow the Activity to maintain it's state across orientation changes.
I am using android PagerStrip Slidding tab. I have two fragments. In the first fragment I have a listview.I want to navigate to the second fragment wen i click on the listview item in the first fragment . How do i do this? Thanks.
Here is the OnClick Listener in Fragment1
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragmentcontainer,Fragment2.newInstance());
transaction.addToBackStack(null);
}
});
//Here is my Fragment 2 which i want to navigate to
public class Fragment2 extends Fragment {
private static final String ARG_POSITION = "position";
public static final String TAG = Fragment2.class
.getSimpleName();
#InjectView(R.id.textView)
TextView textView;
private int position;
public static Fragment2 newInstance(int position) {
Fragment2 f = new Fragment2();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
public static Fragment2 newInstance(){
Fragment2 f = new Fragment2();
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
//setContentView(R.layout.activity_maps);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_card,container,false);
ButterKnife.inject(this, rootView);
ViewCompat.setElevation(rootView, 50);
textView.setText("CARD "+position);
return rootView;
}
}
I have figured out how to do this.
1. In the MainActivity your might already have
ViewPager pager;
and in your Oncreate method you also have
......
pager.setAdapter(adapter);
Now create the method below in the MainActivity below.ie(The Activity which controls all your fragments etc)
public void setCurrentItem (int position, boolean smoothScroll) {
pager.setCurrentItem(item,true);
}
Now Lets assume your fragment1 has a listview or button.And you want to navigate to fragment2 by onClick.
This is what you need to do
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
((MainActivity)getActivity()).setCurrentItem (1, true);
}
});
//Remeber int position is the position of the fragment you want to navigate to in your Adapter
What I want to do is to call one fragment from another on click on item of listview. Program builds without errors however it crash on click on item. Btw I’m making here local Fragment_3 object, I have got already done it in my mainactivity, how to pass it to this function?
Fragment_1.java content:
public class Fragment_1 extends SherlockFragment implements OnItemClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.fragment_1, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView1);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
Fragment Fragment3 = new Fragment_3();
Integer fragmentId = (Integer) v.getTag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(fragmentId, Fragment3);
fragmentTransaction.commit();
}
}
My suspicion is that v.getTag() is null; So Integer fragmentId = (Integer) v.getTag(); will end up in fragmentId being null. When unboxing that to an int you'll get a NullPointerException. Why don't you replace fragmentId in fragmentTransaction.replace(fragmentId, Fragment3); with the id of the container that holds current instance of Fragment_1?
I want to replace a fragment containing a list of items with a new fragment depending on the item clicked. This is my code, how do I need to approach this? Should I declare my ListView, Adapter and such in onCreateView or onActivityCreated? The container is a ViewPager if that makes a difference.
public class PreferenceListFragment extends SherlockFragment
{
ListView lv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return (LinearLayout)inflater.inflate(R.layout.settings, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lv = (ListView)getView().findViewById(R.id.SettingsLV);
lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Preferences.TITLES));
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("FragmentList", "Item clicked: " + arg2);
// Switch case that starts fragment depending on 'position'.
Fragment1 f1 = new Fragment1();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, f1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
);
}
}
Link to some very weird behaviour from my app.
The setting of adapter and listview should be done in onActivityCreated. OnCreateView is just to draw the user interface for the first time. The processing should be in onActivityCreated(). Refer http://developer.android.com/guide/topics/fundamentals/fragments.html