在上一篇博客,Android-fragment简介-fragment的简单使用,介绍了简单的使用;

 

这篇博客主要讲解,在fragment做处理事情(Activity的事情)

 

Activity

package liudeli.activity.fragment;

import android.app.Activity;
import android.os.Bundle;

import liudeli.activity.R;

public class MyTestFragmentActivity2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_fragment2);
    }
}

 

Activity 的 布局 activity_test_fragment2

<?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">

    <!--
        android:id="@+id/fragment" 必须要指定好ID,否则运行会报错
        class="liudeli.activity.fragment.MyFragment" 必须要指定class,否则无效果
    -->
    <fragment
        android:id="@+id/fragment"
        class="liudeli.activity.fragment.MyFragment2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

 

Activity 的 布局 <fragment class 引用的  MyFragment2

package liudeli.activity.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import liudeli.activity.R;

public class MyFragment2 extends Fragment {

    /**
     * 创建View
     * @param inflater 布局加载器
     * @param container
     * @param savedInstanceState 临时保存数据用的Bundle 和 Activity-onCreate方法一样
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        // 使用布局加载器加载
        View view = inflater.inflate(R.layout.fragment_layout, null);
        return view;
    }

    private ListView listView;

    /**
     * 此方法代表View创建已经完成✅
     * @param view 上面方法创建View成功后返回过来的View
     * @param savedInstanceState  临时保存数据用的Bundle 和 Activity-onCreate方法一样
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        listView = view.findViewById(R.id.listview);

        String[] strings = new String[]{"测试数据一", "测试数据二", "测试数据三"};

        // 初始化适配器数据
        final ListAdapter listAdapter = new ArrayAdapter(getActivity(), // 在Fragment不能使用this
                                                           android.R.layout.simple_list_item_1,
                                                           android.R.id.text1,
                                                           strings);
        listView.setAdapter(listAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String itmeValue = (String) listAdapter.getItem(position);
                Toast.makeText(getActivity(), itmeValue, Toast.LENGTH_SHORT).show();
            }

        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "长按Item条目", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

    }
}

 

MyFragment2 的 布局文件 fragment_layout

此属性可以解决,ListView Item 长按无效的问题:android:descendantFocusability="blocksDescendants"

<?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">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        />

</LinearLayout>

 

效果: