首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用静态片段替换活动内容

用静态片段替换活动内容
EN

Stack Overflow用户
提问于 2014-10-11 05:56:22
回答 2查看 1.9K关注 0票数 0

我有一个应用程序,它在一开始就使用活动。它正在加载一个xml文件,以在onStart过程中设置内容。在此之后,内容将使用一个片段进行更改,该片段允许用户在EditText视图中输入内容。这个片段是在运行时通过使用FragmentManager和事务动态加载的。在这个片段上还有一个按钮要继续。当用户单击该按钮时,另一个内容将在该按钮的onClick-过程中加载。我试图用另一个名为“ListFrag.java”的片段替换第一个片段,该片段使用一个名为“list_frag.xml”的布局文件。在纵向模式下,这是一个简单的xml布局,上面有一些视图.

当设备处于横向模式时,问题就开始了。我用“getResources().getConfiguration().orientation“.检查这个此时,我想用布局“ListFrag.java”的另一个版本来更改开始片段“list_frag.xml”。因此,我把这个布局放在一个叫做“布局-土地”的新的res文件夹中。此布局为左窗格定义了静态片段,为右窗格定义了框架布局。如果用户单击左侧窗格中的项,框架布局充当要加载详细碎片的容器。

我真的不知道是否可以使用带有静态片段定义的布局来更改带有片段的活动的内容。我已经试过了所有的东西,但都没有用。也许有人有个主意。

以下是项目中单个文件的源代码。为了缩短代码,我删除了导入语句: MainActivity:

代码语言:javascript
复制
package com.example.wbslideshow;

public class MainActivity extends Activity implements MainFrag.onstartFragBtnClickListener {
    public static final String KEYVAL = "startpath"; 

    Bundle mySavedInstanceState;
    MainFrag newMainFrag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mySavedInstanceState = savedInstanceState;

        //initialize the preferences from the xml-file 
        //if app is running the first time this will be taken from the xml-file
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        //load the MainFrag to select a path to the images and start-button
        newMainFrag = new MainFrag();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.mycontainer, newMainFrag);
        transaction.addToBackStack(null);
        transaction.commit(); 

    }

    //procedure from the interface of the MainFrag class to look for images in the given path
    @Override
    public void onstartFragBtnClicked(String root) 
    {
        //call procedure "private boolean LandscapeMode()" to check the mode
        if (!LandscapeMode())
        {
            ListFrag newListFrag = new ListFrag();
            //put the value from the EditText field of MainFrag class into the arguments for the ListFrag class
            Bundle args = new Bundle();
            args.putString(ListFrag.FRAG_MESSAGE_DEF_Input, root);
            newListFrag.setArguments(args);

            //change the fragments dynamically
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.mycontainer, newListFrag);
            transaction.addToBackStack(null);
            transaction.commit(); 
        }
        else
        {
            //remove the MainFrag
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.remove(newMainFrag);
            transaction.commit(); 

            //load the static ListFrag 
            ListFrag newListFrag = new ListFrag();
            //put the value from the EditText field of MainFrag class into the arguments for the ListFrag class
            Bundle args = new Bundle();
            args.putString(ListFrag.FRAG_MESSAGE_DEF_Input, root);

            //load the ImgFrag for the right pane into the FrameLayout 
            ImgFrag myImgFrag = new ImgFrag();
            if (myImgFrag != null) 
            {
                transaction = getFragmentManager().beginTransaction();
                transaction.add(R.id.myImgContainer, myImgFrag);
                transaction.commit();
            }
        }
    }

    private boolean LandscapeMode() { 
         if (getResources().getConfiguration().orientation ==  
                   Configuration.ORIENTATION_PORTRAIT) {
             return false;
             }

         else 
             if (getResources().getConfiguration().orientation ==  
               Configuration.ORIENTATION_LANDSCAPE) {
                 return true;
         } else return false;
    } 


}

MainFrag:包装com.example.wbslideshow;

代码语言:javascript
复制
public class MainFrag extends Fragment implements OnClickListener{ //, OnSharedPreferenceChangeListener {

