0

I have to design an application for tablet. Due to the fact that space availability in that, I have decided to use fragment. Have to split the screen into four parts. Each part displays news title and description. When I click any one part from that 4 part, I have to get the corresponding fragment ID. How can I get it.

In my layout I have given frame layout, and added fragment dynamically using the fragment manager.

ralativLayout_Id.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {    
      ///Here I need to get the clicked part's fragment ID. Or have to find out  which part clicked using any way.
      Toast.makeText(getActivity(), "Clicked " + news_title, Toast.LENGTH_SHORT).show();                  
      ///Have to show the selected news in the whole screen need to call another activity for that.           }
});
4

1 回答 1

2

我必须为平板电脑设计一个应用程序。由于其中的空间可用性,我决定使用片段。必须将屏幕分成四个部分。每个部分显示新闻标题和描述。当我单击这 4 个部分中的任何一个部分时,我必须获取相应的片段 ID。我怎么才能得到它。

View您可以选择使用该方法向 Android 附加一条额外的信息setTag()(并使用 检索它getTag())。因此,在您的片段中,您可以执行以下操作:

// set as the tag for the view the Fragment's id
ralativLayout_Id.setTag(getId());
ralativLayout_Id.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) { 
      //retrieve the fragment's id   
      Integer fragmentId = (Integer) v.getTag();
      // now you have the fragment's id
      ///Here I need to get the clicked part's fragment ID. Or have to find out  which part clicked using any way.
      Toast.makeText(getActivity(), "Clicked " + news_title, Toast.LENGTH_SHORT).show();                  
      ///Have to show the selected news in the whole screen need to call another activity for that.           }
});
于 2013-01-09T12:37:00.257 回答