开发者

how to get specific position xy coordinates from Linear Layout android?

Iam using below code in xml file and adding child views programmatically by using textview but i need to go specific position(suppose in every textview iam adding number start from 1 then i want to go 5th position textview from total of 10 textviews) on the view using scrollview.I knew the method to scroll but i dont know how to calculate the x y cooardinates from this layout.Please give guidance.

<ScrollView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:scrollbars="vertical"
        android:id="@+id/agenda_scroll"
        >
        <LinearLayout android:id="@+id/inner_layout"
            android:orientation="vertical" android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        </LinearLayout>
    </ScrollView>

Suppost my code is like this

ScrollView scview = (ScrollView)findViewById(R.id.Scl);
LinearLayout eventLayout = (LinearLayout) findViewById(R.id.inner_layout);
TextView txtView = new TextView(cx);
txtView.setText("1");
eventLayout.addView(txtview);
TextView txtView = new TextView(cx);
txtView.setText("2");
eventLayout.addView(txtview);
TextView txtView = new TextView(cx);
txtView.setText("3");
eventLayout.addView(txtview);
TextView txtView = new TextView(cx);
txtView.setText("4")开发者_C百科;
eventLayout.addView(txtview);
TextView txtView = new TextView(cx);
txtView.setText("5");

int y = txtview.getTop();
Log.d(TAG,"YAxis"+y);//returning 0


eventLayout.addView(txtview);
TextView txtView = new TextView(cx);
txtView.setText("6");
eventLayout.addView(txtview);
.........

scview .scrollTo(0,y);///here positions giving (0,0)


I know this is kind of old, but here's something that we've seen working. Every view has a ViewTreeObserver that can be used to observe the View and it's hierarchy. Get the ViewTreeObserver and listen for layout changes as follows. Once the view and it's hierarchy has been laid out, you can perform your size and position based queries.

onCreate(){
  //Code

  eventLayout.addView(txtview);
  TextView txtView = new TextView(cx);
  txtView.setText("6");
  eventLayout.addView(txtview)

  //More code

  //Observe for a layout change
  ViewTreeObserver viewTreeObserver = eventLayout.getViewTreeObserver();
  if (viewTreeObserver.isAlive()) {
    viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        //View and it's hierarchy laid out, perform your operations
        scview .scrollTo(0,y);///here positions giving (0,0)
      }
    });
  }
}


If you're trying to use View.getTop() in onCreate() you're getting 0 because the views are not yet laid out. First onCreate() ends its execution, then Android lays out things and draws them, then it waits for user events, and then you can get views positions.

I don't know a clean way to do something immediately after the whole activity has been laid out (an unclean way is to use onWindowFocusChanged()).


I vote for listview here but the other option you have is to store each textview in an arraylist when you create them and then loop it and find the height of each textview when you need to. Then scroll to the value of the sum or whatever your calculation is.


Thinking out of the box: why not using a custom ListView which is precisely made to contain a list of views and access them easily?

Also, have you tried running this code in the onResume function instead of the onCreate function?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