Java 如何获取视图的实例';s父片段:
我有一个带有Java 如何获取视图的实例';s父片段:,java,android,layout,casting,android-fragments,Java,Android,Layout,Casting,Android Fragments,我有一个带有XML布局文件的片段。在它里面,我有一个2个可点击的ImageViews。 对于每个ImageView我设置了一个onClick方法,例如:android:onClick=“commentFragmentRemoveOnClick” 在FragmentActivity(Activity no the Fragment)中,我这样定义它: public void commentFragmentRemoveOnClick(View v) { } 否此片段的类型为CommentFrag
XML
布局文件的片段。在它里面,我有一个2个可点击的ImageView
s。
对于每个ImageView
我设置了一个onClick
方法,例如:android:onClick=“commentFragmentRemoveOnClick”
在FragmentActivity(Activity no the Fragment)中,我这样定义它:
public void commentFragmentRemoveOnClick(View v)
{
}
否此片段的类型为CommentFragment
,它有一个public void getFragmentTag()
方法
以获取我在较早时间保存的标签。我需要获取片段的一个实例,在该实例中单击图像以获取其标记
我试过:
((CommentFragment)v).getParentFragment().getFragmentTag();
以及:
但是eclipse在这两个方面都给了我错误,这是如何正确完成的
更清楚地说,这是我的CommentFragment
:
public class CommentFragment extends Fragment {
private final static String TAG = CommentFragment.class.getSimpleName();
private String fragmentTag;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.comment_fragment_layout,
container, false);
Bundle bundle = getArguments();
String text = bundle.getString("comment");
String fullUser = bundle.getString("user");
String user = fullUser.substring(0, fullUser.indexOf("@"));
String at = bundle.getString("at");
TextView tvCmment = (TextView) rootView.findViewById(R.id.tvComment);
TextView tvUser = (TextView) rootView.findViewById(R.id.tvUser);
TextView tvAt = (TextView) rootView.findViewById(R.id.tvDate);
tvCmment.setText(text);
tvUser.setText(user);
tvAt.setText(at);
return rootView;
}
public void setText(String item)
{
TextView view = (TextView) getView().findViewById(R.id.tvComment);
view.setText(item);
}
public void setFragmentTag(String tag)
{
this.fragmentTag = tag;
}
public String getFragmentTag()
{
return this.fragmentTag;
}
}
和布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llCommentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/try2">
<TextView
android:id="@+id/tvUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvComment"
android:layout_alignParentTop="true"
android:background="@color/my_gray"
android:text="demo"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="@color/my_even_darker_gray" />
<TextView
android:id="@+id/tvComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvDate"
android:padding="5dp"
android:text="This task is described in more details if you click on it."
android:textColor="@color/my_even_darker_gray" />
<TextView
android:id="@+id/tvAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingRight="5dp"
android:textColor="@color/my_even_darker_gray"
android:layout_toRightOf="@+id/tvUser"
android:background="@color/my_gray"
android:text="at" />
<TextView
android:id="@+id/tvDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvAt"
android:layout_alignBottom="@+id/tvAt"
android:layout_toRightOf="@+id/tvAt"
android:background="@color/my_gray"
android:text="12/02"
android:textColor="@color/my_even_darker_gray" />
<ImageView
android:id="@+id/iEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvComment"
android:layout_marginRight="4dp"
android:clickable="true"
android:contentDescription="@drawable/add_comment_button"
android:onClick="commentFragmentEditOnClick"
android:src="@drawable/add_comment_button" />
<ImageView
android:id="@+id/iRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/iEdit"
android:layout_toRightOf="@+id/iEdit"
android:layout_marginRight="4dp"
android:clickable="true"
android:contentDescription="@drawable/add_comment_button"
android:onClick="commentFragmentRemoveOnClick"
android:src="@drawable/add_comment_button" />
</RelativeLayout>
我很想得到一点帮助
谢谢。v不是
Fragment
的实例,这就是为什么Eclipse不喜欢您的代码。如果您想要片段的实例,必须使用FragmentManager
及其findFragmentByXXX方法之一。要获取在中单击ImageView
的片段实例,我执行了以下操作:
在片段中,我为两个图像设置了两个onClickListeners
,如下所示:
iEdit = (ImageView)rootView.findViewById(R.id.iEdit);
iEdit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d(TAG, "pressed edit button");
((PicturesAndCommentsActivity) getActivity()).commentFragmentEditOnClick(fragmentTag);
}
});
iRemove = (ImageView)rootView.findViewById(R.id.iRemove);
iRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.d(TAG, "pressed remove button");
((PicturesAndCommentsActivity) getActivity()).commentFragmentRemoveOnClick(fragmentTag);
}
});
public void commentFragmentRemoveOnClick (String tag)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentManager.findFragmentByTag(tag)).commit();
}
在片段活动中,我定义了以下两种方法:
iEdit = (ImageView)rootView.findViewById(R.id.iEdit);
iEdit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d(TAG, "pressed edit button");
((PicturesAndCommentsActivity) getActivity()).commentFragmentEditOnClick(fragmentTag);
}
});
iRemove = (ImageView)rootView.findViewById(R.id.iRemove);
iRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.d(TAG, "pressed remove button");
((PicturesAndCommentsActivity) getActivity()).commentFragmentRemoveOnClick(fragmentTag);
}
});
public void commentFragmentRemoveOnClick (String tag)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentManager.findFragmentByTag(tag)).commit();
}
用于删除片段,现在我正在编辑片段。我有一个一般性的建议,可以解决您的问题并在将来帮助您-
它还可以解决您识别单击图像的片段的问题。我知道v不是片段,v是片段内部的ImageView,我需要通过该ImageView获取该片段的实例。我的片段是动态添加的,没有id。它们只有我通过setFragmentTag和getFragmentTag分配给它们的标记,我现在正尝试使用它们。另外,如果您知道v不是片段,为什么要执行
((CommentFragment)v)
?您没有得到我的问题:要通过FragmentManager使用findFragmentByTag方法,我需要获取单击视图所在片段的实例,并从中提取我先前分配给它的已分配标记。这就是我要做的,如果你仔细看的话,你会发现我正在尝试获取点击视图的父视图,我只是不知道它是如何完成的。现在我知道了。所以一些提示:尽量避免使用android:onClick,而是手动分配你的点击监听器。此外,如果动态添加了片段,则可以将片段的标记设置为图像视图。这样,如果您想要包含单击的ImageView
的片段的片段标记,您只需执行v.getTag()。请再看看这个问题,我已经添加了代码。因此,如果我将标记设置为ImageView,我仍然必须使用getFragmentTag方法。没有办法得到它的实例吗?我点击的是视图的容器,它们之间一定有连接,我可以使用。谢谢@Pinhassi,但如果你看看昨天提供的代码a作为答案,那正是我所做的。但再次感谢你的帮助和哈格·萨米亚:)