Java Android:访问片段类中的视图对象
如何从另一个方法访问片段类的onCreateView方法创建的视图对象?我需要访问视图才能引用布局的Gridview控件Java Android:访问片段类中的视图对象,java,android,gridview,android-fragments,view,Java,Android,Gridview,Android Fragments,View,如何从另一个方法访问片段类的onCreateView方法创建的视图对象?我需要访问视图才能引用布局的Gridview控件 public class comments_frag_activity extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
public class comments_frag_activity extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater.inflate(R.layout.comments_frag, container, false);
}
//other methods.....
//other methods .....
//other methods .....
public void printTest()
{
if(!commentsGallery.isEmpty())
{
//I cant access the view object 'android'
GridView list = (GridView)android.findViewById(R.id.commentinglist);
CommentsAdapter bA = new CommentsAdapter(ctx, R.layout.comments_frag, commentsGallery);
list.setAdapter(bA);
}
}
在onViewCreated中的onCreateView之后,将其全局声明为局部变量将其全局声明为局部变量存储视图的引用:
Try This
public class comments_frag_activity extends Fragment{
private View android;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
android = inflater.inflate(R.layout.comments_frag, container, false);
}
public void printTest()
{
if(!commentsGallery.isEmpty())
{
GridView list = (GridView)android.findViewById(R.id.commentinglist);
CommentsAdapter bA = new CommentsAdapter(ctx, R.layout.comments_frag, commentsGallery);
list.setAdapter(bA);
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
localGridView = (GridView) view.findViewById(R.id.commentinglist);
}
在onViewCreated中的onCreateView之后存储视图的引用:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
localGridView = (GridView) view.findViewById(R.id.commentinglist);
}
将视图保存到类变量
私人视野;
......
.....
@凌驾 onCreateView(布局充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(您的布局)
}将视图保存到类变量
私人视野;
......
.....
@凌驾 onCreateView(布局充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(您的布局)
}如果您不想全局存储视图(出于某种原因),您可以执行以下操作
public void printTest()
{
if(!commentsGallery.isEmpty())
{
//I cant access the view object 'android'
GridView list = (GridView) getView().findViewById(R.id.commentinglist);
CommentsAdapter bA = new CommentsAdapter(ctx, R.layout.comments_frag, commentsGallery);
list.setAdapter(bA);
}
}
getView()将返回onCreateView中的根布局。如果您不想全局存储视图(出于某种原因),您可以执行以下操作
public void printTest()
{
if(!commentsGallery.isEmpty())
{
//I cant access the view object 'android'
GridView list = (GridView) getView().findViewById(R.id.commentinglist);
CommentsAdapter bA = new CommentsAdapter(ctx, R.layout.comments_frag, commentsGallery);
list.setAdapter(bA);
}
}
getView()将返回onCreateView中膨胀的根布局
Private
不计算!格式化也没用<代码>专用不计算!格式化也没用!当您发布的代码出现在您的个人代码中时,请将其重新格式化以匹配。如果上面的代码没有让您觉得格式不正确;看在世界的份上,请学会一致地设置代码的格式。对不起,如果我不知道,那么我的格式有什么问题@Chieftwo Pencils这很清楚,任何人都能理解,我确保没有包含任何不必要的方法,以免混淆任何人:)你的缩进至少应该匹配。很多格式都是个人风格的,有些则不然。你的编辑让你非常接近,通常是一个拷贝/过去的问题,但仍然需要注意。最好是查找有关该主题的资源。当您发布的代码出现在您的个人代码中,而不是您引以为豪的代码时,请将其重新格式化以匹配。如果上面的代码没有让您觉得格式不正确;看在世界的份上,请学会一致地设置代码的格式。对不起,如果我不知道,那么我的格式有什么问题@Chieftwo Pencils这很清楚,任何人都能理解,我确保没有包含任何不必要的方法,以免混淆任何人:)你的缩进至少应该匹配。很多格式都是个人风格的,有些则不然。你的编辑让你非常接近,通常是一个拷贝/过去的问题,但仍然需要注意。最好在这个主题上寻找资源。