Hi I have a sliding tab layout with several tabs, each tab contains a list view with several items. When the user clicks a lv item i need to load a different fragment (each lv item has a corresponding fragment with several controls). I managed to load a fragment by inserting a dummy fragment in main activity layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" android:id="@+id/main_activity" > <include android:id="@+id/tool_bar" layout="@layout/tool_bar" android:layout_height="wrap_content" android:layout_width="match_parent" /> <com.grozeaion.www.gvicameraremotecontrol.SlidingTabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:elevation="2dp" android:background="@color/ColorPrimary" android:layout_below="@+id/tool_bar"/> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_height="match_parent" android:layout_width="300dp" android:layout_weight="1" android:background="#bde354" android:layout_below="@+id/tabs"> </android.support.v4.view.ViewPager> <fragment android:layout_width="match_parent" android:layout_height="fill_parent" android:name="com.grozeaion.www.gvicameraremotecontrol.dummy" android:id="@+id/dummy" tools:layout="@layout/fragment_dummy" android:layout_alignTop="@+id/pager" android:layout_toRightOf="@+id/pager" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" /> </RelativeLayout > the list views are populated in class Tab_modes, also here i am trying to replace the dummy fragment. When the app is started the public void onCreate(Bundle savedInstanceState) is called twice and i end up on the first tab with the fragment thet corresponds to second tab lv item ft.replace(R.id.dummy, ModeTriggered.newInstance("Triggered")).commit(); package com.grozeaion.www.gvicameraremotecontrol; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Toast; import java.util.ArrayList; /** * Created by John on 6/12/2015. */ public class Tab_Modes extends ListFragment implements AdapterView.OnItemClickListener { private ArrayList<NameImg> items = new ArrayList<NameImg>(); private int crtMode; private FragmentTransaction ft; public static final Tab_Modes newInstance(int myInt) { Tab_Modes f = new Tab_Modes(); Bundle arguments = new Bundle(); arguments.putInt("position", myInt); f.setArguments(arguments); return f; } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle arguments = getArguments(); crtMode = arguments.getInt("position"); items.clear(); ft = getFragmentManager().beginTransaction(); //Frag2 fragmentObj=(Frag2) geFragmentManager().findFragmentById(R.id.pager); //int crtF = findFragmentById(R.id.pager); switch (crtMode) { case 0: items.add(new NameImg("Manual", "Simple camera Control", R.drawable.hand)); items.add(new NameImg("Bulb", "Long Exposure", R.drawable.bulb)); items.add(new NameImg("Time Lapse", "Frame by Frame Movie", R.drawable.timelapse)); items.add(new NameImg("HDR", "High Dinamic Range", R.drawable.hdr)); items.add(new NameImg("IR", "Infra RED Control", R.drawable.ir)); //ft.replace(R.id.dummy, ModeManual.newInstance("Manual")).commit(); break; case 1: items.add(new NameImg("Triggered", "Trigger camera ", R.drawable.triggrered)); items.add(new NameImg("Dark Room", "Long Exposure", R.drawable.darkroom)); items.add(new NameImg("Lightning", "Frame by Frame Movie", R.drawable.lightning)); //ft.replace(R.id.dummy, ModeTriggered.newInstance("Triggered")).commit(); break; case 2: items.add(new NameImg("Bullet", "Simple camera Control", R.drawable.bullet)); items.add(new NameImg("Water drops", "Long Exposure", R.drawable.waterdrops)); //ft.replace(R.id.dummy, ModeBullet.newInstance("Bullet")).commit(); break; case 3: items.add(new NameImg("Manual", "Simple camera Control", R.drawable.pan_manual)); items.add(new NameImg("Programmed", "Long Exposure", R.drawable.pan_auto)); items.add(new NameImg("Star Tracking", "Long Exposure", R.drawable.starstracker)); //ft.replace(R.id.dummy, ModeManualPan.newInstance("Manual Pan")).commit(); break; case 4: items.add(new NameImg("1", "USB1", R.drawable.usb)); items.add(new NameImg("2", "USB2", R.drawable.waterdrops)); //ft.replace(R.id.dummy, ModeUSB1.newInstance("USB 1")).commit(); break; } setListAdapter(new ModesItemAdapter(getActivity(), R.layout.my_list_item, items)); Toast.makeText(getActivity(), "onCreate :" + crtMode, Toast.LENGTH_LONG).show(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = super.onCreateView(inflater, container, savedInstanceState); return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getListView().setOnItemClickListener(this); switch (crtMode) { case 0: ft.add(R.id.dummy, ModeManual.newInstance("Manual")).commit(); break; case 1: ft.add(R.id.dummy, ModeTriggered.newInstance("Triggered")).commit(); break; case 2: ft.add(R.id.dummy, ModeBullet.newInstance("Bullet")).commit(); break; case 3: ft.add(R.id.dummy, ModeManualPan.newInstance("Manual Pan")).commit(); break; case 4: ft.add(R.id.dummy, ModeUSB1.newInstance("USB 1")).commit(); break; } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getActivity(), "onItemClick :" + parent.getItemAtPosition(0).toString(), Toast.LENGTH_LONG).show(); //ft = getFragmentManager().beginTransaction(); //ft.replace(R.id.dummy, ModeManual.newInstance("selected item :" + position + " ID :" + id),"aaa").commit(); } } 1. I need help to figure out how to load at first rut the fragment associalted with the fist tab lv item ft.replace(R.id.dummy, ModeManual.newInstance("Manual")).commit(); How to find out the existing fragment sot when i click a lv item i can replace the correct fragmet? TreKing, i will try to make my questions clear, but i am not dumping code for debbuging i add it in ordre to make the question clear. If too less/much code is posted jut let me know. Thanks On Thursday, November 26, 2015 at 2:17:35 AM UTC+1, TreKing wrote: > > > On Mon, Nov 23, 2015 at 12:42 PM, gvi70000 <gvi7...@gmail.com > <javascript:>> wrote: > >> Right now i am struggling to just load the first fragment when i click >> the list view. >> >> Can any one help me in achieving this? >> > > You can't just dump your code and expect someone to debug it for you, > especially when your question is not even clear. > > Break your problem down to some specific piece of logic and show exactly > as much code is necessary to illustrate that singular piece of > functionality, and you'll get better assistance. > > > -- > > > ------------------------------------------------------------------------------------------------- > TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago > transit tracking app for Android-powered devices > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.