I've got a custom listview inside a fragment that inflates a PopupWindow
whenever an message is clicked.
However I can't seem to figure out why the buttons in the list-view header are using a different theme/style than the normal rows in the listview. Can someone please care to explain?
popup = new PopupWindow(inflater.inflate(R.layout.message_tool, null, false));
popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setOutsideTouchable(true);
Rect location = locateView(v);
popup.showAtLocation(v, Gravity.NO_GRAVITY, location.left, location.top - 200);
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final LinearLayout view = (LinearLayout) inflater.inflate(R.layout.fragment_question, container, false);
final ListView messages = (ListView)view.findViewById(R.id.messages);
messages.setDivider(null);
// Add question (header)
View headerView = inflater.inflate(R.layout.messages_question, null);
MessageModel messageItem = new MessageModel();
messageItem.message = question.getQuestion();
messageItem.id = question.getId();
messageItem.gender = question.getGender();
messageItem.name = question.getName();
LinearLayout messageBackground = (LinearLayout)headerView.findViewById(R.id.messageBackground);
TextView message = (TextView)headerView.findViewById(R.id.question);
int bgDrawable = (messageItem.gender > 1) ? R.drawable.bubble_red_left_states : R.drawable.bubble_blue_left_states;
messageBackground.setBackground(ContextCompat.getDrawable(getActivity().getApplicationContext(), bgDrawable));
messages.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
SingleInstancePopup.GetInstance().hidePopup();
}
});
messageBackground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(getClass().getName(), "Clicked on question");
SingleInstancePopup popup = SingleInstancePopup.GetInstance();
if(popup.getPopup() != null) {
popup.hidePopup();
} else {
popup.showPopup(inflater, v);
}
}
});
message.setText(messageItem.message);
messages.addHeaderView(headerView, null, false);
// Add footer
View footerView = inflater.inflate(R.layout.messages_footer, null);
messages.addFooterView(footerView, null, false);
messagesAdapter = new MessagesAdapter(getActivity().getApplicationContext());
messages.setAdapter(messagesAdapter);
final Handler handler_ = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == UPDATE_UI) {
QuestionModel question = (QuestionModel) msg.obj;
for (AnswerModel answer : question.getAnswers()) {
MessageModel messageItem = new MessageModel();
messageItem.message = answer.getComment();
messageItem.id = answer.getId();
messageItem.gender = answer.getGender();
messageItem.name = answer.getName();
messageItem.reply = (answer.getParentId() > 0);
messagesAdapter.add(messageItem);
}
ProgressBar progress = (ProgressBar) messages.findViewById(R.id.messagesProgress);
if (progress != null) {
progress.setVisibility(View.GONE);
}
messagesAdapter.notifyDataSetChanged();
}
}
};
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
QuestionModel q = QuestionModel.getById(question.getId());
handler_.sendMessage(Message.obtain(handler_, UPDATE_UI, q));
} catch (Exception e) {
Log.d(getClass().getName(), "Failed to load question", e);
}
}
});
thread.start();
return view;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final MessageModel messageItem = this.getItem(position);
View v;
int bgDrawable;
if(messageItem.reply) {
v = inflater.inflate(R.layout.messages_answer_reply, null, false);
bgDrawable = (messageItem.gender > 1) ? R.drawable.bubble_red_right_states : R.drawable.bubble_blue_right_states;
} else {
v = inflater.inflate(R.layout.messages_answer, null, false);
bgDrawable = (messageItem.gender > 1) ? R.drawable.bubble_red_left_states : R.drawable.bubble_blue_left_states;
}
LinearLayout messageBackground = (LinearLayout)v.findViewById(R.id.messageBackground);
TextView message = (TextView)v.findViewById(R.id.comment);
messageBackground.setBackground(ContextCompat.getDrawable(getContext(), bgDrawable));
message.setText(messageItem.message);
messageBackground.setClickable(true);
messageBackground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(getClass().getName(), "Pressing down on message");
SingleInstancePopup popup = SingleInstancePopup.GetInstance();
if(popup.getPopup() != null) {
popup.hidePopup();
} else {
popup.showPopup(inflater, v);
}
}
});
return v;
}
Thanks!
- Simon
In MessagesAdapter.java, inflater
doesn't have your activity's theme, that's why your buttons look different. Instead of getting the system inflater service, pass the activity's layout inflater to your adapter, like this:
QuestionFragment.java
messagesAdapter = new MessagesAdapter(getActivity().getApplicationContext(), getActivity().getLayoutInflater());
MessagesAdapter.java private LayoutInflater inflater;
public MessagesAdapter(Context context, LayoutInflater inflater) {
super(context, 0);
this.inflater = inflater;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
final MessageModel messageItem = this.getItem(position);
View v;
int bgDrawable;
if (messageItem.reply) {
v = inflater.inflate(R.layout.messages_answer_reply, null, false);
bgDrawable = (messageItem.gender > 1) ? R.drawable.bubble_red_right_states : R.drawable.bubble_blue_right_states;
} else {
v = inflater.inflate(R.layout.messages_answer, null, false);
bgDrawable = (messageItem.gender > 1) ? R.drawable.bubble_red_left_states : R.drawable.bubble_blue_left_states;
}
// .......
return v;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With