2015-01-02 6 views
-3

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 { 

    } 
} 

이미지 뷰 (trac)를 클릭하면 조각이 변경됩니다. 마치 우리가 활동을 바꾸는 것처럼. 내 영어를 유감스럽게 생각합니다.

나는 TRAC() 메도하지만

+0

스택 오버플로에 오신 것을 환영합니다. 이것이 첫 번째 질문이므로 http://stackoverflow.com/help/how-to-ask를 읽어보십시오. 예상되는 행동과 현재 코드로 얻는 행동에 대한 질문에 덧붙여주십시오. 또한 작은 그림이 시나리오를 설명하는 데 도움이 될 수 있습니다. – WeSt

답변

0

감사 Gudin 도와 (텐트를 사용하여)이 새로운 활동의 전화를 (당신이 heroes_layout.xml을 구현), 그리고 해결 적절한 클래스에서. 너 정말 도움이된다. 단편은 이해하기가 매우 복잡합니다. 나는 내 문제를 해결했다.

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(); 
    } 


} 

이것은 작동 중입니다.

0

님의 레이아웃 안드로이드에 설정된 않은 응용 프로그램 충돌 사용하려고 : onclick을 = "TRAC"을,하지만 당신은() 메소드 TRAC를 사용하지 마십시오.

당신은 TRAC를 선언해야합니다() {...}

+0

이미 시도했습니다. 하지만 작동하지 않고 응용 프로그램이 다운됩니다. 그리고 나는 새로운 활동을 부르지 만 새로운 단편을 보여주고 싶지 않습니다. – Babboum

+0

메소드는 다음과 같이 표시되어야합니다. public void trac (View v) {...} 두 개의 Fragment를 대체해야합니다. 여기에는 인도인이 설명하는 https://www.youtube.com/watch?v=cDNoJc7azMs&list=PLonJJ3BVjZW4lMlpHgL7UNQSGMERcDzHo#t가 있습니다. = 404 – Gudin

+0

나는 네가 나에게 말한 것을 시도했다. 그러나 나는 오류가있다. java.lang.IllegalStateException : 활동 클래스 com. ****. ***에서 메소드 trac (View)를 찾을 수 없습니다.보기 클래스의 onClick 핸들러에 대한 MainActivity ID가 'icontrac'인 android.widget.ImageView ImageView가 조각에 있습니다. 그래서 나는 heroes_Fragment.java에서 trac (View) 메소드를 생성한다. MainActivity에서 생성하려고 할 때 많은 에러가 발생한다. – Babboum