android----fragment学习中的listFragment

来源:互联网 发布:最全家庭网络个人投资 编辑:程序博客网 时间:2024/04/30 16:03

对于listFragment,相当于listView。它是Fragment的一个子类,拥有Fragment的所有属性以及方法,同时拥有自己的方法。比如onListItemClick等等。

同时强调一下,一般Fragment与fragment之间传值用的是getArgments和setArgemnts方法,而activity与Fragment用的是方法的回调,当然这是个人观点

好了,上代码

MainActivity.java

package com.example.android_fragment_listfragment1;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button button1;private FragmentManager manager;private FragmentTransaction transaction;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1 = (Button) this.findViewById(R.id.button1);manager = getFragmentManager();button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {transaction = manager.beginTransaction();ArticleListFragment fragment = new ArticleListFragment();transaction.add(R.id.center, fragment, "center");transaction.commit();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
ArticleListFragment.java

package com.example.android_fragment_listfragment1;import java.util.ArrayList;import java.util.List;import com.example.android_fragment_listfragment1.DetailFragment.CallBack;import android.app.FragmentManager;import android.app.FragmentTransaction;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.ListView;import android.widget.TextView;public class ArticleListFragment extends ListFragment {private FragmentManager manager;private FragmentTransaction transaction;private ArrayAdapter<String> adapter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);List<String> data = new ArrayList<String>();for (int i=0; i<30; i++) {data.add("listFragment"+i);}adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data);setListAdapter(adapter);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// TODO Auto-generated method stubreturn super.onCreateView(inflater, container, savedInstanceState);}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();}@Overridepublic void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);manager = getFragmentManager();transaction = manager.beginTransaction();String result = adapter.getItem(position);DetailFragment detailFragment = new DetailFragment();Bundle bundle = new Bundle();bundle.putString("sun", result);detailFragment.setArguments(bundle);transaction.replace(R.id.right, detailFragment, "detailFragment");transaction.addToBackStack("detailFragment");transaction.commit();}}

DetailFragment.java
package com.example.android_fragment_listfragment1;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class DetailFragment extends Fragment {private TextView textView1;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.detail, null);textView1 = (TextView) view.findViewById(R.id.textView1);Bundle bundle = getArguments();textView1.setText(""+bundle.getString("sun"));return view;}@Overridepublic void onPause() {super.onPause();}public void loadData(final CallBack callBack) {callBack.getResult(textView1);}public interface CallBack {public void getResult(TextView textView1);}}

activity_main.xml

<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="horizontal"    tools:context=".MainActivity" >        <LinearLayout        android:id="@+id/left"        android:layout_weight="1"        android:background="#ccc"        android:layout_width="0dp"        android:layout_height="match_parent"        android:orientation="vertical"        >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="show" />            </LinearLayout>    <LinearLayout        android:id="@+id/center"        android:layout_weight="2"        android:background="#cccfff"        android:layout_width="0dp"        android:layout_height="match_parent"        android:orientation="vertical"        >            </LinearLayout>    <LinearLayout        android:id="@+id/right"        android:background="#cccddd"        android:layout_weight="2"        android:layout_width="0dp"        android:layout_height="match_parent"        android:orientation="vertical"        >            </LinearLayout></LinearLayout>

detail.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/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="30sp"         /></LinearLayout>



1 0
原创粉丝点击