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
- android----fragment学习中的listFragment
- [Android]Fragment应用/ListFragment/DialogFragment
- Fragment学习之ListFragment的使用
- Fragment学习之ListFragment及自定义适配器
- Android系列之Fragment----ListFragment的使用
- Android Fragment的使用 八 ListFragment
- Android Fragment学习笔记(2) ----使用ListFragment显示列表(上)
- 一个Android练手工程(ViewPager、Fragment、ListFragment、sqlite)
- Android之Fragment-ListFragment的实践(三)
- Android Fragmnet-Fragment数据交换以及ListFragment的使用
- Android ListFragment
- android Listfragment
- Android ListFragment
- Fragment ListFragment相关API
- 处女男学Android(十)---Fragment完结篇之Fragment通信和ListFragment
- Android UI开发第十七篇——Android Fragment实例(ListFragment)
- android Fragment 实例 Listfragment listiew 下拉刷新 上拉加载 横向滑动事件监听
- 【Android 开发教程】ListFragment
- [Android]当Activity启动模式为singleTask时的生命周期
- android studio添加butterknif插件
- [1] Window PowerShell DSC 学习系列----DSC的定义和2种架构模式
- java spring中对properties属性文件加密及其解密
- HTML转义字符汇总
- android----fragment学习中的listFragment
- 找数
- Rails配置项force_ssl与hsts
- 基于WWW的get请求Demo
- IE css3常用属性兼容性
- HTML5页面开发的基础性模板
- 328. Odd Even Linked List
- 重构自己
- 1078. Hashing