【问题标题】:Moving from one fragment to another in a custom listview in android在android的自定义列表视图中从一个片段移动到另一个片段
【发布时间】:2014-08-02 05:02:51
【问题描述】:

我从androidhive.info 为我的应用程序创建了一个导航抽屉。在那个导航抽屉的一个片段中,我创建了一个自定义列表视图,它显示带有名称和图像的元素。现在单击自定义列表视图的项目后,我需要显示单击的列表元素的详细信息。为此,我创建了另一个片段。现在单击 listview 元素后,我应该转到所选元素的详细信息。我用来从一个片段移动到另一个片段的代码是这样的:

Fragment fragment=new ImportantNumbersFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

getSupportFragmentManager() 在 Eclipse 中显示如下错误:The method getSupportFragmentManager() is undefined for the type PagesFragment。这是我的自定义列表视图片段的代码。

PagesFragment.class

public class PagesFragment extends ListFragment implements OnItemClickListener{

    Typeface tf;
    ListView lv;
    List<SimpleRow>rowItems;
    public PagesFragment(){}

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

    View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
    Helper help=new Helper(getActivity().getApplicationContext());
    String choice1="സര്‍ക്കാര്‍ സ്ഥാപനങ്ങള്‍";
    String choice1new=help.changemalayalam(new Data(choice1));
    String choice2="പത്രം";
    String choice2new=help.changemalayalam(new Data(choice2));
    String choice3="ന്യൂസ് ചാനല്‍";
    String choice3new=help.changemalayalam(new Data(choice3));
    String choice4="യൂണിവേഴ്സിറ്റികള്‍";
    String choice4new=help.changemalayalam(new Data(choice4));
    String choice5="പഞ്ചായത്ത് ഓഫീസ്";
    String choice5new=help.changemalayalam(new Data(choice5));
    String choice6="ഇന്‍ഷുറന്‍സ്";
    String choice6new=help.changemalayalam(new Data(choice6));
    String choice7="പോലീസ് സ്റ്റേഷന്‍";
    String choice7new=help.changemalayalam(new Data(choice7));
    String choice8="ഫയര്‍ സ്റ്റേഷന്‍";
    String choice8new=help.changemalayalam(new Data(choice8));
    ArrayList<String>choice=new ArrayList<String>();
    choice.add(choice1new);
    choice.add(choice2new);
    choice.add(choice3new);
    choice.add(choice4new);
    choice.add(choice5new);
    choice.add(choice6new);
    choice.add(choice7new);
    choice.add(choice8new);

    lv=(ListView)rootView.findViewById(R.id.list1);
    rowItems=new ArrayList<SimpleRow>();

     for (int i = 0; i < choice.size(); i++) {
        SimpleRow item = new SimpleRow(choice.get(i));
        rowItems.add(item);
        CustomSimpleListAdapter adapter = new CustomSimpleListAdapter(getActivity().getApplicationContext(),R.layout.list_single, rowItems);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    }

    return rootView;
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Toast.makeText(getActivity().getApplicationContext(), "You clicked on position "+position,Toast.LENGTH_LONG).show();

//Here upon clicking the list element, I need to go to ImportantNubersFragment

    Fragment fragment=new ImportantNumbersFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.content_frame, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Toast.makeText(getActivity().getApplicationContext(), "You clicked on position "+arg2,Toast.LENGTH_LONG).show();
    }
}

ImportantNumbersFragment.class

public class ImportantNumbersFragment extends Fragment {

    public ImportantNumbersFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_container, container, false);
        return rootView;
    }

}

fragment_important_numbers.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout android:name="fragments.YourInitialFragment"
        android:id="@+id/fragment_container"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dip" />

 </LinearLayout>

单击自定义列表视图中的列表元素后,我想从 PagesFragment 移动到 ImportantNumbersFragment。所以在 OnListItemClick 中,我给出了从片段移动到另一个片段的代码。我必须以与使用以下意图从一个活动移动到另一个活动相同的方式执行此操作: Intent i=new Intent(First.this,Second.class); startActivity(i);.

