Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to replace fragment run time
    primarykey
    data
    text
    <p>I want to add and replace Frame layout with fragment on fragment-1 and fragment-2 on item click of Grid and List item.</p> <p>I had created main.xml class</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;LinearLayout android:layout_width="0dp" android:layout_weight="1" android:orientation="vertical" android:layout_height="fill_parent" &gt; &lt;fragment android:id="@+id/fragment1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" class="com.example.fragment.Fragment1" /&gt; &lt;View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#000000" /&gt; &lt;fragment android:id="@+id/fragment2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" class="com.example.fragment.Fragment2" /&gt; &lt;/LinearLayout&gt; &lt;View android:layout_width="1dp" android:layout_height="fill_parent" android:background="#000000" /&gt; &lt;FrameLayout android:id="@+id/fragment3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="2"/&gt; </code></pre> <p></p> <p>Here is my fragment_1.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="8dp" android:paddingRight="8dp" &gt; &lt;GridView android:id="@+id/Grid1" android:layout_width="match_parent" android:numColumns="auto_fit" android:columnWidth="100dp" android:layout_height="0dp" android:layout_weight="1" android:drawSelectorOnTop="false" /&gt; </code></pre> <p></p> <p>and here is my row_fragment1_list.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="15dp" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/img_view" android:layout_width="50dp" android:layout_height="50dp" android:background="#CCCCCC" android:textSize="16sp" android:textStyle="bold" /&gt; &lt;TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /&gt; </code></pre> <p></p> <p>and fragment_3.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/tv_fragment3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="22sp" android:gravity="center" android:text="This is 3rd Fragment" /&gt; </code></pre> <p></p> <p>and here is my Main.xml</p> <pre><code>public class Main extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } </code></pre> <p>and here is Fragment1.class</p> <pre><code>public class Fragment1 extends Fragment implements OnItemClickListener { Activity myActivity; GridView mGridView; private String ListItem[] = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 7"}; private int imgID[] = {R.drawable.admin_access_rule, R.drawable.admin_backup,R.drawable.admin_browsesite, R.drawable.admin_comment_post,R.drawable.admin_content, R.drawable.admin_content_type,R.drawable.admin_logout,}; @Override public void onAttach(Activity activity) { super.onAttach(activity); this.myActivity = activity; Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show(); View view = inflater.inflate(R.layout.fragment_1,container, false); mGridView = (GridView)view.findViewById(R.id.Grid1); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show(); super.onActivityCreated(savedInstanceState); } @Override public void onStart() { super.onStart(); mGridView.setAdapter(new GridBaseAdapter(myActivity)); mGridView.setOnItemClickListener(this); } private class GridBaseAdapter extends BaseAdapter { LayoutInflater mLayoutInflater = null; public GridBaseAdapter(Context mContext) { mContext = myActivity; mLayoutInflater = LayoutInflater.from(mContext); } @Override public int getCount() { return ListItem.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return ListItem.length; } @Override public View getView(final int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = mLayoutInflater.inflate(R.layout.row_fragment_list, null); } ImageView mImageView = (ImageView) convertView.findViewById(R.id.img_view); mImageView.setImageResource(imgID[position]); TextView tvUserEmail = (TextView) convertView.findViewById(R.id.text2); tvUserEmail.setText("Sub " +ListItem[position]); return convertView; } } @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View view, int position, long id) { TextView txt3 = (TextView)myActivity.findViewById(R.id.tv_fragment3); txt3.setText("1st Fragment's : " + position +" Item Clicked"); } } </code></pre> <p>My Fragment2.class</p> <pre><code>public class Fragment2 extends ListFragment { Activity myActivity; private String ListItem[] = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 6","Item 7"}; @Override public void onAttach(Activity activity) { super.onAttach(activity); this.myActivity = activity; Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show(); /** Creating an array adapter to store the list of countries **/ ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(inflater.getContext(), android.R.layout.simple_list_item_1,ListItem); /** Setting the list adapter for the ListFragment */ setListAdapter(adapter); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show(); super.onActivityCreated(savedInstanceState); } @Override public void onListItemClick(ListView l, View v, int position, long id) { Toast.makeText(myActivity.getApplicationContext(), position +" Item Clicked", Toast.LENGTH_SHORT).show(); TextView txt3 = (TextView)myActivity.findViewById(R.id.tv_fragment3); txt3.setText("2nd Fragment's : " + position +" Item Clicked"); super.onListItemClick(l, v, position, id); } } </code></pre> <p>And Last my Fragment3.class</p> <pre><code>public class Fragment3 extends Fragment { Activity myActivity; TextView txt_view; @Override public void onAttach(Activity activity) { super.onAttach(activity); this.myActivity = activity; Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show(); View view = inflater.inflate(R.layout.fragment_3,container, false); txt_view = (TextView)view.findViewById(R.id.tv_fragment3); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show(); super.onActivityCreated(savedInstanceState); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload