我正在动态创建RelativeLayouts并将它们添加到另一个布局。我希望能够从我正在创建的这些布局中捕获点击事件。我已经把测试放在一起,但似乎无法让这个工作。
这是创建布局的Activity:
public class StatusActivity extends Activity implements OnClickListener
{
private ArrayList<AvatarItem> _buttonList = new ArrayList<AvatarItem>();
private TweeterMe _app;
private Twitter _twitter;
private RelativeLayout _tweetViewer;
private TextView _tweetField;
private ArrayList<Status>_statii;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
_app = (TweeterMe)getApplication();
_twitter = _app.getTwitter();
setContentView(R.layout.main);
_tweetViewer = (RelativeLayout) findViewById(R.id.tweet_view);
_tweetField = (TextView) findViewById(R.id.tweet_text_view);
_tweetViewer.setVisibility(View.INVISIBLE);
_tweetViewer.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
_tweetViewer.setVisibility(View.INVISIBLE);
}
});
}
@Override
protected void onResume()
{
super.onResume();
if (!_app.isAuthorized())
{
authorize();
}
else
{
loadTweets();
}
}
private void loadTweets()
{
try
{
_statii = _twitter.getHomeTimeline();
RelativeLayout layout = (RelativeLayout)findViewById(R.id.main_view);
Display display = getWindowManager().getDefaultDisplay();
Point middle = new Point(display.getWidth() / 2, display.getHeight() / 2);
for (Status s : _statii)
{
AvatarItem ai = new AvatarItem(this, null);
_buttonList.add(ai);
ai.setStatus(s);
ai.setLocation((int)(Math.random() * (display.getWidth() - 100)), (int)(Math.random() * (display.getHeight() - 150)), 500, middle);
layout.addView(ai);
ai.setOnClickListener(this);
}
}
catch(TwitterException e)
{
}
}
private void authorize()
{
Intent intent = new Intent(this, AuthorizeActivity.class);
startActivity(intent);
}
public void onClick(View v)
{
Log.d("TEST", "NOT WORKING");
}
}
这是我正在创建的布局:
public class AvatarItem extends RelativeLayout implements LoadImageAsyncTaskResponder
{
private Status _status;
private ImageButton _avatar;
private AsyncTask<URL, Void, Drawable> _imageLoadTask;
private TranslateAnimation _anim;
public AvatarItem(Context context, AttributeSet attrs)
{
super(context, attrs);
setVisibility(View.INVISIBLE);
_avatar = new ImageButton(getContext());
addView(_avatar);
}
public void setStatus(Status status)
{
_status = status;
final User user = status.getUser();
if (_imageLoadTask != null)
{
_imageLoadTask.cancel(true);
}
_imageLoadTask = new LoadImageAsyncTask(this).execute(user.getProfileImageURL());
}
public void setLocation(int x, int y, int duration, Point middle)
{
_anim = new TranslateAnimation(middle.x, x, middle.y, y);
_anim.setDuration(duration);
_anim.setFillEnabled(true);
_anim.setFillAfter(true);
}
public Status getStatus()
{
return _status;
}
public ImageButton getButton()
{
return _avatar;
}
public void imageLoadCancelled()
{
// Don't care right now
}
public void imageLoaded(Drawable drawable)
{
setVisibility(View.VISIBLE);
_avatar.setImageDrawable(drawable);
startAnimation(_anim);
}
public void imageLoading()
{
_avatar.setImageDrawable(null);
}
}
这是布局xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:id="@+id/main_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<RelativeLayout
android:id="@+id/tweet_view"
android:layout_width="fill_parent"
android:layout_margin="50px"
android:layout_height="150px"
android:background="#c5c5c5"
android:layout_centerInParent="true">
<TextView
android:id="@+id/tweet_text_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
</RelativeLayout>
有什么想法吗?
谢谢!
答案 0 :(得分:0)
我相信你需要覆盖onclick。至少那是我在其他例子中看到的。并且确保在其他地方没有其他的onclick窃取事件。