Android Fragments 在ListView上打开具有不同信息的同一片段

z5btuh9x  于 2023-10-19  发布在  Android
关注(0)|答案(2)|浏览(129)

在我的应用程序中有这个“帮助部分”,它由一个包含不同项的ListView组成。当我单击ListView的每个项目时,我试图打开相同的片段,但每次打开它时,它应该显示不同的信息。
我尝试使用Intent并在打开片段时使用getExtra,但没有成功。我有点迷路了,任何帮助都将不胜感激。
下面是我的“帮助活动”代码:

ListView list_main;
//Itens do LV
ListView list;
// ListView items
String[] item = {"   Como começar?", "   Como adcionar uma carona?", "    Como remover uma carona?",
        "   Como faço para pegar uma carona com alguém?", "   Minhas informações estão seguras?", "   Como editar meu perfil?",
        "   Tive um problema com uma carona, o que fazer?", "   Como trocar minha senha?", "   Preciso dividir o valor da carona?",
        "Recomendações aos usuários", "Entre em contato"
};
// ListView images
Integer[] imageId = {
        R.drawable.information, R.drawable.plus, R.drawable.removecloud, R.drawable.network,
        R.drawable.lock, R.drawable.user, R.drawable.siren, R.drawable.lock, R.drawable.piggy,
        R.drawable.recomenda, R.drawable.mail};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_help_interno);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("Dúvidas Frequentes");

    HelpList adapter = new HelpList(HelpInterno.this,item,imageId); // Custom adapter
    list_main = (ListView) findViewById(R.id.list_main);
    list_main.setAdapter(adapter);

    list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // What to do ?

        }
    })
}

谢谢

23c0lvtd

23c0lvtd1#

试着这样做:

//make an instance for your fragment inside your fragment
public static your_fragment_name newInstance(String var1, String var2, int var3) {
    your_fragment_name fragment = new your_fragment_name();
    Bundle args = new Bundle();
    args.putString(first_key, var1);
    args.putString(second_key, var2);
    args.putInt(third_key, var3);
    your_fragment_name.setArguments(args);
    return fragment;
}

//get variables inside your fragment in this way
private getExtras(){
    String var1 = getArguments().getString(first_key);
    String var2 = getArguments().getString(second_key);
    int var2 = getArguments().getInt(third_key);
}

//call your fragment in this way
your_fragment_name fragment = your_fragment_name.newInstance(var1, var2, var3);

getSupportFragmentManager().beginTransaction()
                      .replace(/*your view detail id*/,fragment).commit();
ibrsph3r

ibrsph3r2#

你可以在documentation中看到如何将数据传递给一个新的片段;在你场景中,你可以像下面这样做,把这段代码放在你的片段类中

public static HelpDetailFragment getStartFragment(String item, int imageId) {
        Bundle arguments = new Bundle();
        arguments.putString(EXTRA_ITEM, imageId);
        arguments.putString(EXTRA_IMAGE_ID, imageId);
        HelpDetailFragment fragment = new HelpDetailFragment();
        fragment.setArguments(arguments);
        return fragment;
    }

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

        View rootView = inflater.inflate(R.layout.fragment_help, container, false);

        Bundle extras = getArguments();
        if (extras != null) {
            String item = extras.getString(EXTRA_ITEM);
            int imageid = extras.getInt(EXTRA_IMAGE_ID);
        }

        ....

        return rootView;
    }

然后在单击列表项时导航到片段

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
getSupportFragmentManager()
                        .beginTransaction()
                        .replace(/*your view detail id*/, HelpFragment.getStartFragment(item[position], imageId[position]))
                        .addToBackStack("help")
                        .commit();
}

在此处阅读片段导航

相关问题