1. 程式人生 > >Fragment實現左右新聞列表和詳情

Fragment實現左右新聞列表和詳情

Fragment的使用,類似於新聞詳情頁面。點選左邊fragment的listview,右邊的fragment顯示當前選中的內容。在這裡實現主要使用監聽。
這裡寫圖片描述
在一些情況下,你可能需要fragment與activity共享事件。這樣做的一個好方法是在fragment內部定義一個回撥介面,並需要宿主activity實現它。當activity通過介面接收到回撥時,可以在必要時與佈局中的其它fragment共享資訊。

舉個例子,如果新聞應用的actvity中有兩個fragment——一個顯示文章列表(fragment A),另一個顯示一篇文章(fragment B)——然後fragment A 必須要告訴activity列表項何時被選種,這樣,activity可以通知fragment B顯示這篇文章。這種情況下,在fragment A內部宣告介面

首先:

public interface MyListener{
        void onListener(String str);
    }

然後:
fragment的宿主activity實現了OnArticleSelectedListener介面,並且重寫onArticleSelected()以通知fragment B來自於fragment A的事件。為了確保宿主activity實現了這個介面,fragment A的onAttach()回撥函式(當新增fragment到activity中時系統會呼叫它)通過作為引數傳入onAttach()的activity的型別轉換來例項化一個MyListener例項。

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

最後在點選listview的條目時設定監聽事件:

 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String str= (String) arrayList.get(position);
        myListener.onListener(str);
    }

1、在activity中程式碼

package com.example.administrator.myapplication;


import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import fragment.MyLeftFragment;
import fragment.MyRightFragment;


public class MainActivity extends AppCompatActivity implements MyLeftFragment.MyListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public void onListener(String str) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        MyRightFragment myRightFragment= (MyRightFragment) fragmentManager.findFragmentById(R.id.fragment_right);
        myRightFragment.setText(str);
    }
}

2、左邊列表程式碼

public class MyLeftFragment extends  Fragment implements AdapterView.OnItemClickListener {
    private ListView listView;

    private View view;

    private ArrayList arrayList;

    private MyAdapter adapter;

    public MyListener myListener;

    //定義介面,為了實現監聽;
    public interface MyListener{
        void onListener(String str);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String str= (String) arrayList.get(position);
        //把監聽設定給點選事件
        myListener.onListener(str);
    }
    //一定要重寫這個方法,為了保證activity實現這個介面,通過作為引數onAttch()型別轉換成OnListener()的一個例項
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try{
         myListener= (MyListener) activity;
        }catch (ClassCastException e){
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_left,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView= (ListView) view.findViewById(R.id.listview);
        arrayList=new ArrayList();
        init();
        adapter=new MyAdapter(getActivity(),arrayList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    private void init() {
        for(int i=0;i<20;i++){
            arrayList.add("我是item"+i);
        }

    }
}

3、右邊詳情程式碼

package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.myapplication.R;

/**
 * Created by Administrator on 2016/4/19.
 */
public class MyRightFragment extends Fragment {
    View view;
    TextView textView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_right,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView= (TextView) view.findViewById(R.id.text);
    }
    public void setText(String str){
      textView.setText(str);
    }
}