Android:想要使用Fragment一次只选择一个图像视图

时间:2016-09-10 07:29:54

标签: android fragment

我将4 Ext.each(grids,function(grid) { if(grid.normalGrid) grid = grid.normalGrid; // Use the normal subgrid of a grid with locked columns. Ext.each(grids,function(partner) { if(partner.normalGrid) partner = partner.normalGrid; // Use the normal subgrid of a grid with locked columns. if(grid != partner) grid.getView().getScrollable().addPartner(partner.getView().getScrollable(), "y"); // "y" tells the scrollable that the partnership is only regarding y axis. }); }); 设置为底部菜单。 我想在那时选择一个图像,其他三个图像被取消选择。

当我移动到下一个屏幕时,我想要选择我设置的特定图像。 我为每个ImageView 1使用了2张图像用于取消选择(灰色),另一张图像用于选择(蓝色)。

这里我发布了我的完整代码: -

HomePage.Java

ImageView

在另一项活动中

Attendence.Java

public class HomePage extends Fragment implements View.OnClickListener {
    ImageView mHomeButton,mattendence,mTarget,mReport;
    public HomePage() {};
    Activity activity;
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // don't look at this layout it's just a listView to show how to handle the keyboard
        View view = inflater.inflate(R.layout.activity_home_page, container, false);
        getActivity().setTitle("HomePage");
        mHomeButton = (ImageView)view.findViewById(R.id.fHome);
        mattendence = (ImageView)view.findViewById(R.id.fAttendence);
        mTarget = (ImageView) view.findViewById(R.id.fTarget);
        mReport = (ImageView)view.findViewById(R.id.fReport);

        mHomeButton.setOnClickListener(this);
        mattendence.setOnClickListener(this);
        mTarget.setOnClickListener(this);
        mReport.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        mHomeButton.setImageResource(R.drawable.home);
        mattendence.setImageResource(R.drawable.att);
        mTarget.setImageResource(R.drawable.target);
        mReport.setImageResource(R.drawable.report);

        Intent intent = new Intent(this.getActivity(), HomePage.class);
        int selected = 0;


        switch (v.getId()){
            case R.id.fHome:
                mHomeButton.setImageResource(R.drawable.blue);

                selected =1;
                Fragment con = new HomePage();
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.framelayout, con).commit();
                break;
            // define same as all other views
            case R.id.fAttendence:
                mattendence.setImageResource(R.drawable.blue);
                selected = 2;
                con = new Attendence();
                fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.framelayout, con).commit();

                break;
            case R.id.fTarget:
                mTarget.setImageResource(R.drawable.blue);
                selected =3;
                con = new Payment();
                fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.framelayout, con).commit();

                break;
            case R.id.fReport:
                mReport.setImageResource(R.drawable.blue);
                selected =4;
                con = new Delivery();
                fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.framelayout, con).commit();

                break;
        }
        intent.putExtra("selected",selected);
        startActivity(intent);
    }
}

Delivery.Java

public class Attendence extends Fragment implements View.OnClickListener{
    ImageView mHomeButton,mattendence,mTarget,mReport;
    public Attendence(){};
    Activity activity;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // don't look at this layout it's just a listView to show how to handle the keyboard
        View view = inflater.inflate(R.layout.activity_attendence, container, false);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getActivity().setTitle("My Attendence");
        activity = getActivity();
        mHomeButton = (ImageView)view.findViewById(R.id.fHome);
        mattendence = (ImageView)view.findViewById(R.id.fAttendence);
        mTarget = (ImageView) view.findViewById(R.id.fTarget);
        mReport = (ImageView)view.findViewById(R.id.fReport);

        mHomeButton.setOnClickListener(this);
        mattendence.setOnClickListener(this);
        mTarget.setOnClickListener(this);
        mReport.setOnClickListener(this);


