如何从点击视图中获得片段Id,或者如何识别从4个部分中的一个点击了哪个部分(将屏幕分成4个相等的部分)



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

在我的布局中,我给出了框架布局,并使用片段管理器动态添加了片段。

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

我必须为平板电脑设计一个应用程序。因为空间在可用性方面,我决定使用fragment。必须分开屏幕分为四个部分。每个部分显示新闻标题和描述。当我点击这四个部分中的任何一个部分时,我必须得到对应的分片ID。我怎样才能得到它呢?

您可以选择使用setTag()方法将额外的信息附加到Android View(并使用getTag()检索它)。在fragments中,你可以输入:

// 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.           }
});

最新更新