«

Fragment详解-android学习之旅(四十八)

时间:2024-3-2 19:54     作者:韩俊     分类: Android


Fragment的设计哲学

Fragment的继承体系

Fragment的开发

大部分都会继承如下的三个方法

Fragment与Activity的通信


Fragment与Activity交互信息


Fragment的管理与Fragment事务

开发兼顾分辨率的应用


开发显示图书详情的Fragment

图书想详情Fragment类机器布局

package peng.liu.test;

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

/**
 * Created by lplp on 2015/6/26.
 */
public class BookDetailFragment extends Fragment {
    public static final String ITEM_ID = "item_id";
    BookContent.Book book;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments().containsKey(ITEM_ID)){
            book = BookContent.ITEM_MAP.get(getArguments().getInt(ITEM_ID));

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View bookView = inflater.inflate(R.layout.cell,container,false);
        if (book != null){
            ((TextView)bookView.findViewById(R.id.title)).setText(book.title);
            ((TextView)bookView.findViewById(R.id.desc)).setText(book.desc);
        }
        return bookView;

    }
}
<?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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:padding="16dp"
        style="?android:attr/textAppearanceLarge"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/desc"
        android:padding="16dp"
        style="?android:attr/textAppearanceLarge"/>
</LinearLayout>

图书列表类斐然关门他,因为继承了ListFragment,所以不需要布局文件,setLsitAdapetr就可以了

package peng.liu.test;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by lplp on 2015/6/26.
 */
public class BookListFragment extends ListFragment {
    private CallBacks mCallBacks;
    public interface CallBacks{
        public void onItemSelected(Integer id);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<BookContent.Book>(getActivity(),android.R.layout.simple_list_item_activated_1,android.R.id.text1,BookContent.ITEMS));
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof CallBacks)){
            throw new IllegalStateException("BookListFragment所在的Activity必须实现CallBacks接口");
        }
        mCallBacks = (CallBacks)activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallBacks = null;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        mCallBacks.onItemSelected(BookContent.ITEMS.get(position).id);
    }
    public void setActivateOnItemClick(boolean activateOnItemClick){
        getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE:ListView.CHOICE_MODE_NONE);
    }
}

主Activity类及其布局

package peng.liu.test;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends Activity implements BookListFragment.CallBacks{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void onItemSelected(Integer id) {
        Bundle bundle = new Bundle();
        bundle.putInt(BookDetailFragment.ITEM_ID,id);
        BookDetailFragment fragment = new BookDetailFragment();
        fragment.setArguments(bundle);
        getFragmentManager().beginTransaction().replace(R.id.bookContainer,fragment).commit();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    >
    <fragment
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:name="peng.liu.test.BookDetailFragment"
        android:id="@+id/bookDetail"
        android:layout_weight="1"/>
    <FrameLayout
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:id="@+id/bookContainer"
        android:layout_weight="3"/>
</LinearLayout>
        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