        return  view;

    }

    @Override
    public void onClick(View v) {
        mHomeButton.setImageResource(R.drawable.home);
        mattendence.setImageResource(R.drawable.att);
        mTarget.setImageResource(R.drawable.target);
        mReport.setImageResource(R.drawable.report);
        int selected = getActivity().getIntent().getIntExtra("selected",2);
            switch(selected){

                case 1:
                    mHomeButton.setImageResource(R.drawable.blue);
                    Fragment con1 = new HomePage();
                    FragmentManager fragmentManager1 = getFragmentManager();
                    fragmentManager1.beginTransaction().replace(R.id.framelayout, con1).commit();

                    break;
                // define same as all other views
                case 2:
                    mattendence.setImageResource(R.drawable.blue);
                    Fragment con2 = new Attendence();
                    FragmentManager fragmentManager2 = getFragmentManager();
                    fragmentManager2.beginTransaction().replace(R.id.framelayout, con2).commit();

                    break;
                case 3:
                    mTarget.setImageResource(R.drawable.blue);
                    Fragment con3 = new Payment();
                    FragmentManager fragmentManager3 = getFragmentManager();
                    fragmentManager3.beginTransaction().replace(R.id.framelayout, con3).commit();


                    break;
                case 4:
                    mReport.setImageResource(R.drawable.blue);
                    Fragment con4 = new Delivery();
                    FragmentManager fragmentManager4 = getFragmentManager();
                    fragmentManager4.beginTransaction().replace(R.id.framelayout, con4).commit();
                    break; }

        }

    }

Payment.Java

public class Delivery extends Fragment implements View.OnClickListener{

    ImageView mHomeButton,mattendence,mTarget,mReport;
    Activity activity;

    public Delivery(){};

    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // don't look at this layout it's just a listView to show how to handle the keyboard
        View view = inflater.inflate(R.layout.activity_delivery, container, false);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getActivity().setTitle("Delivery");
        activity = getActivity();
        mHomeButton = (ImageView)view.findViewById(R.id.fHome);
        mattendence = (ImageView)view.findViewById(R.id.fAttendence);
        mTarget = (ImageView) view.findViewById(R.id.fTarget);
        mReport = (ImageView)view.findViewById(R.id.fReport);

        mHomeButton.setOnClickListener(this);
        mattendence.setOnClickListener(this);
        mTarget.setOnClickListener(this);
        mReport.setOnClickListener(this);

        return  view;

    }

    @Override
    public void onClick(View v) {
        mHomeButton.setImageResource(R.drawable.home);
        mattendence.setImageResource(R.drawable.att);
        mTarget.setImageResource(R.drawable.target);
        mReport.setImageResource(R.drawable.report);
        int selected = getActivity().getIntent().getIntExtra("selected",4);

        switch(selected){
            case 1:
                mHomeButton.setImageResource(R.drawable.blue);
                Fragment con1 = new HomePage();
                FragmentManager fragmentManager1 = getFragmentManager();
                fragmentManager1.beginTransaction().replace(R.id.framelayout, con1).commit();

                break;
            // define same as all other views
            case 2:
                mattendence.setImageResource(R.drawable.blue);
                Fragment con2 = new Attendence();
                FragmentManager fragmentManager2 = getFragmentManager();
                fragmentManager2.beginTransaction().replace(R.id.framelayout, con2).commit();

                break;
            case 3:
                mTarget.setImageResource(R.drawable.blue);
                Fragment con3 = new Payment();
                FragmentManager fragmentManager3 = getFragmentManager();
                fragmentManager3.beginTransaction().replace(R.id.framelayout, con3).commit();


                break;
            case 4:
                mReport.setImageResource(R.drawable.blue);
                Fragment con4 = new Delivery();
                FragmentManager fragmentManager4 = getFragmentManager();
                fragmentManager4.beginTransaction().replace(R.id.framelayout, con4).commit();
                break;

        }


    }

    }

就像 Payment.Java 我创建了另外两个类名 Delivery.Java &的 Attendence.Java

我得到的错误如下: -

public class Payment extends Fragment implements View.OnClickListener {
    ImageView mHomeButton, mattendence, mTarget, mReport;
    public Payment() {};
    View view;
    Activity activity;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // don't look at this layout it's just a listView to show how to handle the keyboard
        activity = getActivity();
        View view = inflater.inflate(R.layout.activity_payment, container, false);
        getActivity().setTitle("Payment");
        mHomeButton = (ImageView) view.findViewById(R.id.fHome);
        mattendence = (ImageView) view.findViewById(R.id.fAttendence);
        mTarget = (ImageView) view.findViewById(R.id.fTarget);
        mReport = (ImageView) view.findViewById(R.id.fReport);

