同一 Activity 中的 2 个 ListView

发布于 2024-12-17 18:15:34 字数 278 浏览 0 评论 0原文

如果我在一个 Activity 中有 2 个或更多列表视图,那么如何使用 onclicklistener?我的意思是我如何知道用户点击了其中哪一个?

public void onItemClick(AdapterView parent, View v, int position, long id) {


        }

上面的代码是我使用的,但是当我尝试使用另一个列表视图时,我只是找不到方法来检测单击了哪个列表视图。

有什么想法可以解决这个问题吗?

If I have 2 or more listviews in one activity,then how do I use a onclicklistener? I mean How do I know on which one of them the user click?

public void onItemClick(AdapterView parent, View v, int position, long id) {


        }

The above code is what I used,however when I try to use another listview,I just can't find a way to detect which listview is clicked.

Any ideeas to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

佼人 2024-12-24 18:15:34

在本例中,parent 是 itemClick 源自的 listView。因此,您可以做的是为每个 ListView 保留一个成员变量,并将父级与这些成员进行比较,以查看哪个列表触发了单击。

所以这是一个简单的类来表达我的意思:

public class MyTest extends Activity{
  private ListView list1;
  private ListView list2;

  public void onCreate(Bundle b){
    super.onCreate(b);

    list1 = new ListView();
    list2 = new ListView();  //or findViewById if you declared them in your layout
    //the rest of your creation code here
  }

  public void onItemClick(AdapterView parent, View v, int position, long id) {
      if(list1 == parent){
         //handle list1 click
      }else{
        //handle list 2 click
      }

        }

}

In this case, the parent is the listView from which the itemClick originated. So what you can do is keep a member variable for each ListView and compare the parent to those members to see which list triggered the click.

So here's a simple class with what I mean:

public class MyTest extends Activity{
  private ListView list1;
  private ListView list2;

  public void onCreate(Bundle b){
    super.onCreate(b);

    list1 = new ListView();
    list2 = new ListView();  //or findViewById if you declared them in your layout
    //the rest of your creation code here
  }

  public void onItemClick(AdapterView parent, View v, int position, long id) {
      if(list1 == parent){
         //handle list1 click
      }else{
        //handle list 2 click
      }

        }

}
爺獨霸怡葒院 2024-12-24 18:15:34

有两种方法可以做到这一点。

实现 OnItemClickListener

public class ListViewTest extends Activity implements OnItemClickListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
        if(view ==myListView)1{

        }
        if(view ==myListView){

        }
    }
}

设置您自己的监听器

myListView.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
        // TODO: click on second listview
     }
  });

There are two ways you can do it.

Implement OnItemClickListener

public class ListViewTest extends Activity implements OnItemClickListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
        if(view ==myListView)1{

        }
        if(view ==myListView){

        }
    }
}

Set your own listener

myListView.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
        // TODO: click on second listview
     }
  });
秉烛思 2024-12-24 18:15:34

你可以这样做:

  listView1.setOnClickListener(new OnClickListener() {      
     @Override
     public void onClick(View v) {
        // TODO: click on first listview
     }
  });

  listView2.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
        // TODO: click on second listview
     }
  });

You can do it as this:

  listView1.setOnClickListener(new OnClickListener() {      
     @Override
     public void onClick(View v) {
        // TODO: click on first listview
     }
  });

  listView2.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
        // TODO: click on second listview
     }
  });
寄与心 2024-12-24 18:15:34

它非常简单,
只有一个列表可以充当 ListActivity 下的官方列表,并且此列表(并且只有此列表)应该具有特殊的列表 id (我认为@android:list),因此只需将另一个列表的 id 设置为其他 id 并设置它的 setOnItemClickListener 可以做任何你想做的事情。我目前正在开发一个带有 2 个列表视图和一个附加列表片段的应用程序。

its pretty simple ,
only one list can act as the official list under a ListActivity and this list (and only this list) should have the special list id (@android:list i think) so just set the id of the other list to some other id and set its setOnItemClickListener to do whatever you want. I currently work on an app with 2 listViews and an additional list Fragment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文