如何在点击期间在片段之间切换? [英] How to switch between fragments during onclick?

查看:29
本文介绍了如何在点击期间在片段之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目要完成.我遇到了一个小问题,我不知道如何解决它.下面是迄今为止该应用程序的图像.

I have a project I'm trying to do. I ran into a little issue and I'm not sure how to get around it. Below is an image of the application so far.

我希望它做的是当用户点击其中一个列表项时,显示你好!它是 Fragment2"的部分更改为我在应用程序中声明的新 xml.因此,如果我单击 ATO listItem,则右侧的片段应更改为您好!这是 ATO 片段"

What I would like it to do is when the user onclicks one of the list items, the part which says "Hello! It's Fragment2" changes to a new xml which I declared in the app. So if I click the ATO listItem, then the fragment on the right side should change to something like "Hello! It's ATO Fragment"

这是我目前的代码:

AndroidListFragmentActivity:

AndroidListFragmentActivity:

package com.exercise.AndroidListFragment;

import android.app.Activity;
import android.os.Bundle;

public class AndroidListFragmentActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

片段 2:

package com.exercise.AndroidListFragment;

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

public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment2, container, false);
    }

}  

MyListFragment1:

MyListFragment1:

package com.exercise.AndroidListFragment;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListFragment1 extends ListFragment {

    String[] options ={
            "ATO", 
            "BETA", 
            "DELT", 
            "PHI DELT",
            "SAE", 
            "SIG NU", 
            "FIJI", 
            "SIG CHI",
            "PHI PSI" 
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListAdapter myListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, options);
        setListAdapter(myListAdapter);
    }

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

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    }
}

Fragment2.xml

Fragment2.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="vertical" >

    <TextView
        android:id="@+id/fragment2text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello! It's Fragment2" />
</LinearLayout>

listfragment1.xml

listfragment1.xml

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

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:text="No data"/>
 </LinearLayout>

main.xml

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

  <fragment
      android:name="com.exercise.AndroidListFragment.MyListFragment1"
      android:id="@+id/fragment1"
      android:layout_weight="1"
      android:layout_width="0px"
      android:layout_height="match_parent" />
  <fragment
      android:name="com.exercise.AndroidListFragment.Fragment2"
      android:id="@+id/fragment2"
      android:layout_weight="2"
      android:layout_width="0px"
      android:layout_height="match_parent" />

</LinearLayout>

推荐答案

不确定让您的代码正常工作的最小修复是什么,但您是否考虑过使用 导航抽屉在片段之间切换?在我看来,官方文档中的示例与您想要实现的几乎完全匹配.

Not sure what's the minimal fix to get your code working, but have you looked at using a Navigation Drawer to switch between the fragments? It looks to me like the example in the official docs matches pretty much exactly what you want to achieve.

关键是为当前显示的片段提供某种容器(而不是像在 XML 中那样使用 ).例如:

A key is to have some kind of container for the currently displayed fragment (instead of using <fragment> like in your XML). For example:

 <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

然后,切换片段是这样的:

Then, switching fragments goes something like this:

Fragment fragment = new Fragment2();
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.content_frame, fragment)
               .commit();

进一步阅读:在运行时向活动添加片段 在 Android 开发者文档中.

Further reading: Add a Fragment to an Activity at Runtime in Android developer docs.

这篇关于如何在点击期间在片段之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