    /*
     * Constant is representing the value of the android:key from preferences.xml. 
     * This value is found in android:defaultValue
    */
    public static final String KEYVAL = "startpath"; 
    public static final String JPGVAL = "pref-jpg";
    public static final String PNGVAL = "pref-png";

    EditText myEditText;
    SharedPreferences sharedPref;

    //define the interface to communicate with the main activity when user clicked the button
    private onstartFragBtnClickListener mCallback;
    public interface onstartFragBtnClickListener {
        public void onstartFragBtnClicked(String myInput);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState ) {
        View view = inflater.inflate(R.layout.main_frag, container, false);

        //instantiate the start button and register the onClickListener
        Button start = (Button) view.findViewById(R.id.startBtn);
        start.setOnClickListener(this);

        //read the current path to the pics from SharedPreferences file
        Activity myActivity = getActivity();
        sharedPref = PreferenceManager.getDefaultSharedPreferences(myActivity.getBaseContext());

        //registerPreferenceListener();
        myEditText = (EditText) view.findViewById(R.id.inputSearchPath);
        myEditText.setText(sharedPref.getString(KEYVAL, "/"));
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        //read the current path to the pics from SharedPreferences file
        Activity myActivity = getActivity();
        sharedPref = PreferenceManager.getDefaultSharedPreferences(myActivity.getBaseContext());
        myEditText.setText(sharedPref.getString(KEYVAL, "/"));
    }

    @Override
    public void onAttach (Activity activity) {
        super.onAttach(activity);
        try
        {mCallback = (onstartFragBtnClickListener) activity;}
        catch (ClassCastException e) 
        {throw new ClassCastException(activity.toString()
                  + " must implement OnControlButtonClickedListener");}
    }


    //change to ListFrag class if user clicked the button
    @Override
    public void onClick(View view) 
    {
        Activity myActivity = getActivity();
        if (myActivity != null) 
        {
            //Get the users input for to pass it to the activity
            String root = ((EditText) myActivity.findViewById(
                    R.id.inputSearchPath)).getText().toString();

            //Call the interface method in the activity to go on
            mCallback.onstartFragBtnClicked(root);
        }
    }

}

ListFrag:

代码语言:javascript
复制
package com.example.wbslideshow;
public class ListFrag extends ListFragment{

    ImageView image;
    //Arrays to get files and folders 
    private List<String> listItem = null;
    //TextView  Object for the headline 
    private TextView TVmyPath;
    //global variable taken the start path once 
    //used to compare when user clicked an item
    private String g_startPath, g_myInput;

