在Android的自定义列表视图中从一个片段移动到另一个片段

j

我已经从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);

由于我是片段的新手,所以我不知道如何在片段中实现此目的。这就是为什么我在这里问。有人可以指出此代码中的错误吗?提前致谢..

德芙拉斯
  • 您正在使用Support packagefragment所有扩展片段类,检查!
  • 我本人已经使用了其中的代码,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 ::

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

{编辑-示例如何执行片段交易的方法}

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. 
  • 代码中的错误清楚地表明您正在使用supportpackage导入,并且与执行冲突
  • 记住您不能混入进口

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 viewpager 从一个片段移动到另一个片段

在Android中单击ImageView时如何从一个片段移动到另一个片段?

通过从另一个片段中减去一个片段来创建自定义片段

Android:如何在不引起用户注意的情况下从一个片段移动到另一个片段?

MVC从一个模型视图移动到另一个模型视图

如果从一个片段移动到另一个片段,则在发布版本中显示汉堡图标代替后图标

如何从列表视图从一个片段发送到另一个数据

C:将元素从一个列表移动到另一个列表

将字段从一个列表移动到另一个列表

谷歌表。自定义脚本将随机值从一个列移动到另一个

如何在不停机的情况下将自定义域从一个Firebase项目移动到另一个项目?

将自定义对象传递给另一个片段

从一个列表框移动到另一个 tkinter

改变另一个片段的片段(列表)

将自定义 CLI 命令移动到另一个文件

单击列表项时如何从一个片段切换到另一个片段?

片段A的原始自定义模型对象正受到另一个片段B的更改的影响

将数据从一个片段传递到另一个片段

从一个片段到另一个片段的交易问题

如何从一个片段到另一个片段获取捆绑数据?

无法从一个片段接收数据到另一个片段

将 Spinner 值从一个片段传递到另一个片段

Android片段被另一个片段覆盖

将带有数据表的列表视图移动到另一个列表视图中

无法从具有列表视图的片段导航到另一个片段

在Android上从一个片段转到另一个片段时的动画滞后

如何将HashSet对象从一个片段传递到android中的另一个片段

如何在另一个自定义视图中添加自定义视图?

Android - 从另一个片段内的回收器视图调用片段