2012-11-21 30 views
1

我真的在尝试一些非常简单的事情,不过android和java让我的生活变得不容易。这个想法是滚动到表格布局中的指定的孩子。tablelayout getchildat滚动到那个孩子

我使用的是Layoutinflater的条目添加到像表布局如下:

 TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout3); 

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View itemView = inflater.inflate(R.layout.element_news, null); 

    TextView firstname = (TextView) itemView.findViewById(R.id.tvname); 
    firstname.setText(FN + " " + iLN); 

    TextView date = (TextView) itemView.findViewById(R.id.tvdate); 
    date.setText(newPD); 

    TextView post = (TextView) itemView.findViewById(R.id.tvpost); 
    post.setText(c.getString(iP)); 
    post.setFocusable(true); 

    tl.addView(itemView, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

我试图用getChildAt()为tablelayout和我得到这个孩子,但是当我使用孩子返回一切为“0”的孩子。

例如代码:

LinearLayout row = (LinearLayout) tl.getChildAt(1); 
TextView tv = (TextView) row.getChildAt(1); 
tv.requestFocus(); 
Log.w("news", Integer.toString(tv.getHeight())); 

对于TextView的高度则返回“0”尽管它已经包含多个线和requestFocus()方法也不起作用。

那么如何滚动到表格布局中的孩子?

在此先感谢您的帮助。

回答

1

该代码示例不起作用,因为您可能在onCreate方法中使用它,并且此时UI尚未绘制,因此视图没有任何尺寸。如果是这样的话,你可以简单地张贴在您的视图之一Runnable推迟了一下尺寸聚集,直到onCreate方法是这样后:

// post the Runnable 
tl.post(new Runnable() { 

     @Override 
     public void run() { 
      LinearLayout row = (LinearLayout) tl.getChildAt(1); 
          // get the top position relative to the parent 
      int top = tr.getTop(); 
          // scroll to that top position the wrapping ScrollView 
      sv.scrollTo(0, top);     
     } 

    }); 

有一些奇怪的代码(从我的观点查看),如果上面的代码不起作用,你应该发布更多关于你的布局文件和使用场景的细节。

+0

非常感谢它为我工作。 – Max

+1

只要22小时过去了,我就会给你+50的声望点。 – Max

相关问题