【问题标题】:Android: Accessing View object in a Fragment classAndroid:在 Fragment 类中访问 View 对象
【发布时间】:2014-12-26 11:05:43
【问题描述】:

如何从另一个方法访问在 Fragment 类的 onCreateView 方法创建的 View 对象?我需要访问视图才能引用我的布局的 Gridview 控件。

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

}

【问题讨论】:

  • 当您发布的代码出现在您的个人代码中不是您引以为豪的地方时,请重新格式化以匹配。如果上面的代码没有让你觉得格式不正确;请学会一致地格式化您的代码 - 为世界着想。
  • 对不起,如果我不了解,但我的格式有什么问题? @ChiefTwoPencils 非常清楚,任何人都可以理解,并且我确保我没有包含任何不必要的方法,以免混淆任何人:)
  • 您的缩进至少应该匹配。大部分格式都是个人风格的,有些则不是。您的编辑让您非常接近,通常这是一个副本/过去的问题,但仍然需要注意。最好寻找有关该主题的资源。

标签: java android gridview android-fragments view


【解决方案1】:

将视图保存到类变量中。


私人视图;
……
......
@覆盖 onCreateView (LayoutInflater inflater, ViewGroup 容器, Bundle savedInstanceState){
view = inflater.inflate(你的布局)

}

【讨论】:

    【解决方案2】:

    全局声明它而不是局部变量

    【讨论】:

      【解决方案3】:

      在 onViewCreated 中存储 onCreateView 之后的视图引用:

      @Override
      public void onViewCreated(View view, Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
          localGridView = (GridView) view.findViewById(R.id.commentinglist);
      }
      

      【讨论】:

        【解决方案4】:
        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);
                    }
                 } 
        

        【讨论】:

        • Private 不计算!格式化也无济于事!
        【解决方案5】:

        如果您不想全局存储视图(出于某种原因),您可以随时执行类似的操作

         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 中膨胀的根布局

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多