任何人都可以解决我的Android Fragment问题吗?

时间:2014-05-27 12:30:45

标签: android performance android-layout android-intent android-fragments

我需要将Data From Activity传递给fragment.here下面我附上了我的代码。请帮助我

这是我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<GridView
    android:id="@+id/grdView_products_order"
    android:layout_width="258dp"
    android:layout_height="match_parent"
    android:numColumns="3" >
</GridView>

<fragment
    android:id="@+id/fragment1"
    android:name="com.example.orderfree.order_screen.OrdersListFragment"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />
</LinearLayout>

我已经在 com.example.orderfree.order_screen.OrdersListFragment 这个类扩展了Fragment.like

public class OrdersListFragment extends Fragment {
              .......
              .......
}

我也在MainActivity类上扩展Activity.Like

public class MainActivity extends Activity{

    private GridView productList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


     Fragment fr=new OrdersListFragment();
     FragmentManager fm = getFragmentManager();
     FragmentTransaction fragmentTransaction = fm.beginTransaction();
     fragmentTransaction.replace(R.id.fragment1, fr);
     fragmentTransaction.commit();

     productList = (GridView) findViewById(R.id.grdView_products_order);

    productList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int                                    position,
                long id) {

        // From Here i need to pass value to That Fragment
       //  Can you tell How to send Value to fragments Dynamically

        }
    });  

   }

3 个答案:

答案 0 :(得分:1)

试试这个

package com.example.myfragments;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.view.WindowManager;

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      Configuration config = getResources().getConfiguration();

      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = 
      fragmentManager.beginTransaction();

      /**
      * Check the device orientation and act accordingly
      */
      if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         /**
         * Landscape mode of the device
         */
         LM_Fragment ls_fragment = new LM_Fragment();
         fragmentTransaction.replace(android.R.id.content, ls_fragment);
      }else{
         /**
         * Portrait mode of the device
         */
         PM_Fragment pm_fragment = new PM_Fragment();
         fragmentTransaction.replace(android.R.id.content, pm_fragment);
      }
      fragmentTransaction.commit();
   }

}

在com.example.mycontentprovider包下创建两个片段文件LM_Fragement.java和PM_Fragment.java。

以下是LM_Fragement.java文件的内容:

package com.example.myfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class LM_Fragment extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      /**
       * Inflate the layout for this fragment
       */
      return inflater.inflate(
      R.layout.lm_fragment, container, false);
   }
}

以下是PM_Fragement.java文件的内容:

package com.example.myfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class PM_Fragment extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      /**
       * Inflate the layout for this fragment
       */
      return inflater.inflate(
      R.layout.pm_fragment, container, false);
   }
}

在res / layout目录下创建两个布局文件lm_fragement.xml和pm_fragment.xml。

以下是lm_fragement.xml文件的内容:

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#7bae16">

   <TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/landscape_message"
   android:textColor="#000000"
   android:textSize="20px" />

<!-- More GUI components go here  -->

</LinearLayout>

以下是pm_fragment.xml文件的内容:

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#666666">

   <TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/portrait_message"
   android:textColor="#000000"
   android:textSize="20px" />

<!-- More GUI components go here  -->

</LinearLayout>

以下是res / layout / activity_main.xml文件的内容,其中包含您的片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

   <fragment
   android:name="com.example.fragments"
   android:id="@+id/lm_fragment"
   android:layout_weight="1"
   android:layout_width="0dp"
   android:layout_height="match_parent" />

   <fragment
   android:name="com.example.fragments"
   android:id="@+id/pm_fragment"
   android:layout_weight="2"
   android:layout_width="0dp"
   android:layout_height="match_parent" />

</LinearLayout>

确保您拥有res / values / strings.xml文件的以下内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MyFragments</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="landscape_message">This is Landscape mode fragment
    </string>
    <string name="portrait_message">This is Portrait mode fragment
    </string>

</resources>

答案 1 :(得分:0)

使用getActivity()您可以访问protectedpublic方法。

您的Activity

public class MyActivity extends Activity {

    private int data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);

        data=125;
   }

    public int getMyIntVariable() {
        return data;
    }
}

您的Fragment

public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        MyActivity activity = (MyActivity)getActivity(); 
        int i = activity.getMyIntVariable();

        return view;
   }
}

答案 2 :(得分:0)

假设您要发送一个名为clickstring的字符串。然后在活动中:

 Bundle bundle=new Bundle();
bundle.putString("name1",clickstring );
fragment = new FragButton1();
fragment.setArguments(bundle);

FragButton1 是您的片段。

然后在 FragButton1

  String strtext=getArguments().getString("name1");
        tv=(TextView)rootView.findViewById(R.id.tv_times1);
        tv.setText(strtext);

我正在接收字符串 strtext 中的数据并将其显示在textview中。