通过ImageView替换片段

时间:2018-01-04 18:55:04

标签: java android xml android-fragments

我正在使用导航抽屉模板。在我的MainActivity中,我有'HomeFragment',其中包含许多ImageView。我想点击其中一个ImageViews然后我想要它然后带我到另一个片段,它有一个更大的ImageView和一些文本的图片。然后,我想按下后退按钮,并希望它带我回到之前的片段。

请提供代码,请有人帮忙吗?然后,我将研究代码,看看它是如何工作的。

MainActivity代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
//      Code to display Home Fragment on App Launch Page
        HomeFragment homeFragment = new HomeFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(
                R.id.relativelayout_for_fragment,
                homeFragment,
                homeFragment.getTag()
        ).commit();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.action_LoginPage:
                Intent intent = new Intent(this, LoginPageMain.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
//      Code for Nav Bar Item Clicks (Button Clicks)
        int id = item.getItemId();

//      Code to link Fragment to Button
        if (id == R.id.nav_home) {
            HomeFragment homeFragment = new HomeFragment();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(
                    R.id.relativelayout_for_fragment,
                    homeFragment,
                    homeFragment.getTag()
            ).commit();
        } else if (id == R.id.nav_SUV) {
            SUVFragment suvFragment = new SUVFragment();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(
                    R.id.relativelayout_for_fragment,
                    suvFragment,
                    suvFragment.getTag()
            ).commit();
        } else if (id == R.id.nav_hatchbacks) {
            HatchbackFragment hatchbackFragment = new HatchbackFragment();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(
                    R.id.relativelayout_for_fragment,
                    hatchbackFragment,
                    hatchbackFragment.getTag()
            ).commit();

        } else if (id == R.id.nav_coupes) {
            CoupesFragment coupesFragment = new CoupesFragment();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(
                    R.id.relativelayout_for_fragment,
                    coupesFragment,
                    coupesFragment.getTag()
            ).commit();

        } else if (id == R.id.nav_find_us) {
            FindUsFragment findUsFragment = new FindUsFragment();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(
                    R.id.relativelayout_for_fragment,
                    findUsFragment,
                    findUsFragment.getTag()
            ).commit();

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

HomeFragment代码(我想用代码将用户转换为第二个片段替换“已被点击”的位)

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    public HomeFragment() {
        // Required empty public constructor
    }

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.home_fragment, container, false);

        ImageView button1 = (ImageView) view.findViewById(R.id.imageViewa45);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
                alert.setTitle("its been clicked");

                alert.show();
            }
        });

        return view;
    }
}

HomeFragment XML代码:

    <ImageView
        android:id="@+id/imageViewa45"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/imageView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="318dp"
        android:src="@drawable/a45hatchback" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="350dp"
        android:src="@drawable/velarad" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="310dp"
        android:src="@drawable/rs5coupe" />

</RelativeLayout>

A45Fragment(我希望用户在点击ImageView后看到的片段)

public class A45Fragment {
}

1 个答案:

答案 0 :(得分:1)

button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               A45Fragment findUsFragment = new A45Fragment();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(
                    R.id.relativelayout_for_fragment,
                    findUsFragment,
                    findUsFragment.getTag()
            ).commit();
            }
        });

试试这个。通过上面的调用你可以在图像点击上加载片段。