fragment使用的简单示例
来源:互联网 发布:涤纶 锦纶 外套 知乎 编辑:程序博客网 时间:2024/05/22 01:50
实现如下效果
activity_main.xml
<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/articleFragment" android:name="com.example.fragmentdemo.IndexFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/contentFragment" android:name="com.example.fragmentdemo.ContentFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /></LinearLayout>
content_fragment.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.fragmentdemo.MainActivity" > <TextView android:id="@+id/contentTxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="50sp" /></RelativeLayout>
index_fragment.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.fragmentdemo.MainActivity" > <ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/textView" android:textSize="50sp" android:layout_width="fill_parent" android:layout_height="fill_parent" ></TextView>
ContentFragment .ja
package com.example.fragmentdemo;import android.annotation.SuppressLint;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;@SuppressLint("NewApi")public class ContentFragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub// 第一个是resource ID,指明了当前的Fragment对应的资源文件;// 第二个参数是父容器控件;// 第三个布尔值参数表明是否连接该布局和其父容器控件,在这里的情况设置为false,// 因为系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View// Group。return inflater.inflate(R.layout.content_fragment, container, false);}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();}}
IndexFragment.java
package com.example.fragmentdemo;import android.annotation.SuppressLint;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;@SuppressLint("NewApi")public class IndexFragment extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub// 第一个是resource ID,指明了当前的Fragment对应的资源文件;// 第二个参数是父容器控件;// 第三个布尔值参数表明是否连接该布局和其父容器控件,在这里的情况设置为false,// 因为系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View// Group。return inflater.inflate(R.layout.index_fragment, container, false);}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();}}
MainActivity.java
package com.example.fragmentdemo;import java.util.ArrayList;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends FragmentActivity {private ListView listView;private TextView contentTxt;private ArrayList<String> content;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView) findViewById(R.id.listView);contentTxt = (TextView) findViewById(R.id.contentTxt);ArrayList<String> name = new ArrayList<String>();for (int i = 0; i < 10; i++) {name.add(i + "");}content = new ArrayList<String>();for (int i = 0; i < 10; i++) {content.add("" + i);}ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, name);listView.setAdapter(adapter);listView.setOnItemClickListener(ListViewItemClickListener);}OnItemClickListener ListViewItemClickListener = new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {// TODO Auto-generated method stubcontentTxt.setText("点击的数字是:" + content.get(position));}};}
0 0
- fragment使用的简单示例
- Fragment使用简单示例
- Fragment的简单使用
- Fragment的简单使用
- Fragment的简单使用
- Fragment 的简单使用
- 简单的Fragment使用
- Fragment的简单使用
- Fragment的简单使用
- Fragment简单示例
- Fragment简单示例
- android ---Fragment简单示例
- Android中Fragment的使用总结示例
- android Fragment的简单使用
- Android fragment的简单使用
- android Fragment 简单的使用
- Android Fragment的简单使用
- android fragment的简单使用
- 关于Android Button 与 EditText ImageView 的点击问题
- 友推SDK微信分享问题
- GPUImage简单说明
- 表空间相关sql
- android PreferenceActivity详解
- fragment使用的简单示例
- APP NAME 国际化
- 解决IE8 内置JSON.stringify,中文变unicode的问题
- 基于xmpp openfire smack开发之Android客户端开发[3]
- 浅析新闻推荐及个性化推荐的领域相关性
- C++数据结构之字符串的BF算法
- Oracle 删除重复数据只留一条
- hadoop-hive-关于IN与NOT IN
- LCA最近公共祖先