通知が届いている間にリスト項目の背景がハイライト表示されるリストビューがありますが、リストをスクロールすると項目が強調表示されるという問題があります。最初にスクロールしたときに最初のアイテムがリストされ、別のアイテムがハイライト表示されたとします。私はnotifysetdatachangedも使用しました。リストアクティビティのためのアンドロイドでスクロールしながらリストビュー項目が変わる
コード
public class NewPageActivity extends Fragment{
// notifcationは、Webサービスとセットアダプタ
を介してデータを取得するときのため@Override
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(
"unique_name");
mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//extract our message from intent
String msg_for_me = intent.getStringExtra("message");
//log our message value
Log.e("TAG", "onReceive: boradcast" + msg_for_me);
if (msg_for_me.contains("&")) {
String msg1[] = msg_for_me.split("&");
if (msg1[0].contains("Waiting for acceptance...")) {
edit.putInt("newTabHighlight", sp.getInt("newTabHighlight", 0) + 1);
edit.putString("newOrder", sp.getString("newOrder", null) + "&" + msg1[1].split("=")[1]);
edit.putInt("reminderTab", sp.getInt("reminderTab", 0) + 1);
edit.putInt("newOrderCount", 1);
edit.commit();
connectToDatabase();
}
}
//コードを到着したときに、これは私の通知コードです
if(getActivity()!= null) {
adapter = new customadapetr_new(dataModels, getActivity(), NewPageActivity.this);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
setListViewHeightBasedOnChildren(list);
}
}
//バックグラウンドhighl ights
private void setNewCardBackground(final String Order_id, final RelativeLayout MenuCard, String OrderFlag)
{
Log.e("TAG", "setNewCardBackground: ");
newOrder = sp.getString("newOrder", "null");
if (newOrder.contains("&")) {
newOrderArray = newOrder.split("&");
Log.e("TAG", "setNewCardBackground:order "+newOrderArray);
for (int i = 0; i < newOrderArray.length; i++) {
if (Order_id.equals(newOrderArray[i])) {
MenuCard.setBackgroundResource(R.drawable.highlightedorder);
MenuCard.setPadding(20,10,20,10);
newOrderlength++;
break;
}
//カスタムアダプタのgetビューコード
@Override
public View getView(final int position, View vv, ViewGroup parent) {
menuModels= new ArrayList<>();
final ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
final DataModel data=dataSet.get(position);
if (vv == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(mContext);
vv = inflater.inflate(R.layout.adapter_listview_newpage_card, parent, false);
viewHolder.rupee=(TextView)vv.findViewById(R.id.rupee);
viewHolder.dateandtime=(TextView)vv.findViewById(R.id.dateandtime);
viewHolder.modeofpayment=(TextView)vv.findViewById(R.id.modeofpayment);
viewHolder.location=(TextView)vv.findViewById(R.id.location);
viewHolder.customerName=(TextView)vv.findViewById(R.id.customerName);
viewHolder.orderId = (TextView)vv.findViewById(R.id.orderId);
viewHolder.order_time = (TextView) vv.findViewById(R.id.order_time);
viewHolder.menucard = (RelativeLayout) vv.findViewById(R.id.menucard);
viewHolder.accept = (ImageView) vv.findViewById(R.id.accept);
viewHolder.statustext = (TextView)vv.findViewById(R.id.statustext);
viewHolder.orderstatus = (TextView) vv.findViewById(R.id.orderstatus);
viewHolder.order_card_layout =(RelativeLayout) vv.findViewById(R.id.order_card_layout);
viewHolder.progressBar = (PlayGifView) vv.findViewById(R.id.progressBar);
viewHolder.progressBar.setImageResource(R.drawable.renewedloader);
viewHolder.showhide =(ImageView) vv.findViewById(R.id.showhide);
viewHolder.comments = (TextView) vv.findViewById(R.id.comments);
viewHolder.expanded_menulist =(ListView) vv.findViewById(R.id.menulist1);
viewHolder.expanded_menulist.setVisibility(View.GONE);
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
height = metrics.heightPixels;
width = metrics.widthPixels;
sp =mContext.getSharedPreferences("afewtaps", Context.MODE_PRIVATE);
face = Typeface.createFromAsset(mContext.getAssets(), "fonts/Nunito-Regular.ttf");
face1 = Typeface.createFromAsset(mContext.getAssets(), "fonts/Rupee_Foradian_2.ttf");
int size = dataSet.size();
String menu=data.getMenu();
viewHolder.listview=(ListView)vv.findViewById(R.id.menulist);
setListViewHeightBasedOnChildren(viewHolder.listview);
String paynt = data.getPaymemt_method();
final String order_id = data.getOrder_id();
final String Order_flag = data.getOrder_flag();
setNewCardBackground(order_id, viewHolder.menucard,Order_flag);
String orderEscalation = data.getOrder_escalated();
setEscalation(viewHolder.statustext, viewHolder.accept, viewHolder.orderstatus, order_id, orderEscalation);
int time = Integer.parseInt(data.getTimer_time());
setTime(time, viewHolder.order_time);
// menucard item
comment_value = data.getOrder_comments();
viewHolder.accept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeListItem(viewHolder.order_card_layout, position);
objNewPageActivity.SendOrder(data.getOrder_id());
}
});
viewHolder.customerName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCustomerProfile(order_id, mContext);
}
});
try {
JSONArray jsonArray1 = new JSONArray(menu);
String orderMenu[][] = new String[jsonArray1.length()][4];
int j;
for (j = 0; j < jsonArray1.length(); j++) {
menumodel = new MenuItemModel();
JSONObject ob2 = jsonArray1.getJSONObject(j);
// String menu_name, menu_qty, menu_price;
String menu_veg_type = ob2.getString("type");
menumodel.setMenuitem_type(menu_veg_type);
String menu_name = ob2.getString("item");
menumodel.setMenuitem_name(menu_name);
String menu_qty = ob2.getString("qty");
menumodel.setMenuitem_qty(menu_qty);
String menu_price = ob2.getString("price");
menumodel.setMenuitem_price(menu_price);
menuModels.add(menumodel);
}
viewHolder.listview.setAdapter(new MyAdapter(mContext,menuModels));
setListViewHeightBasedOnChildren(viewHolder.listview);
viewHolder.expanded_menulist.setAdapter(new Expanded_Menu_Adapter(mContext,menuModels));
//setListViewHeightBasedOnChildren(viewHolder.expanded_menulist);
}catch (Exception e)
{
}
int menuArrayLenghth = menuModels.size();
if(menuArrayLenghth > 2)
{
viewHolder.showhide.setVisibility(View.VISIBLE);
}
else
{
viewHolder.showhide.setVisibility(View.GONE);
}
result=vv;
vv.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) vv.getTag();
result=vv;
}
lastPosition = position;
viewHolder.dateandtime.setText(data.getOrder_time());
viewHolder.modeofpayment.setText(data.getPaymemt_method());
viewHolder.location.setText(data.getCustomer_location());
// amount
String amounttext = "` " + data.getPrice();
SpannableStringBuilder mySpannable = new SpannableStringBuilder(amounttext);
mySpannable.setSpan(new CustomTypefaceSpan("", face1), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
mySpannable.setSpan(new CustomTypefaceSpan("", face), 2, data.getPrice().length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
viewHolder.rupee.setText(mySpannable);
viewHolder.customerName.setText(data.getCustomer_name());
viewHolder.orderId.setText(data.getOrder_id());
if(!(comment_value.equals("0"))){
viewHolder.comments.setVisibility(View.VISIBLE);
viewHolder.comments.setText(data.getOrder_comments());
}
else
{
viewHolder.comments.setVisibility(View.GONE);
}
viewHolder.showhide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(viewHolder.listview.getVisibility() == View.VISIBLE)
{
viewHolder.expanded_menulist.setVisibility(View.VISIBLE);
viewHolder.listview.setVisibility(View.GONE);
viewHolder.showhide.setImageResource(R.drawable.blackarrrowup);
if(!(comment_value.equals("0"))){
viewHolder.comments.setVisibility(View.VISIBLE);
viewHolder.comments.setText(data.getOrder_comments());
}
else
{
viewHolder.comments.setVisibility(View.GONE);
}
}
else if(viewHolder.expanded_menulist.getVisibility() == View.VISIBLE) {
viewHolder.expanded_menulist.setVisibility(View.GONE);
viewHolder.listview.setVisibility(View.VISIBLE);
viewHolder.showhide.setImageResource(R.drawable.blackarrow);
viewHolder.comments.setVisibility(View.GONE);
}
}
});
return vv;
}
[ListView Changes on Scroll](http://stackoverflow.com/questions/34523053/listview-changes-data-on-scroll) –
アダプターの 'getView'コードを投稿してください。 –
getview checkの最後の部分@junaidhafeez –