1

嗨,我想使用此代码,它在 onclick 方法中工作,因为(查看 v)但是我如何在活动的其他地方使用此代码?

这是代码

        ListView list= getListView();
        int position = list.getPositionForView(v);
        switch (position) {
        case 1:
                ImageView arrow = (ImageView) getView().findViewById(R.id.arrow);
                arrow.setVisibility(View.VISIBLE);
            break;

        default:
            break;
        }

那么在这种情况下如何获取 View v 呢?

提前致谢!

4

1 回答 1

0

我遇到过同样的问题。我不确定这种方式是否是最好的方式,但对我来说效果很好。创建所有视图后,创建监听 器 onLayoutChangeListener并将您想要为项目视图实现的逻辑放在您想要的特定位置。

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    setOnLayoutChangeListener(getListView());

    super.onViewCreated(view, savedInstanceState);
}


private void setOnLayoutChangeListener(final ListView listview) {
    if (listview != null) {
	listview.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

	   @Override
	   public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

	       changeFirstListItemView(listview);

	   }
       });
    }
}

private void changeFirstListItemView(final ListView listview) {
   if (listview != null) {
	try {
	   int listFirstPosition = 1;
	   View view = listview.getChildAt(listFirstPosition);

	   if (view != null) {
		   setHiddenViewVisible(view);
	   }

	} catch (Exception e) {
		// Print some debug info.
		String msg = "An error occurred while trying to set onFocusChange listener.";
		Log.e(TAG, msg, e);
	}
      }
   }

private void setHiddenButtonVisible(View listItem) {
    Button button = null;
	try {
	   buttonId = R.id.listButton;
	   button = (Button) listItem.findViewById(buttonId);
	} catch (Resources.NotFoundException nfe) {
           // Print some debug info.
	}

	if (button != null) {
	   button.setVisibility(View.VISIBLE);
           // Optional, add here button listener, or something else...
        }
}

于 2017-01-29T18:39:00.043 回答