2017-03-31 122 views
0

我在那裏有一組可繪製的int數組。點擊按鈕時是否可以更改片段中圖像視圖的圖像資源?例如我有一個方法onclicklistener按鈕benefit_babies ...我想設置圖像資源到int數組benefitBaby點擊按鈕。更改按鈕上的圖像資源設置點擊片段

InformationTab片段

package com.example.aldrinjohn.sample; 

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

/** 
* Created by Aldrin John on 3/20/2017. 
*/ 

public class InformationTab extends Fragment{ 
    ViewPager viewPager; 
    CustomSwipeAdapter adapter; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.informationtab, container, false); 
     viewPager = (ViewPager)rootView.findViewById(R.id.view_pager); 
     adapter = new CustomSwipeAdapter(this.getActivity()); 
     viewPager.setAdapter(adapter); 


     return rootView; 

    } 

} 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:id="@+id/trivia" 
     android:text="BreastFeeding Trivia"/> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:id="@+id/benefit_babies" 
     android:text="Benefits to Babies"/> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:id="@+id/storing" 
     android:text="Storing Breastmilk"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:id="@+id/pump" 
      android:text="How to Pump Breastmilk"/> 

     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:id="@+id/benefit_mother" 
      android:text="Benefits to Mothers"/> 

     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:id="@+id/tips" 
      android:text="Tips and Information"/> 

    </LinearLayout> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/image_view"/> 

</LinearLayout> 

Java代碼:

package com.example.aldrinjohn.sample; 

import android.content.Context; 
import android.support.v4.view.PagerAdapter; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

import com.google.android.gms.tagmanager.Container; 

/** 
* Created by Aldrin John on 3/26/2017. 
*/ 

public class CustomSwipeAdapter extends PagerAdapter implements View.OnClickListener{ 
    private int[] trivia = {R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4, R.drawable.c5,R.drawable.c6}; 
    private int[] benefitBaby = {R.drawable.c7,R.drawable.c8,R.drawable.c9}; 
    private int[] benefitMother = {R.drawable.c91,R.drawable.c92}; 
    private int[] info = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7, 
          R.drawable.a8,R.drawable.a9,R.drawable.a91,R.drawable.a92}; 
    private int[] storing = {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4}; 
    private Context ctx; 
    private LayoutInflater layoutInflater; 
    Button btnbenefitB; 

    public CustomSwipeAdapter(Context ctx) 
    { 
     this.ctx = ctx; 
    } 

    @Override 
    public int getCount() { 
     return trivia.length; 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return (view==(LinearLayout)object); 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position){ 
     layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false); 
     btnbenefitB = (Button)item_view.findViewById(R.id.benefit_babies); 
     btnbenefitB.setOnClickListener(this); 
     ImageView imageView = (ImageView)item_view.findViewById(R.id.image_view); 
     imageView.setImageResource(trivia[position]); 
     container.addView(item_view); 

     return item_view; 
    } 
@Override 
    public void destroyItem(ViewGroup container, int position, Object object){ 
     container.removeView((LinearLayout)object); 
    } 


    public int getCount(int[] x) { 
     return x.length - 1; 
    } 



    @Override 
    public void onClick(View v) { 
     switch(v.getId()){ 
      case R.id.benefit_babies: 

       break; 
     } 
    } 
} 
+0

可能出現[android change image onclick imageviev]重複(http://stackoverflow.com/questions/25957031/android-change-image-onclick-imageviev) –

+0

@ivan_pozdeev不是真正的先生。那只是設置一個圖像。而我試圖要求的是在按鈕單擊時初始化一組新的可繪製圖像。 –

回答

1

imageview.setImageResource(id)其中id是你int陣列。有關詳細信息,請參閱documentation for ImageView。這與您在代碼中處於onClick()處理程序中的相同。

setImageResource

空隙setImageResource(INT渣油)

設置一個可拉伸,因爲這ImageView的內容。

這會在UI線程上進行位圖讀取和解碼,這會導致延遲打嗝。如果這是一個問題,請考慮使用setImageDrawable(android.graphics.drawable.Drawable)或setImageBitmap(android.graphics.Bitmap)和BitmapFactory代替。

+0

如何在按鈕onclick中編碼?我試過 ImageView imageView =(ImageView)v.findViewById(R.id.image_view); imageView.setImageResource(benefitBaby [0]);我嘗試了值0-2,但仍然崩潰,說它返回null。 –

0

大家好,感謝您的幫助。我通過使用視圖頁適配器解決了它。每一個按鈕我都會改變適配器。謝謝!!