如何建fragment和在fragment中添加ListView,并设置ListView中条目的值

最后界面如下:

如何建fragment和在fragment中添加ListView,并设置ListView中条目的值

制作思路:首先在一个Activity中做出左右fragment(碎片的意思),然后再在左边的fragment中加入ListView控件,再用编程的方法给ListView控件设置条目,即“新闻1,新闻2,新闻3,....”,注意给ListView设置条目,不能手动修改什么设计输入进去,只能用程序的方式写进去。 具体看后面的代码:

一、在Activity中做出左右fragment

第一步:新建一个工程。
第二步:新建一个LeftFragment 后,把它java文件中其它自动产生的代码都清除,只留下onCreateView()函数。不清除最后生成APP后会闪退。
public class LeftFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_left, container, false);
    }
}
第三步:新建一个RightFragment后,把它java文件中其它自动产生的代码都清除,只留下onCreateView()函数。不清除最后生成APP后会闪退。

public class RightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_right, container, false);
    }
}

第四步:修改fragment_left.xml中的代码为如下代码,它原来是constraint布局,现在要改为线性布局, 修改原因是constraint布局很难做。

<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
    tools:context=".LeftFragment">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button" />

</LinearLayout>

第五步:修改fragment_right.xml中的代码为如下代码,它原来是constraint布局,现在要改为线性布局, 修改原因是constraint布局很难做。
<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
    android:background="#00ff00"
    tools:context=".RightFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is right fragment"
        />

</LinearLayout>

第六步: 修改主界面的布局为线性布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity"
    >

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.jintingbo.myapplication16.LeftFragment"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_left" />
    <fragment
        android:id="@+id/right_fragment"
        android:name="com.jintingbo.myapplication16.RightFragment"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_right" />

</LinearLayout>

第七步:运行后就可以看到左右界面的图了; 

二、在上面的基础上,为fragment_left.xml添加ListView控件。

第一步:打开fragment_left.xml文件,并转到Design(设计)模式, 从Legacy中拉一个ListView控件,会自动生成代码如下:

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

第二步:在宿主Activity中写代码设置碎片中的条目

要为ListView控件添加条目,用手动修的方法办不到,这与其它语言中的ListView不一样,只能通过代码的方式来解决,并且这个代码必须写在它的宿主Activity中,不能写在fragment_left.xml对应的LeftFragment.java文件中,具体代码如下:

public class MainActivity extends AppCompatActivity {
    String[]  nameList ={"新闻1","新闻2","新闻3","新闻4","新闻5","新闻6"};
    private ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview =(ListView)findViewById(R.id.list_view); //碎片在宿主中的地位也就相当于是一个控件,所以可以用findView()
        addString();
    }
    private void addString(){
        ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nameList);
        listview.setAdapter(adapter);
    }

}
从上面的代码可以看到,要把条目写入到ListView中,必须通过ArrayAdapter才可以实现。

三、补充安卓系统自带的List View资料

安卓系统自带的List View里,有simple_list_item_1、simple_list_item_2、two_line_list_item等。以下对这些布局进行简要介绍:
  1、simple_list_item_1
        (单行显示)此布局显示最为简单,其中只有一个TextView,id为:
        android:id="@android:id/text1" 
        显示样式为:
        如何建fragment和在fragment中添加ListView,并设置ListView中条目的值
  2、simple_list_item_2和two_line_list_item
        (双行显示)每一个item包含两个TextView,id为
        android:id="@android:id/text1" 
        android:id="@android:id/text2" 
        显示样式为:
如何建fragment和在fragment中添加ListView,并设置ListView中条目的值

3、two_line_list_item
        (双行显示)每一个item包含两个TextView,id为
        android:id="@android:id/text1" 
        android:id="@android:id/text2" 
        显示样式为:
        如何建fragment和在fragment中添加ListView,并设置ListView中条目的值
  4、simple_list_item_checked:单选check;
     simple_list_item_multiple_choice:多选;
     simple_list_item_single_choice:单选项

setListAdapter(new SimpleAdapter(this,data,android.R.layout.simple_list_item_multiple_choice, 
        new String[]{"item1"},            //每行显示一组姓名  
        new int[]{android.R.id.text1}   //名字在text1上显示  
));  

设置ListView选择模式

/* 
 * CHOICE_MODE_SINGLE    单选 
 * CHOICE_MODE_MULTIPLE  多选
 * CHOICE_MODE_NONE      缺省 
 */  
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);