Android 什么是片段?如何将frgament添加到现有活动?如何修改片段的布局

Android 什么是片段?如何将frgament添加到现有活动?如何修改片段的布局,android,android-fragments,Android,Android Fragments,我是android新手,我想创建一个包含listview的片段,我想将该片段添加到我的主要活动中。我正在尝试,但我无法将其添加到我的活动中,也无法为其定义我自己的布局。因此,请帮助我。 提前谢谢 这是我的MainActivity.java package com.example.sample; import android.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundl

我是android新手,我想创建一个包含listview的片段,我想将该片段添加到我的主要活动中。我正在尝试,但我无法将其添加到我的活动中,也无法为其定义我自己的布局。因此,请帮助我。 提前谢谢

这是我的MainActivity.java

package com.example.sample;

import android.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


String[] codeLearnChapters = new String[] { "Android Introduction","Android Setup/Installation","Android Hello World","Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"};

ArrayAdapter<String> codeLearnArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, codeLearnChapters);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView codeLearnLessons = (ListView) findViewById(R.id.idListView1);
    setContentView(R.layout.activity_main);
    codeLearnLessons.setAdapter(codeLearnArrayAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
它的xml即act_fragment.xml是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/idListView1">
</ListView>

</RelativeLayout>

片段表示活动中的行为或用户界面的一部分。您可以在单个活动中组合多个片段,以构建多窗格UI,并在多个活动中重用片段。您可以将片段视为活动的模块化部分,它有自己的生命周期,接收自己的输入事件,并且可以在活动运行时添加或删除(有点像可以在不同活动中重用的“子活动”)

用我的话说,片段是一个子活动


您可以阅读更多关于片段是什么的信息

还有一个你可以使用的示例(这将对你有所帮助,因为你将学到一些新的东西)。样本是

您还可以在youtube上查找它(一旦您阅读了它并理解了片段是什么)。还有一个视频教程,让你开始学习。只需搜索“Android Fragment Lifecycle第1部分:Android应用程序开发教程[HD 1080p]”-抱歉,Stackoverflow不允许我添加两个以上的链接


他们有五个教程,我会推荐它,因为它将帮助你自己更好地开始

您可以阅读更多关于片段的内容 代码:

公共类Tab2Fragment扩展片段{
编辑文本fromdate;
编辑文本todate;
//列表视图
私有ListView lv;
//列表视图适配器
阵列适配器;
//搜索编辑文本
编辑文本输入搜索;
//Listview的ArrayList
ArrayList产品列表;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图V=充气机。充气(R.layout.tab2_视图,容器,假);
EditText fromdate=(EditText)V.findViewById(R.id.fromdate);
EditText todate=(EditText)V.findViewById(R.id.todate);
fromdate.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
} 
}); 
//列表视图数据
字符串产品[]={“戴尔Inspiron”、“HTC One X”、“HTC Wildfire S”、“HTC Sense”、“HTC Sensation XE”,
“iPhone 4S”、“三星Galaxy Note 800”,
“三星Galaxy S3”、“MacBook Air”、“MacMini”、“MacBook Pro”};
lv=(列表视图)V.findViewById(R.id.list\u视图);
//向listview添加项目
适配器=新的ArrayAdapter(getActivity(),R.layout.list_项,R.id.product_名称,产品);
低压设置适配器(适配器);
返回V;
} 
} 
希望此链接对您有所帮助

把这个放在你的活动课上 FragmentManager FragmentManager=getFragmentManager()

FragmentTransaction FragmentTransaction=fragmentManager.beginTransaction()

ExampleFragment=新的ExampleFragment()

fragmentTransaction.add(R.id.fragment\u容器,fragment)

fragmentTransaction.commit()

并将其放置在activitylayout代码的下面
xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/fragment_container"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:ignore="MergeRootFrame" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/idListView1">
</ListView>

</RelativeLayout>
public class Tab2Fragment extends Fragment {


    EditText fromdate;
    EditText todate;

    // List view 
    private ListView lv;

    // Listview Adapter 
    ArrayAdapter<String> adapter;

    // Search EditText 
    EditText inputSearch;


    // ArrayList for Listview 
    ArrayList<HashMap<String, String>> productList;

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View V = inflater.inflate(R.layout.tab2_view, container, false);


        EditText fromdate=(EditText)V.findViewById(R.id.fromdate);
        EditText todate=(EditText)V.findViewById(R.id.todate);

        fromdate.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {


            } 
        }); 

        // Listview Data 
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                "iPhone 4S", "Samsung Galaxy Note 800", 
                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"}; 

        lv = (ListView) V.findViewById(R.id.list_view);


        // Adding items to listview 
        adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);


        return V;
    } 


} 
xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/fragment_container"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:ignore="MergeRootFrame" />