View Not Receiving Clicks
I am creating RelativeLayouts dynamically and adding them to another layout. I want to be able to catch click events from these layouts I'm creating. I've put a test together but cannot seem to get this working.
Here is the Activity which creates the layouts:
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");
}
}
Here is the layout I am creating:
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(Vie开发者_Go百科w.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);
}
}
Here is the layout xml file:
<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>
Any thoughts?
Thanks!
I believe you need to override onclick. At least that's what I'm seeing in other examples. And be sure there isn't another onclick somewhere else that's stealing the event.
精彩评论