由于我是 Fragment 的新手,我不知道如何在 Fragment 中实现这一点。这就是我在这里问的原因。有人可以指出此代码中的错误。提前谢谢..

【问题讨论】:

    标签: android android-intent android-fragments android-listview


    【解决方案1】:
    • 您是否在所有扩展片段中使用Support package 代替fragment 上课,检查! .
    • 我自己使用了AndroidHive 的代码,它运行良好 .
    • 它不使用Support package 扩展片段类 .
    • 您在代码中的某处使用了支持导入,请确保您 保持进口统一 .
    • Programatically Speaking for ex:: 如果您正在使用支持包,请使用这些代码

    import android.support.v4.app.ActionBarDrawerToggle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.widget.DrawerLayout;
    

    • 如果您不使用支持包,请使用代码导入 ::

    import android.app.ActionBarDrawerToggle;
    import android.app.Fragment;
    import android.app.FragmentActivity;
    import android.app.FragmentTransaction;
    import android.widget.DrawerLayout;
    

    {EDIT - 关于如何执行片段事务的方式之一的示例}

    MainActivity.java

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTransaction;
    import android.util.Log;
    
    public class MainActivity extends FragmentActivity {
    
        Fragment fragment;
        String className;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.d("MainActivity", "onCreate");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Store the name of the class
            className=MainActivity.class.getSimpleName();
    
            //First fragment should be mounted on oncreate of main activity
            if (savedInstanceState == null) {  
                /*fragment=FragmentOne.newInstance();
                getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).addToBackStack(className).commit();
                */
    
                Fragment newFragment = FragmentOne.newInstance();  
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();  
                ft.replace(R.id.container, newFragment).addToBackStack(null).commit();  
                Log.d("FRAGMENT-A", "fragment added to backstack");
            }
    
        }
    }
    

    FragmentOne.java

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.util.Log;
    import android.util.TypedValue;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.SeekBar;
    import android.widget.Spinner;
    
    import com.sample.screennavigation.UtilRangeSeekBar.OnRangeSeekBarChangeListener;
    
        public class FragmentOne extends Fragment{
    
            //Declare variables that hold the data for configuration change
    
            public static FragmentOne newInstance(){
                Log.d("FragmentOne", "newInstance");
                FragmentOne fragment = new FragmentOne();
                return  fragment;
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                Log.d("FragmentOne", "onCreateView");
                View view=inflater.inflate(R.layout.fragment_one, container, false);
    
                return view;
            }
    
        }
    

    {EDIT-2}


    • PagesFragment ... 正在某处使用支持包,请检查 它
    • 检查pagefragment 的导入

    But the getSupportFragmentManager() is showing an error in eclipse like this: The method getSupportFragmentManager() is undefined for the type PagesFragment. 
    
    • 代码中的错误清楚地表明您正在使用支持包导入 它与执行冲突
    • 请记住,您不能混合导入

    希望对您有所帮助!如果您需要任何其他信息,请告诉我

    【讨论】:

    • 您能否向我解释一下如何创建一个新片段及其布局,以及如何在列表项点击事件上从我当前的片段移动到新片段..所有代码都必须用于移动一个片段到另一个片段...
    • @TeeJay ....查看我的答案的编辑(我使用支持包).....这就是我使用片段事务的方式......你在做什么也是正确的.....你只需要检查你的进口并保持一致性
    • @ Tee Jay .... 如果我的回答有帮助 .... 将其标记为已接受! ....检查顶部的绿色箭头!
    • 其实你说的可能是对的。但这不是我问题的解决方案。实际上,我自己找到了解决方案。这就是为什么没有将您的答案标记为已接受答案的原因。无论如何,你给我一些关于这方面的信息。谢谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多