我有一个ScrollView,我正在动态添加自定义视图。 ScrollView正在onClick事件中显示和填充。我试过调用invalidate(),postInvalidate()和requestLayout(),但没有做任何事情。这是第一次显示和填充ScrollView。如果我隐藏,清空重新填充,然后再次显示ScrollView,我将调用所有自定义视图onDraw方法,而不是之前。在第一个种群上,我创建了所有对象,并且调用了构造函数和度量方法,但不是onDraw。第二次调用所有方法。任何帮助都会很棒。
这是我的自定义视图
package com.drivevelocity.android.Views;
import com.drivevelocity.android.Drive;
import com.drivevelocity.android.R;
import com.drivevelocity.android.Utils.Toaster;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class NotificationMessageView extends View {
private String mName,
mDate,
mTo,
mSubject,
mMessage;
private RectF mIconSize = new RectF(0, 0, 0, 0),
mBottomBorder = new RectF(0, 0, 0, 0);
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Context Context;
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
invalidate();
postInvalidate();
requestLayout();
}
public String getDate() {
return mDate;
}
public void setDate(String date) {
mDate = date;
invalidate();
postInvalidate();
requestLayout();
}
public String getTo() {
return mTo;
}
public void setTo(String to) {
mTo = to;
invalidate();
postInvalidate();
requestLayout();
}
public String getSubject() {
return mSubject;
}
public void setSubject(String subject) {
mSubject = subject.replace("<br />", " ").replace("<br/>", " ").replace("<br >", " ").replace("<br>", " ");
invalidate();
postInvalidate();
requestLayout();
}
public String getMessage() {
return mMessage;
}
public void setMessage(String message) {
mMessage = message.replace("<br />", " ").replace("<br/>", " ").replace("<br >", " ").replace("<br>", " ");
invalidate();
postInvalidate();
requestLayout();
}
public NotificationMessageView(Context c, AttributeSet a) {
super(c, a);
invalidate();
postInvalidate();
requestLayout();
}
public NotificationMessageView(Context context, String name, String date, String to, String subject, String message) {
super(context);
Context = context;
mName = name;
mDate = date;
mTo = to;
mSubject = subject;//.replace("<br />", " ").replace("<br/>", " ").replace("<br >", " ").replace("<br>", " ");
mMessage = message;//.replace("<br />", " ").replace("<br/>", " ").replace("<br >", " ").replace("<br>", " ");
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toaster.ToastUser("clicked " + mTo, Context);
}
});
invalidate();
postInvalidate();
requestLayout();
}
@Override
public void onDraw (Canvas canvas) {
mPaint.setTypeface(Drive.OpenSansBold);
mPaint.setTextSize(Drive.Resources.getDimension(R.dimen.home_tile_text));
mPaint.setColor(Drive.Resources.getColor(R.color.dropdown_messages_text));
mPaint.setTextSize(Drive.Resources.getDimension(R.dimen.dropdown_messages_text));
canvas.drawText(mName, 130, 70, mPaint);
mPaint.setTypeface(Drive.OpenSansLight);
canvas.drawText(mDate, 450, 70, mPaint);
canvas.drawText(mTo, 130, 110, mPaint);
mPaint.setTypeface(Drive.OpenSansBold);
canvas.drawText(mSubject, 130, 150, mPaint);
mPaint.setTypeface(Drive.OpenSansLight);
canvas.drawText(mMessage, 130, 190, mPaint);
int imageSize = Drive.Resources.getInteger(R.integer.dropdown_messages_image_size);
float x = Drive.Resources.getInteger(R.integer.dropdown_messages_image_padding_left);
float y = Drive.Resources.getInteger(R.integer.dropdown_messages_image_padding_top);
mIconSize.set(x, y, x + imageSize, y + imageSize);
canvas.drawBitmap(((BitmapDrawable)Drive.Resources.getDrawable(R.drawable.dropdown_runner)).getBitmap(), null, mIconSize, mPaint);
mBottomBorder.set(0, 219, getWidth(), 220);
canvas.drawRect(mBottomBorder, mPaint);
}
@Override
public void onMeasure(int x, int y) {
super.onMeasure(x, y);
LinearLayout p = (LinearLayout)getParent();
int width = 1000;
if (p != null) {
ScrollView l = (ScrollView)p.getParent();
width = l.getWidth();
}
setMeasuredDimension(width, 220);
}
}
这是视图的某些部分正在使用它(我将一些不相关的部分剪掉以缩小尺寸)。
package com.drivevelocity.android;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;
import com.drivevelocity.android.Models.NotificationAlert;
import com.drivevelocity.android.Models.NotificationGrabIt;
import com.drivevelocity.android.Models.NotificationInStore;
import com.drivevelocity.android.Models.NotificationMessage;
import com.drivevelocity.android.Models.Notifications;
import com.drivevelocity.android.Models.Tile;
import com.drivevelocity.android.Models.User;
import com.drivevelocity.android.Networking.Json;
import com.drivevelocity.android.Utils.JsonArray;
import com.drivevelocity.android.Utils.Settings;
import com.drivevelocity.android.Utils.Toaster;
import com.drivevelocity.android.Views.NotificationMessageView;
import com.drivevelocity.android.Views.TileView;
public class HomeActivity extends Activity {
public static List<TileView> TileViews;
public static Settings Settings;
public static List<Tile> TileBoardCounts;
public static String Today = new SimpleDateFormat("MM/dd/yyyy", Locale.US).format(new Date());
public static List<NotificationGrabIt> GrabIts;
public static List<NotificationMessage> Messages;
public static List<NotificationInStore> Peeps;
public static List<NotificationAlert> Alerts;
public static Boolean SlidingMenuShown = false,
DropdownShown = false;
public static LinearLayout SlidingMenu,
HomeDropdownLayout,
HomeTileBoardLayout,
DropdownList;
public static TextView DropdownTitle;
public static ImageView ToolbarMenuImageButton,
ToolbarGrabItsImageButton,
ToolbarMessagesImageButton,
ToolbarInStoreImageButton,
ToolbarAlertsImageButton,
DropdownGrabItsArrow,
DropdownMessagesArrow,
DropdownInStoresArrow,
DropdownAlertsArrow;
public static ToggleButton OpportunitiesNotificationsOn,
OpportunitiesNotificationsOff,
MessagesNotificationsOn,
MessagesNotificationsOff,
InStoresNotificationsOn,
InStoresNotificationsOff,
NotificationsOn,
NotificationsOff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setFields();
setTypefaces();
setEventHandlers();
new NotificationTask().execute((Void) null);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
private void resetDropdown() {
HomeDropdownLayout.setVisibility(View.GONE);
DropdownGrabItsArrow.setVisibility(View.GONE);
DropdownInStoresArrow.setVisibility(View.GONE);
DropdownInStoresArrow.setVisibility(View.GONE);
DropdownAlertsArrow.setVisibility(View.GONE);
DropdownTitle.setText("");
DropdownShown = false;
}
private void setFields() {
ToolbarMenuImageButton = (ImageView)findViewById(R.id.menu_image_button);
ToolbarGrabItsImageButton = (ImageView)findViewById(R.id.grabit_image_button);
ToolbarMessagesImageButton = (ImageView)findViewById(R.id.message_image_button);
ToolbarInStoreImageButton = (ImageView)findViewById(R.id.peeps_image_button);
ToolbarAlertsImageButton = (ImageView)findViewById(R.id.alert_image_button);
DropdownGrabItsArrow = (ImageView)findViewById(R.id.home_dropdown_grabits_arrow);
DropdownMessagesArrow = (ImageView)findViewById(R.id.home_dropdown_messages_arrow);
DropdownInStoresArrow = (ImageView)findViewById(R.id.home_dropdown_peeps_arrow);
DropdownAlertsArrow = (ImageView)findViewById(R.id.home_dropdown_alerts_arrow);
HomeDropdownLayout = (LinearLayout)findViewById(R.id.home_dropdown_layout);
DropdownList = (LinearLayout)findViewById(R.id.home_dropdown_items);
DropdownTitle = (TextView)findViewById(R.id.home_dropdown_items_title);
}
private void setTypefaces() {
DropdownTitle.setTypeface(Drive.OpenSansLight);
}
private void setEventHandlers() {
HomeDropdownLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
ToolbarGrabItsImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (DropdownShown && DropdownTitle.getText().equals("Grabits")) {
HomeDropdownLayout.callOnClick();
} else {
HomeDropdownLayout.setVisibility(View.VISIBLE);
DropdownMessagesArrow.setVisibility(View.INVISIBLE);
DropdownInStoresArrow.setVisibility(View.INVISIBLE);
DropdownAlertsArrow.setVisibility(View.INVISIBLE);
DropdownGrabItsArrow.setVisibility(View.VISIBLE);
DropdownTitle.setText("Grabits");
DropdownShown = true;
populateGrabits();
}
}
});
ToolbarMessagesImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (DropdownShown && DropdownTitle.getText().equals("Messages")) {
HomeDropdownLayout.callOnClick();
} else {
populateMessages();
HomeDropdownLayout.setVisibility(View.VISIBLE);
DropdownGrabItsArrow.setVisibility(View.INVISIBLE);
DropdownInStoresArrow.setVisibility(View.INVISIBLE);
DropdownAlertsArrow.setVisibility(View.INVISIBLE);
DropdownMessagesArrow.setVisibility(View.VISIBLE);
DropdownTitle.setText("Messages");
DropdownShown = true;
}
}
});
ToolbarInStoreImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (DropdownShown && DropdownTitle.getText().equals("In Stores")) {
HomeDropdownLayout.callOnClick();
} else {
HomeDropdownLayout.setVisibility(View.VISIBLE);
DropdownGrabItsArrow.setVisibility(View.INVISIBLE);
DropdownMessagesArrow.setVisibility(View.INVISIBLE);
DropdownAlertsArrow.setVisibility(View.INVISIBLE);
DropdownInStoresArrow.setVisibility(View.VISIBLE);
DropdownTitle.setText("In Stores");
DropdownShown = true;
populatePeeps();
}
}
});
ToolbarAlertsImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (DropdownShown && DropdownTitle.getText().equals("Alerts")) {
HomeDropdownLayout.callOnClick();
} else {
HomeDropdownLayout.setVisibility(View.VISIBLE);
DropdownGrabItsArrow.setVisibility(View.INVISIBLE);
DropdownMessagesArrow.setVisibility(View.INVISIBLE);
DropdownInStoresArrow.setVisibility(View.INVISIBLE);
DropdownAlertsArrow.setVisibility(View.VISIBLE);
DropdownTitle.setText("Alerts");
DropdownShown = true;
populateAlerts();
}
}
});
}
private void populateGrabits() { // Just doing the same thing in all of these populate methods untill I get the message one working.
DropdownList.removeAllViews();
for (NotificationMessage message : Messages) {
DropdownList.addView(new NotificationMessageView(HomeActivity.this, message.CustomerFirstName + " " + message.CustomerLastName, message.CrumbDateCreated, message.To, message.Subject, message.Message));
}
}
private void populateMessages() {
DropdownList.removeAllViews();
for (NotificationMessage message : Messages) {
DropdownList.addView(new NotificationMessageView(HomeActivity.this, message.CustomerFirstName + " " + message.CustomerLastName, message.CrumbDateCreated, message.To, message.Subject, message.Message));
}
}
private void populatePeeps() { // Just doing the same thing in all of these populate methods untill I get the message one working.
DropdownList.removeAllViews();
for (NotificationMessage message : Messages) {
DropdownList.addView(new NotificationMessageView(HomeActivity.this, message.CustomerFirstName + " " + message.CustomerLastName, message.CrumbDateCreated, message.To, message.Subject, message.Message));
}
}
private void populateAlerts() { // Just doing the same thing in all of these populate methods untill I get the message one working.
DropdownList.removeAllViews();
for (NotificationMessage message : Messages) {
DropdownList.addView(new NotificationMessageView(HomeActivity.this, message.CustomerFirstName + " " + message.CustomerLastName, message.CrumbDateCreated, message.To, message.Subject, message.Message));
}
}
public class NotificationTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
Map<String, String> urlParams = new HashMap<String, String>();
urlParams.put("RequestedByUserGUID", User.GUID);
urlParams.put("StoreID", Integer.toString(User.StoreID, 10));
urlParams.put("Limit", "100");
urlParams.put("NewCode", "1");
Notifications notifications = new Notifications(Json.GetJSONObject("Notifications", urlParams));
GrabIts = notifications.GrabIts;
Messages = notifications.Messages;
Peeps = notifications.InStores;
Alerts = notifications.Alerts;
return true;
} catch (JSONException e) {
Log.v("HomeActivity", e.getMessage(), e);
Toaster.ToastUser(e.getMessage(), HomeActivity.this);
e.printStackTrace();
return false;
} catch (IOException e) {
Log.v("HomeActivity", e.getMessage(), e);
Toaster.ToastUser(e.getMessage(), HomeActivity.this);
e.printStackTrace();
return false;
} catch (Exception e) {
Log.v("HomeActivity", e.getMessage(), e);
Toaster.ToastUser(e.getMessage(), HomeActivity.this);
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
try {
if (success) {
TextView grabItsTextView = (TextView)findViewById(R.id.grabit_text_view);
ImageView grabItsImageButton = (ImageView)findViewById(R.id.grabit_image_button);
TextView messagesTextView = (TextView)findViewById(R.id.mail_text_view);
ImageView messagesImageButton = (ImageView)findViewById(R.id.message_image_button);
TextView peepsTextView = (TextView)findViewById(R.id.peeps_text_view);
ImageView peepsImageButton = (ImageView)findViewById(R.id.peeps_image_button);
TextView alertsTextView = (TextView)findViewById(R.id.alert_text_view);
ImageView alertsImageButton = (ImageView)findViewById(R.id.alert_image_button);
grabItsTextView.setTypeface(Drive.OpenSansBold);
messagesTextView.setTypeface(Drive.OpenSansBold);
peepsTextView.setTypeface(Drive.OpenSansBold);
alertsTextView.setTypeface(Drive.OpenSansBold);
if (GrabIts.size() > 0) {
grabItsTextView.setVisibility(View.VISIBLE);
grabItsTextView.setText(Integer.toString(GrabIts.size(), 10));
grabItsImageButton.setImageDrawable(Drive.Resources.getDrawable(R.drawable.toolbar_grabit_active));
} else {
grabItsTextView.setText("");
grabItsTextView.setVisibility(View.GONE);
grabItsImageButton.setImageDrawable(Drive.Resources.getDrawable(R.drawable.toolbar_grabit));
}
if (Messages.size() > 0) {
messagesTextView.setVisibility(View.VISIBLE);
messagesTextView.setText(Integer.toString(Messages.size(), 10));
messagesImageButton.setImageDrawable(Drive.Resources.getDrawable(R.drawable.toolbar_mail_active));
} else {
messagesTextView.setText("");
messagesTextView.setVisibility(View.GONE);
messagesImageButton.setImageDrawable(Drive.Resources.getDrawable(R.drawable.toolbar_mail));
}
if (Peeps.size() > 0) {
peepsTextView.setVisibility(View.VISIBLE);
peepsTextView.setText(Integer.toString(Peeps.size(), 10));
peepsImageButton.setImageDrawable(Drive.Resources.getDrawable(R.drawable.toolbar_peeps_active));
} else {
peepsTextView.setText("");
peepsTextView.setVisibility(View.GONE);
peepsImageButton.setImageDrawable(Drive.Resources.getDrawable(R.drawable.toolbar_peeps));
}
if (Alerts.size() > 0) {
alertsTextView.setVisibility(View.VISIBLE);
alertsTextView.setText(Integer.toString(Alerts.size(), 10));
alertsImageButton.setImageDrawable(Drive.Resources.getDrawable(R.drawable.toolbar_alert_active));
} else {
alertsTextView.setText("");
alertsTextView.setVisibility(View.GONE);
alertsImageButton.setImageDrawable(Drive.Resources.getDrawable(R.drawable.toolbar_alert));
}
} else {
Toaster.ToastUser("Error getting notifications", HomeActivity.this);
}
} catch (Exception e) {
Toaster.ToastUser("Error getting notifications", HomeActivity.this);
Log.v("HomeActivity", e.getMessage(), e);
Toaster.ToastUser(e.getMessage(), HomeActivity.this);
e.printStackTrace();
}
}
}
}
这是视图(我将一些不相关的部分剪掉以缩小尺寸)。
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".HomeActivity" >
<LinearLayout
android:id="@+id/home_dropdown_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:orientation="vertical"
android:visibility="gone" >
<LinearLayout
android:id="@+id/home_dropdown_arrows_layout"
android:layout_width="match_parent"
android:layout_height="8dp" >
<ImageView
android:id="@+id/home_dropdown_left_empty_slot"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight="1"
android:contentDescription="@string/description"
android:visibility="invisible" />
<ImageView
android:id="@+id/home_dropdown_grabits_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight="1"
android:contentDescription="@string/description"
android:src="@drawable/dropdown_up_arrow"
android:visibility="invisible" />
<ImageView
android:id="@+id/home_dropdown_messages_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight="1"
android:contentDescription="@string/description"
android:src="@drawable/dropdown_up_arrow"
android:visibility="invisible" />
<ImageView
android:id="@+id/home_dropdown_peeps_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight="1"
android:contentDescription="@string/description"
android:src="@drawable/dropdown_up_arrow"
android:visibility="invisible" />
<ImageView
android:id="@+id/home_dropdown_alerts_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight="1"
android:contentDescription="@string/description"
android:src="@drawable/dropdown_up_arrow"
android:visibility="invisible" />
<ImageView
android:id="@+id/home_dropdown_right_empty_slot"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight="1"
android:contentDescription="@string/description"
android:visibility="invisible" />
</LinearLayout>
<TextView
android:id="@+id/home_dropdown_items_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#56616c"
android:padding="12dp"
android:textColor="#dedede"
android:textSize="20sp" />
<ScrollView
android:id="@+id/home_dropdown_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#13191f"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<LinearLayout
android:id="@+id/home_dropdown_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>