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

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

问题描述

我有一个项目,我想要做的。我遇到了一个小问题,我不知道如何解决它。下面是应用程序的图像为止。

我想它做的是,当用户onclicks一个列表项,部分它说你好!这是Fragment2的转变,而我在应用程序中声明一个新的XML。所以,如果我点击ATO的listItem,然后在右侧的片段应更改为类似你好!这是ATO片段

下面是我的code到目前为止:

AndroidListFragmentActivity:

 包com.exercise.AndroidListFragment;

进口android.app.Activity;
进口android.os.Bundle;

公共类AndroidListFragmentActivity延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }
}
 

Fragment2:

 包com.exercise.AndroidListFragment;

进口android.app.Fragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;

公共类Fragment2扩展片段{

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        // TODO自动生成方法存根
        返回inflater.inflate(R.layout.fragment2,集装箱,假);
    }

}
 

MyListFragment1:

 包com.exercise.AndroidListFragment;

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

公共类MyListFragment1扩展ListFragment {

    的String []选项= {
            ATO
            BETA,
            DELT
            PHI DELT
            SAE
            SIG NU,
            斐济,
            SIG CHI,
            PHI PSI
    };

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        ListAdapter myListAdapter =新的ArrayAdapter<字符串>(getActivity(),android.R.layout.simple_list_item_1,期权);
        setListAdapter(myListAdapter);
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.listfragment1,集装箱,假);
    }

    @覆盖
    公共无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        // TODO自动生成方法存根
        Toast.makeText(getActivity(),getListView()getItemAtPosition(位置)的ToString(),Toast.LENGTH_LONG。).show();
    }
}
 

Fragment2.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    <的TextView
        机器人:ID =@ + ID / fragment2text
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=您好!这是Fragment2/>
< / LinearLayout中>
 

listfragment1.xml

 < XML版本=1.0编码=UTF-8&GT?;
 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
         机器人:方向=垂直
         机器人:layout_width =match_parent
         机器人:layout_height =match_parent
         机器人:以下属性来=8DP
         机器人:paddingRight =8DP>

     < ListView的机器人:ID =@ ID / Android的:清单
               机器人:layout_width =match_parent
               机器人:layout_height =match_parent
               机器人:layout_weight =1
               机器人:drawSelectorOnTop =FALSE/>

     < TextView的机器人:ID =@ ID /安卓:空
               机器人:layout_width =match_parent
               机器人:layout_height =match_parent
               机器人:文本=无数据/>
 < / LinearLayout中>
 

main.xml中

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=横向>

  <片段
      机器人:名称=com.exercise.AndroidListFragment.MyListFragment1
      机器人:ID =@ + ID /片段1
      机器人:layout_weight =1
      机器人:layout_width =0px
      机器人:layout_height =match_parent/>
  <片段
      机器人:名称=com.exercise.AndroidListFragment.Fragment2
      机器人:ID =@ + ID / fragment2
      机器人:layout_weight =2
      机器人:layout_width =0px
      机器人:layout_height =match_parent/>

< / LinearLayout中>
 

解决方案

不知道什么是最小的修复,让您的code的工作,但你看使用的Navigation抽屉 的片段之间进行切换?它看起来对我来说,在官方文档的例子匹配pretty的多,你想达到什么。

一个关键是要有某种容器当前显示的片段(而不是使用<片断> 就像在你的XML)。例如:

 <的FrameLayout
        机器人:ID =@ + ID / content_frame
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>
 

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

 片段片段=新Fragment2();
//插入片段替换现有的片段
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.content_frame,片段)
               。承诺();
 

延伸阅读:添加一个片段的活动在运行时 Android中开发者文档。

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.

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"

Here is my code so far:

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);
    }
}

Fragment2:

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:

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

<?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

<?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.

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();

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

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

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