The use of Android's ListFragment and Fragment

Project directory:
Insert picture description here
CenterListFragment

package com.example.test1;

import java.util.ArrayList;
import java.util.List;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class CenterListFragment extends ListFragment {
    private ArrayAdapter<String> adapter;
    private List<String> data;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data = new ArrayList<String>();
        for (int i = 0; i < 11; i++) {
            data.add("data" + i);
        }
        manager = getFragmentManager();
        adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, data);
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String str = adapter.getItem(position);
        transaction = manager.beginTransaction();

        RightFragment rFragment = new RightFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", str);
        rFragment.setArguments(bundle);
        transaction.replace(R.id.right, rFragment, "detail");
        transaction.commit();
        Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }
}

MainActivity

package com.example.test1;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button button;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager = getFragmentManager();
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                transaction = manager.beginTransaction();
                CenterListFragment centerListFragment = new CenterListFragment();
                transaction.add(R.id.center, centerListFragment, "center");
                transaction.commit();
            }
        });
    }

}

RightFragment

package com.example.test1;

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 RightFragment extends Fragment {
    private TextView textView;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.detail, null);
        textView = (TextView) view.findViewById(R.id.textView);

        Bundle bundle = getArguments();
        String str = bundle.getString("id");
        textView.setText(str);
        return view;
    }

}

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_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="显示列表" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/center"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

detail.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" >


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"/>


</LinearLayout>

Demo:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43873198/article/details/108810998