ListView配合popmenu的使用

  项目中需要在一个Listview不仅有OnItemClicked还需要item中某一个按钮具有单独的点击事件。在安卓官网中 已经有了类似的实现

官网地址https://developer.android.com/samples/ActionBarCompat-ListPopupMenu/index.html

代码主要是在ListView的apdater中的getView中


/**
 * A simple array adapter that creates a list of cheeses.
 */
class PopupAdapter extends ArrayAdapter<String> {

    PopupAdapter(ArrayList<String> items) {
        super(getActivity(), R.layout.list_item, android.R.id.text1, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        // Let ArrayAdapter inflate the layout and set the text
        View view = super.getView(position, convertView, container);

        // BEGIN_INCLUDE(button_popup)
        // Retrieve the popup button from the inflated view
        View popupButton = view.findViewById(R.id.button_popup);

        // Set the item as the button's tag so it can be retrieved later
        popupButton.setTag(getItem(position));

        // Set the fragment instance as the OnClickListener
        popupButton.setOnClickListener(PopupListFragment.this);
        // END_INCLUDE(button_popup)

        // Finally return the view to be displayed
        return view;
    }
}
而后是点击事件的代码


@Override
public void onClick(final View view) {
    // We need to post a Runnable to show the popup to make sure that the PopupMenu is
    // correctly positioned. The reason being that the view may change position before the
    // PopupMenu is shown.
    view.post(new Runnable() {
        @Override
        public void run() {
            showPopupMenu(view);
        }
    });
}
// BEGIN_INCLUDE(show_popup)
private void showPopupMenu(View view) {
    final PopupAdapter adapter = (PopupAdapter) getListAdapter();

    // Retrieve the clicked item from view's tag
    final String item = (String) view.getTag();

    // Create a PopupMenu, giving it the clicked view for an anchor
    PopupMenu popup = new PopupMenu(getActivity(), view);

    // Inflate our menu resource into the PopupMenu's Menu
    popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

    // Set a listener so we are notified if a menu item is clicked
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.menu_remove:
                    // Remove the item from the adapter
                    adapter.remove(item);
                    return true;
            }
            return false;
        }
    });

    // Finally show the PopupMenu
    popup.show();
}
// END_INCLUDE(show_popup)
这样就实现了一个listview的一个条目不同点击事件

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 中使用两个 ListView 配合使用的方法如下: 1. 首先,在你的布局文件(例如 activity_main.xml)中添加两个 ListView 组件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/listView2" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> ``` 2. 在你的 Activity 类中,找到这两个 ListView 组件并设置它们的适配器: ```java public class MainActivity extends AppCompatActivity { private ListView listView1; private ListView listView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找到 ListView 组件 listView1 = findViewById(R.id.listView1); listView2 = findViewById(R.id.listView2); // 创建适配器对象 ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getData1()); ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getData2()); // 设置适配器 listView1.setAdapter(adapter1); listView2.setAdapter(adapter2); } // 获取第一个 ListView 的数据 private List<String> getData1() { List<String> data = new ArrayList<>(); // 添加数据到 data 列表中 return data; } // 获取第二个 ListView 的数据 private List<String> getData2() { List<String> data = new ArrayList<>(); // 添加数据到 data 列表中 return data; } } ``` 3. 在 `getData1()` 和 `getData2()` 方法中,你可以根据需要添加数据到两个 ListView 中。可以通过调用 `data.add("your data")` 将数据添加到列表中。 这样,你就可以在 Android Studio 中使用两个 ListView 组件,并根据需要设置它们的数据和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值