2016-08-02 89 views
0

我有動態填充的ImageViews和Textviews。如何在ImageView上實現onClick()方法

現在我有一個在ImageView上實現onClick方法的小問題。在某些ImageView上單擊它應該切換到片段,並有一些點擊到活動。 這裏是我的代碼 - 我的主要片段:

public class MainFragment extends Fragment { 
public static String[] gridViewStrings = { 
      "string1", 
      "string2", 
      "string3", 
      "string4", 
      "string5", 
      "string6" 
    }; 
    public static int[] gridViewImages = { 
      R.drawable.delivery, 
      R.drawable.shipping_logs, 
      R.drawable.meassurement, 
      R.drawable.takeovers, 
      R.drawable.settings, 
      R.drawable.download_data 
    }; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.main_fragment, parent, false); 
gridView = (GridView) view.findViewById(R.id.grid); 
     gridView.setAdapter(new CustomAndroidGridViewAdapter(getActivity(), gridViewStrings, gridViewImages)); 

     imageView = (ImageView) view.findViewById(R.id.gridview_image); 
} 

我自定義的GridView適配器:

public class CustomAndroidGridViewAdapter extends BaseAdapter { 
    private Context mContext; 
    private final String[] string; 
    private final int[] Imageid; 

    public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid) { 
     mContext = c; 
     this.Imageid = Imageid; 
     this.string = string; 
    } 

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

    @Override 
    public Object getItem(int p) { 
     return null; 
    } 

    @Override 
    public long getItemId(int p) { 
     return 0; 
    } 

    @Override 
    public View getView(final int p, final View convertView, ViewGroup parent) { 
     View grid; 
     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 

      grid = new View(mContext); 
      grid = inflater.inflate(R.layout.grid, null); 
      TextView textView = (TextView) grid.findViewById(R.id.gridview_text); 
      ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image); 
      textView.setText(string[p]); 
      imageView.setImageResource(Imageid[p]); 
     } else { 
      grid = (View) convertView; 
     } 

     return grid; 
    } 
} 

問題:如何在ImageView的實現的onClick()並切換到片段和活動?

回答

1

接口回調:

public interface LaunchFragmentWithImage{ 
void launchFragmentWithImage(int imageID); 
} 

給適配器低於這個類的一個基準,因此它可以調用該方法。不建議給適配器本身一個參考,因爲它不適合適配器負責知道誰接收圖像,因此違反了OOD的單一責任原則,但爲了學習它的好處。

public class ClassToLaunchNewFragment extends AppCompatActivity implements 
LaunchFragmentWithImage{ 

    @Override //Method from interface we've defined. 
    public void launchFragmentWithImage(int imageID){ 
    FragmentToLaunch fragmentToLaunch = FragmentToLaunch.newInstance(); 
    Bundle imageData = new Bundle(); 
    imageData.putInt(imageID, "key"); 
    fragmentToLaunch.setArguments(imageData); 

    getSupportFragmentManager().beginTransaction() //Call replace not add 
           .replace(containerID, fragmentToLaunch) 
           .commit(); 

    } 

} 

在AdapterClass

imageViewVar.setOnClickListener(new View.OnClickListener(){ 

@Override 
public void onClick(View view){ 
fragmentToLaunchInstanceThatWasPassedIn.launchFragmentWithImage(theImageResource); 
} 


}); 
+0

我知道接口看起來像沒有用,因爲它們除了定義方法之外什麼都不做簽名,但實際上他們是OOP程序員的夢想。我建議你真的學會如何使用它們。 –

+0

謝謝你的回答,非常有用。這是我要去特定片段的例子。當不同的圖像打開不同的片段時,你能否給我提供幾點建議 – RubyDigger19

+0

這實際上取決於你想如何構造碎片。我的意思是,如果這些片段是相同的一般對象,但具有不同的行爲和狀態,我建議讓他們擴展一個抽象類,然後根據哪個imageview是基於哪個圖像視圖挑選並選擇要啓動的抽象片段的哪個結構點擊。此外,如果我的答案有幫助,一定要評爲最佳答案並進行投票。謝謝! –

0
grid = new View(mContext); 
grid = inflater.inflate(R.layout.grid, null); 

你爲什麼要將網格變量指向兩個單獨的視圖對象?

+0

錯誤,我做了一些事情,忘記刪除第一行。 – RubyDigger19

+0

哦,好吧。那麼你必須像你的答案一樣具體,因爲你必須在編碼方面。你在問如何啓動一個新的片段實例,或者問如何啓動一個新的活動? –

+0

這兩者,因爲其中一些ImageView會去片段和一些活動,但讓我們專注於片段。 – RubyDigger19

0
if (convertView == null) { 

     grid = inflater.inflate(R.layout.grid, null); 
     TextView textView = (TextView) grid.findViewById(R.id.gridview_text); 
     ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image); 
     textView.setText(string[p]); 
     imageView.setImageResource(Imageid[p]); 
     imageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      switch(choosedPosition){ 
       case 0:do something;break; 
       case 1:do something;break; 
       ... 
      } 
     } 
     }); 
     } 

這只是一個例子,不能運行。

3
public DlgOutletListAdapter(Context context, List<WrapperOutlet> wrapperOutletList, View.OnClickListener listener) { 
     this.context = context; 
     this.wrapperOutletList = wrapperOutletList; 
     this.listener = listener;  //---> pass in listener.... 

    } 

    @Override 
    public int getCount() { 
     return wrapperOutletList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return wrapperOutletList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder viewHolder; 
     if (convertView == null) { 
      convertView = LayoutInflater.from(context).inflate(R.layout.item_dlg_outlet_list, parent, false); 
      viewHolder = new ViewHolder(convertView); 
      viewHolder.ll_dlg_outlet_list_container.setOnClickListener(listener); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     if (!wrapperOutletList.isEmpty()) { 
      WrapperOutlet wrapperOutlet = wrapperOutletList.get(position); 
      Outlet outlet = wrapperOutlet.getOutlet(); 
      if (outlet != null) { 
       if (TextUtils.isEmpty(outlet.getTel())){ 
        viewHolder.ivCall.setImageResource(R.drawable.icon_call2); 
        viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.medium_gray)); 
         viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(null); 
       } else { 
        viewHolder.ivCall.setImageResource(R.drawable.icon_call); 
        viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.fresh_teal)); 
        viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(listener);  
       } 


      } 
     } 

     return convertView; 
    } 

通行證的聽衆。給你一個上面的例子。以下代碼是您在片段中調用適配器的位置。我的片段實現View.OnClickListener

lv_dlg_outlet_list.setAdapter(new DlgOutletListAdapter(activity, wrapperOutletList, this));