2015-09-22 68 views
1

我是全新的android和嘗試創建一個活動,其中有一個片段。這個想法是片段有3個ImageButtons。您單擊的ImageButton會將其圖像傳遞給活動,然後將其顯示在主活動內的onClick(全屏ImageView)之後創建的另一個片段中。問題是我無法弄清楚如何傳遞信息。我已經閱讀了許多類似於這個問題的問題,並且瀏覽了android開發者網站,但是我讀得越多,它就越容易混淆。我也遇到了f​​indViewById的問題,說它無法解決該方法。我不知道爲什麼它告訴我這一點,並嘗試了很多事情來解決它,沒有任何工作。這是我的代碼和任何建議,將不勝感激。通過ImageButton值

主要活動:

import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity implements MainActivityFragment.CanPassData{ 

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


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 


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

     return super.onOptionsItemSelected(item); 
    } 
    public void passData(int value){ 
     MainActivityFragment fragment = new MainActivityFragment(); 
     fragment.setData(5); 
     getSupportFragmentManager().beginTransaction().add(R.id.fragment, fragment).commit(); 
     //***Taken From Android Developer, I believe this is how you create a new fragment when you pass the data to the main activity**** 
     if (fragment != null) { 
      fragment.getData(); 
     } else { 
      MainActivityFragment newFragment = new MainActivityFragment(); 
      Bundle args = new Bundle(); 
      //******No idea what this is calling, pictureChosen and picture are default values taken from the website******* 
      args.putInt(MainActivityFragment.pictureChosen, picture); 
      newFragment.setArguments(args); 

      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 


      transaction.replace(R.id.fragment, newFragment); 
      transaction.addToBackStack(null); 
      transaction.commit(); 
     } 
     //***************************************************************************************** 
    } 
    public void processData(){ 
     MainActivityFragment fragment = (MainActivityFragment) getSupportFragmentManager().findFragmentById(R.id.fragment); 
     int value = fragment.getData(); 
    } 
} 

片段:

import android.content.Intent; 
import android.graphics.drawable.Drawable; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageButton; 
import android.widget.ImageView; 

public class MainActivityFragment extends Fragment implements View.OnClickListener { 

    private int data; 
    private CanPassData parent; 

    public interface CanPassData{ 
     public void passData(int value); 
    } 

    public MainActivityFragment() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     //***findViewById is saying it cannot resolve below 
     setContentView(R.layout.fragment_main); 
     ImageButton button1 = (ImageButton) findViewById(R.id.imageButton); 
     ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2); 
     ImageButton button3 = (ImageButton) findViewById(R.id.imageButton3); 

     button1.setOnClickListener(this); 
     button2.setOnClickListener(this); 
     button3.setOnClickListener(this); 
     //****************************************************************************************** 
     return inflater.inflate(R.layout.fragment_main, container, false); 
    } 

    public void onAttach(MainActivity activity){ 
     parent = (CanPassData) activity; 
    } 

    public void setData(int value){ 
     data = value; 
    } 
    //***onClick also has all findViewByID as cannot resolve method, and i'm not exactly sure what to do within each case, the stuff I have in there was also taken from android developer. 
    @Override 
    public void onClick(View v){ 
     switch (v.getId()) { 
      case R.id.imageButton: 
       ImageView box = (ImageView)findViewByID(R.id.imageButton); 
       Drawable name = box.getBackground().getCurrent(); 
       Intent intent = new Intent (this, newFragment.class); 
       intent.putExtra("com.example.coding.assignment2", name); 
       startActivity(intent); 
       break; 
      case R.id.imageButton2: 
       box = (ImageView)findViewByID(R.id.imageButton2); 
       name = box.getBackground().getCurrent(); 
       intent = new Intent (this, newFragment.class); 
       intent.putExtra("com.example.coding.assignment2", name); 
       startActivity(intent); 
       break; 
      case R.id.imageButton3: 
       box = (ImageView)findViewByID(R.id.imageButton3); 
       name = box.getBackground().getCurrent(); 
       intent = new Intent (this, newFragment.class); 
       intent.putExtra("com.example.coding.assignment2", name); 
       startActivity(intent); 
       break; 
     } 
    } 

    public void pushData(){ 
     parent.passData(data); 
    } 

    public int getData(){ 
     return data; 
    } 
} 

了很多方法,如的getData和pushData我不完全知道如何使用獲得的信息的主要活動,這是在課堂上給我的信息。我想知道是否有一種方法可以將drawable本身傳遞給click,然後將該drawable分配給新的fragment中的ImageView,從而顯示它覆蓋整個片段。我被告知我需要創建第二個片段xml,但是java文件本身應該通過主要活動創建。它是否正確?

+0

笑。這可能會讓人困惑,因爲有多種方法可以做到這一點,那麼該怎麼做?既然你打算從片段 - >活動 - >片段,你可以創建一個新的類文件,命名爲(例如)全局變量,你可以在其中定義一個靜態的drawable或其他東西。 OnClick事件將圖像放入靜態全局繪圖和bam!你可以在任何地方使用它,而無需在任何地方編寫代碼。作爲它的靜態,你需要確保內存泄漏和使用,但它的可重用雖然可以重用。 – CmosBattery

回答

0

不能化解findViewById因爲你必須以人爲方式View使用片段findViewById,像這樣:

View v = inflater.inflate(R.layout.fragment_main, parent, false); 
ImageButton button1 = (ImageButton) v.findViewById(R.id.imageButton); 
ImageButton button2 = (ImageButton) v.findViewById(R.id.imageButton2); 
ImageButton button3 = (ImageButton) v.findViewById(R.id.imageButton3); 

//call your button.setOnClickListener 
return v; 

無需調用setContentView()

onClick(View v)你還需要打電話給v.findViewById()

另外,你也可以在一個案例中解釋你的變量,你應該在任何案例之前聲明它們,如果你想用這種方式。它應該是這個樣子:

public void onClick(View v){ 
    ImageView box=null; 
    Drawable name = null; 
    Intent intent = null; 
    switch(v.getId()){ 
     case R.id.imageButton: 
      box = (ImageView) v.findViewByID(R.id.imageButton); 
      name = box.getBackground().getCurrent(); 
      intent = new Intent (this, newFragment.class); 
      intent.putExtra("com.example.coding.assignment2", name); 
      startActivity(intent); 
      break; 

等等