    /* define a constant to take the passed input string from the start fragment */
    public static String FRAG_MESSAGE_DEF_Input = "com.example.wbslideshow.CALL_ListFragment"; 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState ) {
        View  myview = inflater.inflate(R.layout.list_frag, container, false);
        TVmyPath = (TextView) myview.findViewById(R.id.startpath);
        return myview;
    } 

    @Override
    public void onStart() 
    {
        super.onStart();
        Activity myActivity = getActivity();

        //get an instance of the image and make it invisible 
        image = (ImageView) myActivity.findViewById(R.id.imageView1);
        image.setVisibility(View.INVISIBLE);

         Bundle args = getArguments();
         if (args != null) { g_startPath = args.getString(FRAG_MESSAGE_DEF_Input); } 

         //save the input the very first time to compare later with new item click
         g_myInput = g_startPath;

         getDir(g_startPath);  
    }   

    private void getDir(String p_startPath)
    {
        //set the headline
        TVmyPath.setText("Location: " + p_startPath);

        listItem = new ArrayList<String>();

        File f = new File(p_startPath);
        //file array which get's all the folders and files from the input path
        File[] files = f.listFiles();

        //startPath changed if user clicked a new folder 
        if(!g_myInput.equals(p_startPath))
        {
            //alter g_myInput for the next comparison
            g_myInput = p_startPath;
            //put this item to make it possible to get one directory up
            listItem.add("../");
        }

        Arrays.sort(files, filecomparator);

        for(int i=0; i < files.length; i++)
        {
            if(files[i].isFile())
            {
                String filename = files[i].getName();
                //get the file extension 
                int z = filename.lastIndexOf('.');
                //read the file extension
                String wbname = filename.substring(z+1);
                if (wbname.equalsIgnoreCase("jpg"))  {listItem.add(filename);}
                if (wbname.equalsIgnoreCase("jpeg")) {listItem.add(filename);}
            }
            else {listItem.add(files[i].getName() + "/");}
        }
        ArrayAdapter<String> fileList =
                new ArrayAdapter<String>(getActivity(), R.layout.row, listItem);
        Activity myActivity = getActivity();
        ListView myList = (ListView) myActivity.findViewById(android.R.id.list);
        myList.setAdapter(fileList); 
    }

    //procedure to sort the arrays
    Comparator<? super File> filecomparator = new Comparator<File>(){

        public int compare(File file1, File file2) {
            if(file1.isDirectory()){
                if (file2.isDirectory()){
                    return String.valueOf
                            (file1.getName().toLowerCase(Locale.getDefault())).compareTo
                            (file2.getName().toLowerCase(Locale.getDefault()));
                }else{
                    return -1;
                }
            }else {
                if (file2.isDirectory()){
                    return 1;
                }else{
                    return String.valueOf
                            (file1.getName().toLowerCase(Locale.getDefault())).compareTo
                            (file2.getName().toLowerCase(Locale.getDefault()));
                }
            }
        }   
    };


    @Override
    public void onListItemClick(ListView l, View v, int position, long id) 
    {
        File file;
        //user clicked one path back
        if (listItem.get(position) == "../") 
            {
                file = new File(g_startPath);
                g_startPath = file.getParent();
            }
        else 
        //if user clicked to a picture or 
        //to a new folder (>> getDir has to be called with the new path)
        //      >>file has to be set to path and position   
        {file = new File(g_startPath + '/' + listItem.get(position));}

            Bitmap myBitmap;
            //user clicked only to an image >> the image has to be shown in the image view - nothing else
            if (file.isFile())
            {
                //if(file.canRead()){
                myBitmap = BitmapFactory.decodeFile(file.getPath());
                image.setImageBitmap(myBitmap);
                image.setVisibility(View.VISIBLE);
                //}
            }else   
            {
                if (file.isDirectory())
                {
                        image.setVisibility(View.INVISIBLE);
                        if (listItem.get(position) != "../"){g_startPath = file.getPath();}
                        getDir(g_startPath);
                }
            }   
    }
} 

ImgFrag:

代码语言:javascript
复制
package com.example.wbslideshow;

public class ImgFrag extends Fragment{
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View myImgView = inflater.inflate(R.layout.img_frag, container, false);
        return myImgView;
    }

}

布局activity_main.xml

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DA8306"
    android:orientation="vertical" 
    android:id="@+id/mycontainer">

</LinearLayout

main_frag.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FragMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/startHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/startHeader" />

    <EditText
        android:id="@+id/inputSearchPath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/startInputHint" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/startBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/startBtn" />

</LinearLayout>

layout/list_Frame.xml:

代码语言:javascript
复制
<?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:id="@+id/listFrag"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/startpath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#C27302"
        android:height="40dp"
        android:maxLines="1"
        android:scrollHorizontally="false"
        android:text="@string/list_header"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="312dp"
        android:layout_gravity="fill"
        android:background="#B012EB" />

    <TextView
        android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

        <FrameLayout
        android:id="@+id/myImgContainer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
         />

</LinearLayout>

layout-land/list_fra.xml:

代码语言:javascript
复制
<?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:baselineAligned="false"
    android:orientation="horizontal" >

    <!-- static fragment for the left pane -->
    <fragment
        android:name="com.example.wbslideshow.ListFrag"
        android:id="@+id/listFrag"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/myImgContainer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="fill_parent">
    </FrameLayout>

</LinearLayout>

img_frag.xml:

代码语言:javascript
复制
<?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="horizontal" 
    android:id="@+id/imgFrag">

        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />


</LinearLayout>

代码可能并不完美,但这并不是问题所在。在纵向模式下,应用程序正在工作。只有在景观模式下,我才会遇到加载列表框架的问题。下面也是当前的logcat:

10-11 13:20:12.563: W/dalvikvm(5158):threadid=1:线程退出带有未明异常(group=0x414539a8) 10-11 13:20:12.583: E/AndroidRuntime(5158):致命异常:主10-11 13:20:12.583: E/AndroidRuntime(5158):java.lang.IllegalArgumentException:未找到id 0x7f060041 (com.ple.wbsndeshowow:id/myImgContainer)的片段ImgFrag{417af140 #1 id=0x7f060041} 10-11 13:20:12.583: E/AndroidRuntime(5158)::android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877) 10-11 13:20:12.583: E/AndroidRuntime(5158):android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 10-11 13:12.583: E/AndroidRuntime(5158):android.app.BackStackRecord.run(BackStackRecord.java:694) 10-11 13:20:12.583: E/AndroidRuntime(5158):android.app.FragmentManagerImpl.execPendingActions(( android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 10-11 13: 12.583: E/AndroidRuntime(5158):android.os.Handler.handleCallback(Handler.java:725) 10-11 13:20:12.583: 12.583: E/AndroidRuntime(5158):android.os.Handler.dispatchMessage(Handler.java:92) 10-11 13:20:12.583: E/AndroidRuntime(5158):android.os.Looper.loop(Looper.java:153) 10-11 13:12.583: E/AndroidRuntime(5158):at Looper.java:153 10-11 13:12.583: E/AndroidRuntime(5158):at java.lang.reflect.Method.invokeNative(原生方法) 10-11 13:20:12.583: E/AndroidRuntime(5158):java.lang.reflect.Method.invoke(Method.java:511) 10-11 13:20:12.583: E/AndroidRuntime(5158):com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 10-11 13:20:12.583: E/AndroidRuntime(5158):com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 10-11 13:20:12.583: E/AndroidRuntime(5158):dalvik.system.NativeStart.main(原生方法)

我能看出“找不到vie”有问题.“但我不知道为什么。对我来说,来自res/ layout -land的布局文件似乎没有加载在横向模式中,因此IMgFrag的IMgFrag遇到了一个问题。但是为什么呢?

安德烈亚斯

EN

回答 2

Stack Overflow用户

发布于 2014-10-11 06:18:49

ID myImgContainer在布局layout-land/list_frag.xml中可用,在调用单击事件时,屏幕上只会显示main_frag,所以MainActivity用户R.id.mycontainer本身中的R.id.myImgContainer代替了R.id.myImgContainer

票数 0
EN

Stack Overflow用户

发布于 2014-10-11 06:21:22

您不应该在布局-land/list_fra.xml文件中使用类似的fragment标记。

list_frag.xml是ListFrag类的布局。您尝试将ListFrag放入它自己的布局文件中。这太不对了。

,您想要在纵向模式下的列表下面的ImageView,以及在横向模式下在列表右侧的ImageView (就像两个面板)吗?

如果你想做的话。只需编写布局-land/list_fra.xml,如下所示:

代码语言:javascript
复制
<?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="horizontal">

    <LinearLayout
        android:id="@+id/listFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/startpath"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:height="40dp"
            android:background="#C27302"
            android:maxLines="1"
            android:scrollHorizontally="false"
            android:text="@string/list_header"
            android:textSize="16sp"
            android:textStyle="bold" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill"
            android:background="#B012EB" />

        <TextView
            android:id="@+id/empty"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>


    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
    </FrameLayout>

</LinearLayout>

您不需要myImgContainer,所以将其从布局/list_Frame.xml中删除:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listFrag"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/startpath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:height="40dp"
        android:background="#C27302"
        android:maxLines="1"
        android:scrollHorizontally="false"
        android:text="@string/list_header"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="312dp"
        android:layout_gravity="fill"
        android:background="#B012EB" />

    <TextView
        android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

</LinearLayout>

更改MainActivity中的MainActivity方法,只替换为ListFrag。该平台将负责使用哪个xml文件作为ListFrag的布局。

代码语言:javascript
复制
public void onstartFragBtnClicked(String root) 
{
    ListFrag newListFrag = new ListFrag();
    Bundle args = new Bundle();
    args.putString(ListFrag.FRAG_MESSAGE_DEF_Input, root);
    newListFrag.setArguments(args);

    //change the fragments dynamically
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.mycontainer, newListFrag);
    transaction.addToBackStack(null);
    transaction.commit(); 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26311622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档