单击Imageview更改片段

时间:2015-01-02 13:38:18

标签: java android android-fragments imageview fragment

我使用NavigationDrawerFragment创建了一个项目,并为菜单设置了片段。切换菜单片段正在工作。但现在我在其中一个片段上创建了一个imageview。我希望当你点击imageview时,它会像菜单一样改变片段。

MainActivity.java

package com.****.****;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {

        Fragment objFragment = null;

        switch (position) {
            case 0:
                objFragment = new news_Fragment();
                break;
            case 1:
                objFragment = new heroes_Fragment();
                break;
            case 2:
                objFragment = new game_Fragment();
                break;
        }

        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, objFragment)
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }

}

heroes_Fragment.java

package com.****.****;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Calvin on 27/12/2014.
 */
public class heroes_Fragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.trac_layout, container, false);
    }

    public View trac(View view, LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.trac_layout, container, false);


    }
}

heroes_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/header_bg">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="false"
        android:id="@+id/tableLayout"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/icontrac"
                android:src="@drawable/icon_portrait_trac"
                android:layout_column="0"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="20dp"
                android:clickable="true"
                android:onClick="trac"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/iconreap"
                android:src="@drawable/icon_portrait_reap"
                android:layout_column="1"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="20dp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/iconwid"
                android:src="@drawable/icon_portrait_wid"
                android:layout_column="2"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp" />

        </TableRow>

    </TableLayout>

</RelativeLayout>

trac_Fragment.java

package com.****.****;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Calvin on 27/12/2014.
 */
public class trac_Fragment extends Fragment {

    }
}

我希望当你点击imageview(trac)时它会改变片段。好像我们改变了活动。 对不起我的英语。

我尝试使用trac()方法,但应用程序崩溃

2 个答案:

答案 0 :(得分:0)

你确实设置了布局 android:onClick =&#34; trac&#34; ,但你从未使用过trac()方法。

你应该在适当的类(你实现heroes_layout.xml)下声明trac(){...},并在那里解析对新活动的调用(使用Intents)

答案 1 :(得分:0)

感谢Gudin的帮助。你真的帮帮我片段非常复杂,无法理解。我解决了我的问题。

package com.****.****;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

/**
 * Created by Calvin on 27/12/2014.
 */
public class heroes_Fragment extends Fragment implements OnClickListener{
    Button btnChange;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.heroes_layout, container, false);

        ImageButton btntrac = (ImageButton) view.findViewById(R.id.icontrac);
        btntrac.setOnClickListener(this);
        return view;
    }

    public void onClick(View v) {
        trac(v);
    }
android.support.v4.app.FragmentManager manager;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        manager=getFragmentManager();

    }

    public void trac(View view) {
        android.support.v4.app.FragmentTransaction transaction =manager.beginTransaction();
        transaction.replace(R.id.container,new trac_Fragment());
        transaction.addToBackStack(null);
        transaction.commit();
    }


}

这很有效。

相关问题