因此,我发现这个奇怪的行为列表视图是在满足特定条件时(在这种情况下,如果批准为“未选中”,则应用弹出菜单)
除了第一个项目的批准值为“接受”或“拒绝”时,列表视图中的所有项目均按预期方式运行。
这是我的代码。
Order.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>
如您所见,MikeA的批准值为“接受”,
因此不应显示弹出菜单(三个点)。
所以,有什么我想念的吗?
答案 0 :(得分:2)
以此替换您的代码
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"));
}