        mHomeButton.setOnClickListener(this);
        mattendence.setOnClickListener(this);
        mTarget.setOnClickListener(this);
        mReport.setOnClickListener(this);

        return view;
    }
    @Override
    public void onClick(View v) {
        mHomeButton.setImageResource(R.drawable.home);
        mattendence.setImageResource(R.drawable.att);
        mTarget.setImageResource(R.drawable.target);
        mReport.setImageResource(R.drawable.report);
        int selected = getActivity().getIntent().getIntExtra("selected",3);
        switch(selected){

            case 1:
                mHomeButton.setImageResource(R.drawable.blue);
                Fragment con1 = new HomePage();
                FragmentManager fragmentManager1 = getFragmentManager();
                fragmentManager1.beginTransaction().replace(R.id.framelayout, con1).commit();

                break;
            // define same as all other views
            case 2:
                mattendence.setImageResource(R.drawable.blue);
                Fragment con2 = new Attendence();
                FragmentManager fragmentManager2 = getFragmentManager();
                fragmentManager2.beginTransaction().replace(R.id.framelayout, con2).commit();

                break;
            case 3:
                mTarget.setImageResource(R.drawable.blue);
                Fragment con3 = new Payment();
                FragmentManager fragmentManager3 = getFragmentManager();
                fragmentManager3.beginTransaction().replace(R.id.framelayout, con3).commit();


                break;
            case 4:
                mReport.setImageResource(R.drawable.blue);
                Fragment con4 = new Delivery();
                FragmentManager fragmentManager4 = getFragmentManager();
                fragmentManager4.beginTransaction().replace(R.id.framelayout, con4).commit();
                break;
        }
    }
}

清单文件

Process: com.example.sachin.omcommunication, PID: 23488
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.sachin.omcommunication/com.example.sachin.omcommunication.HomePage}; have you declared this activity in your AndroidManifest.xml?
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1630)
        at android.app.Activity.startActivityForResult(Activity.java:4549)
        at android.app.Activity.startActivityFromFragment(Activity.java:4535)
        at android.app.Activity$HostCallbacks.onStartActivityFromFragment(Activity.java:6609)
        at android.app.Fragment.startActivity(Fragment.java:1092)
        at android.app.Fragment.startActivity(Fragment.java:1071)
        at com.example.sachin.omcommunication.HomePage.onClick(HomePage.java:85)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

请帮我解决这个问题..

2 个答案:

答案 0 :(得分:0)

您想要使用Fragment

加载Intent
Intent intent = new Intent(this.getActivity(), HomePage.class);
intent.putExtra("selected",selected);
startActivity(intent);

HomePageFragment。您无法使用Intent加载它。从代码中删除这些行。这将解决您的错误。

现在您的图像选择问题。

您正在选择图像并尝试将所选图像索引传递给每个片段。

int selected = getActivity().getIntent().getIntExtra("selected", index); // This is wrong

您需要更改此问题的方法。使用当前的方法,您需要将值从一个片段传递到另一个片段。这是凌乱的(恕我直言)。

更好的方法是将页脚仅放在Activity(Home.java)中。因此,您根本不必为传递值而烦恼。您的活动的xml需要以下布局。

<LinearLayout android:orientation="vertical" >

    <FrameLayout android:id= "@+id/R.id.framelayout" />

    <include
        android:id="@+id/footer"
        layout="@layout/footer"/>

<LinearLayout>

仅在此Activity中管理点击次数。因此,您无需管理Fragment中的点击次数。

修改

您也可以使用FragmentTabHost。默认情况下,选项卡显示在屏幕上方,这是android推荐的。你可以把它搞定。 Herehere就是一些例子。

您需要调用setup()的{​​{1}}方法进行初始化。内容加载为FragmentTabHost。使用Fragments方法为其添加标签。

Tabhost tutorial

祝你好运:)

答案 1 :(得分:0)

它清楚地表明你没有在Manifest.xml

中宣布该课程
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.sachin.omcommunication/com.example.sachin.omcommunication.HomePage}; have you declared this activity in your AndroidManifest.xml?

如果有任何错误,请尝试添加和更新。