gpt4 book ai didi

java - ListView 的第一项在不应该显示的情况下显示弹出菜单

转载 作者:行者123 更新时间:2023-12-02 02:04:47 24 4
gpt4 key购买 nike

所以,我发现了这个奇怪的行为 ListView ,当满足特定条件时应用弹出菜单(在这种情况下,如果批准为“未选中”)

ListView 上的所有项目的行为都符合我的预期,除了当批准值为“接受”或“拒绝”时的第一个项目。

所以,这是我的代码。
订单.java

package com.example.listview;

public class Order {
String name;
String approval;

public Order(String name, String approval) {
this.name = name;
this.approval = approval;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getApproval() {
return approval;
}

public void setApproval(String approval) {
this.approval = approval;
}
}

OrderAdapter.java

package com.example.listview;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class OrderAdapter extends BaseAdapter {
Activity activity;
List<Order> orders;
LayoutInflater inflater;

public OrderAdapter(Activity activity, List<Order> orders) {
this.activity = activity;
this.orders = orders;

inflater = activity.getLayoutInflater();
}

@Override
public int getCount() {
return orders.size();
}

@Override
public Object getItem(int i) {
return i;
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

ViewHolder holder = null;

if (view == null){

view = inflater.inflate(R.layout.lv_order_item, viewGroup, false);

holder = new ViewHolder();

holder.tvName = view.findViewById(R.id.tv_name);
holder.tvApproval = view.findViewById(R.id.tv_approval);
holder.rlPopUpMenu = view.findViewById(R.id.rl_popUpMenu);

view.setTag(holder);
}else
holder = (ViewHolder)view.getTag();

final Order model = orders.get(i);

holder.tvName.setText(model.getName());
holder.tvApproval.setText(model.getApproval());
holder.tvApproval.setTextColor(Color.parseColor("#D0CACA"));

// Condition For PopUp Menu
if(model.getApproval().equals("Unchecked")) {
// Menu Popup
holder.rlPopUpMenu.setVisibility(View.VISIBLE);
holder.rlPopUpMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(view.getContext(), view);
popup.getMenuInflater().inflate(R.menu.order_item_popup_menu, popup.getMenu());

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {

if(menuItem.getItemId() == R.id.action_accept) {
Toast.makeText(activity, "You Click Accept", Toast.LENGTH_SHORT).show();
}


if(menuItem.getItemId() == R.id.action_denied) {
Toast.makeText(activity, "You Click Denied", Toast.LENGTH_SHORT).show();
}

return true;
}
});
popup.show();
}
});


} else if(model.getApproval().equals("Accept")) {
holder.tvApproval.setTextColor(Color.parseColor("#32FF84"));
} else if(model.getApproval().equals("Denied")) {
holder.tvApproval.setTextColor(Color.parseColor("#1F9999"));
}

return view;
}

class ViewHolder{
TextView tvName;
TextView tvApproval;
RelativeLayout rlPopUpMenu;
}

}

OrderActivity.java

package com.example.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ScrollView;

import java.util.ArrayList;
import java.util.List;

public class OrderActivity extends AppCompatActivity {
List<Order> orders;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);

getSupportActionBar().setTitle("List View");

final ScrollView scrollView = findViewById(R.id.order_scrollview);
// To Scroll to the Top when Opening The Activity
scrollView.smoothScrollTo(0, 0);

final NonScrollListView non_scroll_list = (NonScrollListView) findViewById(R.id.lv_order);

orders = new ArrayList<>();
orders.add(new Order("MikeA", "Accept"));
orders.add(new Order("MikeB", "Accept"));
orders.add(new Order("MikeC", "Unchecked"));
orders.add(new Order("MikeD", "Denied"));
orders.add(new Order("MikeE", "Unchecked"));
orders.add(new Order("MikeF", "Unchecked"));
orders.add(new Order("MikeG", "Unchecked"));
orders.add(new Order("MikeH", "Unchecked"));
orders.add(new Order("MikeI", "Accept"));
orders.add(new Order("MikeJ", "Unchecked"));
orders.add(new Order("MikeK", "Unchecked"));
orders.add(new Order("MikeL", "Unchecked"));

final OrderAdapter adapter = new OrderAdapter(this, orders);
non_scroll_list.setAdapter(adapter);
}
}

NonScrollListView.java

package com.example.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;

public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}

文件夹布局

activity_order.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/order_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fillViewport="true"
android:background="#F2F2F2"
tools:context="com.example.listview.OrderActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="12dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F2F2F2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#F2F2F2"
android:orientation="vertical">
<com.example.listview.NonScrollListView
android:id="@+id/lv_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent"
android:background="#FFFFFF">
</com.example.listview.NonScrollListView>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>

lv_order_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:paddingBottom="13dp"
android:background="#991F1F">

<RelativeLayout
android:id="@+id/rl_popUpMenu"
android:layout_width="70dp"
android:layout_height="40dp"
android:gravity="center"
android:paddingLeft="30dp"
android:visibility="gone"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
<ImageView
android:layout_width="4dp"
android:layout_height="18dp"
android:src="@drawable/three_dots_order"/>
</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#fff"/>
<TextView
android:id="@+id/tv_approval"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#fff"/>
</LinearLayout>

</RelativeLayout>

文件夹菜单
order_item_popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_accept"
android:title="Accept"/>
<item
android:id="@+id/action_denied"
android:title="Denied"/>
</menu>

这是列表的图片
ListView Image

如您所见,MikeA 的批准值为“接受”,
因此弹出菜单(三个点)不应出现。
那么,有什么我想念的吗?

最佳答案

用此替换您的代码

 if(model.getApproval().equals("Unchecked")) {
// Menu Popup
holder.rlPopUpMenu.setVisibility(View.VISIBLE);
holder.rlPopUpMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(view.getContext(), view);
popup.getMenuInflater().inflate(R.menu.order_item_popup_menu, popup.getMenu());

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {

if(menuItem.getItemId() == R.id.action_accept) {
Toast.makeText(activity, "You Click Accept", Toast.LENGTH_SHORT).show();
}


if(menuItem.getItemId() == R.id.action_denied) {
Toast.makeText(activity, "You Click Denied", Toast.LENGTH_SHORT).show();
}

return true;
}
});
popup.show();
}
});


} else if(model.getApproval().equals("Accept")) {
holder.rlPopUpMenu.setVisibility(View.GONE);
holder.tvApproval.setTextColor(Color.parseColor("#32FF84"));
} else if(model.getApproval().equals("Denied")) {
holder.rlPopUpMenu.setVisibility(View.GONE);
holder.tvApproval.setTextColor(Color.parseColor("#1F9999"));
}

关于java - ListView 的第一项在不应该显示的情况下显示弹出菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50983252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com