使用片段在Android中创建抽认卡

时间:2018-12-13 02:55:24

标签: java android android-fragments

我正在尝试制作一个简单的闪存卡程序,用户可以在其中选择是否要加,减或乘的问题,然后使用单独的片段来显示闪存卡。当我运行程序时,它似乎并没有进入第一个片段,因为它一直在加载到模拟器中。我究竟做错了什么?

主要活动

    package com.example.johnt.hw4;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainAvtivity";

    private SectionsStatePageAdapter mSectionStatePagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: Started.");

        mSectionStatePagerAdapter = new SectionsStatePageAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById( R.id.list_fragment_port );

        // setup the page
        setupViewPager(mViewPager);
        setViewPager(0);




        /*
        if (findViewById( R.id.list_fragment_port ) != null ) {
            // create the fragment
            ListFragment listFragment = new ListFragment();
            // replace anything that was there before
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            // replace any existing fragment
            transaction.add( R.id.list_fragment_port, listFragment );
            transaction.commit();
        }
        */
    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsStatePageAdapter adapter = new SectionsStatePageAdapter(getSupportFragmentManager());
        adapter.addFragment(new ListFragment(), "ListFragment");
        adapter.addFragment(new DataFragment(), "DataFragment");
        viewPager.setAdapter(adapter);
    }

    public void setViewPager(int fragmentNumber) {
        mViewPager.setCurrentItem(fragmentNumber);
    }


    public void onListFragmentInteraction(int operatorIndex) {
        //create a new data fragment with appropriate information
        DataFragment dataFragment = DataFragment.newInstance( operatorIndex );

        // replace anything that was there before
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // replace any existing fragment
        transaction.replace( R.id.list_fragment_port, dataFragment );
        // activate the back button
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

SectionStatePageAdapter

package com.example.johnt.hw4;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

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

public class SectionsStatePageAdapter extends FragmentStatePagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public SectionsStatePageAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }
}

ListFragment

package com.example.johnt.hw4;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class ListFragment extends Fragment {
    private static final String TAG = "ListFragment";

    //private OnListFragmentInteractionListener mListener;

    public final static int OPERATOR_ADDITION = 1;
    public final static int OPERATOR_SUBTRACTION = 2;
    public final static int OPERATOR_MULTIPLICATION = 3;

    private Button addition;
    private Button subtraction;
    private Button multiplication;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View listfrag = inflater.inflate(R.layout.fragment_list, container, false);
        // deal with the buttons
        addition = (Button) listfrag.findViewById( R.id.btn_addition );
        subtraction = (Button) listfrag.findViewById( R.id.btn_subtraction );
        multiplication = (Button) listfrag.findViewById( R.id.btn_multiplication );

        addition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonPressed( OPERATOR_ADDITION );
            }
        });

        subtraction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonPressed( OPERATOR_SUBTRACTION );
            }
        });

        multiplication.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onButtonPressed( OPERATOR_MULTIPLICATION );
            }
        });
        return listfrag;
    }

    public void onButtonPressed( int operatorIndex ) {
            ((MainActivity)getActivity()).setViewPager(1);
            //mListener.onListFragmentInteraction( operatorIndex );
    }
/*
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnListFragmentInteractionListener {
        void onListFragmentInteraction( int operatorIndex );
    }
*/
}

DataFragment

package com.example.johnt.hw4;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Random;

public class DataFragment extends Fragment {
    private static final String TAG = "DataFragment";

    private static final String ARGS_OPERATOR = "operatorIndex";

    private int operatorIndex = 1;

    public static DataFragment newInstance(int operatorIndex) {
        DataFragment fragment = new DataFragment();

        Bundle args = new Bundle();
        args.putInt(ARGS_OPERATOR, operatorIndex);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // inflate the layout for this fragment
        View datafrag = inflater.inflate(R.layout.fragment_data, container, false);
        // get a link to the textview
        TextView num1 = (TextView) datafrag.findViewById( R.id.tv_num1 );
        TextView num2 = (TextView) datafrag.findViewById( R.id.tv_num2 );
        TextView num3 = (TextView) datafrag.findViewById( R.id.tv_num3 );
        TextView operator = (TextView) datafrag.findViewById( R.id.tv_operator );
        Random r = new Random();
        switch ( operatorIndex ) {
            case ListFragment.OPERATOR_ADDITION:
                int x = r.nextInt(100);  // 0 - 99
                int y = r.nextInt(100);  // 0 - 99
                int z = x + y;
                num1.setText("" + x);
                num2.setText(""+y);
                num3.setText(""+z);
                operator.setText("+");
                break;

            case ListFragment.OPERATOR_SUBTRACTION:
                int a = r.nextInt(100);  // 0 - 99
                int b = r.nextInt(100 - a + 1) + a;  // a - 99
                int c = a - b;
                num1.setText(a);
                num2.setText(b);
                num3.setText(c);
                operator.setText("-");
                break;

            case ListFragment.OPERATOR_MULTIPLICATION:
                int v = r.nextInt(100);  // 0 - 99
                int m = r.nextInt(100);  // 0 - 99
                int n = v * m;
                num1.setText(v);
                num2.setText(m);
                num3.setText(n);
                operator.setText("X");
                break;
        }
        return datafrag;
    }

}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/list_fragment_port"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

</android.support.constraint.ConstraintLayout>

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Button
        android:id="@+id/btn_addition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Addition"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/btn_subtraction"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_subtraction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/btn_multiplication"
        android:layout_alignParentTop="true"
        android:text="Subtraction"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/btn_multiplication"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_addition" />

    <Button
        android:id="@+id/btn_multiplication"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Multiplication"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_subtraction" />
</android.support.constraint.ConstraintLayout>

fragment_data.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/tv_num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num1"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/tv_operator"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num2"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/tv_equals"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_operator" />

    <TextView
        android:id="@+id/tv_num3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="num3"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_equals" />

    <TextView
        android:id="@+id/tv_operator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="operator"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/tv_num2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_num1" />

    <TextView
        android:id="@+id/tv_equals"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/tv_num3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_num2" />
</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案