java – 片段中的super.onCreateView

前端之家收集整理的这篇文章主要介绍了java – 片段中的super.onCreateView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我完成了针对 Android手持系统的移动应用编程的Coursera课程.
课程中的一个示例代码告诉我们如何对我们进行分片.基本上它的作用是将屏幕分成两个片段,一个用于书名,一个用于书中的引用.如果用户单击左侧片段中的标题,则从该书中关联的引用将显示在右侧的片段中.

这是mainActivity的代码

package course.examples.Fragments.StaticLayout;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import course.examples.Fragments.StaticLayout.TitlesFragment.ListSelectionListener;

public class QuoteViewerActivity extends Activity implements
        ListSelectionListener {

    public static String[] mTitleArray;
    public static String[] mQuoteArray;
    private QuotesFragment mDetailsFragment;

    private static final String TAG = "QuoteViewerActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitleArray = getResources().getStringArray(R.array.Titles);    
        mQuoteArray = getResources().getStringArray(R.array.Quotes);

        setContentView(R.layout.main);

        mDetailsFragment = (QuotesFragment) getFragmentManager()
                .findFragmentById(R.id.details);        
    }

    @Override
    public void onListSelection(int index) {
        if (mDetailsFragment.getShownIndex() != index) {        
            mDetailsFragment.showQuoteAtIndex(index);
        }
    }

这是Quote片段的代码

package course.examples.Fragments.StaticLayout;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class QuotesFragment extends Fragment {

    private TextView mQuoteView = null;
    private int mCurrIdx = -1;
    private int mQuoteArrayLen;

    private static final String TAG = "QuotesFragment";

    public int getShownIndex() {
        return mCurrIdx;
    }

    public void showQuoteAtIndex(int newIndex) {
        if (newIndex < 0 || newIndex >= mQuoteArrayLen)
            return;
        mCurrIdx = newIndex;
        mQuoteView.setText(QuoteViewerActivity.mQuoteArray[mCurrIdx]);
    }
@Override
public void onAttach(Activity activity) {
    Log.i(TAG,getClass().getSimpleName() + ":entered onAttach()");
    super.onAttach(activity);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG,getClass().getSimpleName() + ":entered onCreate()");
    super.onCreate(savedInstanceState);
}

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

@Override
public void onActivityCreated(Bundle savedInstanceState) {      
    super.onActivityCreated(savedInstanceState);
    mQuoteView = (TextView) getActivity().findViewById(R.id.quoteView);
    mQuoteArrayLen = QuoteViewerActivity.mQuoteArray.length;
}

这是标题片段的代码

package course.examples.Fragments.StaticLayout;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TitlesFragment extends ListFragment {
    private ListSelectionListener mListener = null;
    private static final String TAG = "TitlesFragment";

    public interface ListSelectionListener {
        public void onListSelection(int index);     
    }

    @Override
    public void onListItemClick(ListView l,View v,int pos,long id) {
        getListView().setItemChecked(pos,true);
        mListener.onListSelection(pos);
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG,getClass().getSimpleName() + ":entered onCreate()");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
        Log.i(TAG,getClass().getSimpleName() + ":entered onCreate()");

        **return super.onCreateView(inflater,savedInstanceState);**

    }

    @Override
    public void onActivityCreated(Bundle savedState) {
        super.onActivityCreated(savedState);

        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setListAdapter(new ArrayAdapter<String>(getActivity(),R.layout.title_item,QuoteViewerActivity.mTitleArray));
    }

MainActivity 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:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/titles"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="course.examples.Fragments.StaticLayout.TitlesFragment" />

    <fragment
        android:id="@+id/details"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="2"
        class="course.examples.Fragments.StaticLayout.QuotesFragment" />

</LinearLayout>

引用片段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/quoteView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dip"
        android:textSize="32sp" >
    </TextView>
</LinearLayout>

标题片段xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:orientation="vertical"
    android:padding="5dip"
    android:textSize="32sp" >

</TextView>

我的问题是为什么onCreateView下的方法在Quote片段和Title片段下有所不同?
QuoteFragment返回inflater.inflate(R.layout.quote_fragment,false);
TitleFragment返回super.onCreateView(inflater,savedInstanceState);

解决方法

因为QuotesFragment扩展了默认情况下没有布局的Fragment,并且用户必须膨胀并返回自己的布局.这就是Fragment的onCreateView方法的样子:
public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
    return null;
}

TitlesFragment扩展了ListFragment,它默认包含一个布局,包含项目的ListView,列表为空时的标签的TextView和ProgressBar.在这种情况下,用户不必返回他自己的视图并且可以返回超级调用获得的对象. ListFragment的onCreateView:

public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
    return inflater.inflate(com.android.internal.R.layout.list_content,false);
}
原文链接:https://www.f2er.com/java/130235.html

猜你在找的Java相关文章