在片段可点击列表视图 [英] Clickable listview in fragment

查看:118
本文介绍了在片段可点击列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个片段一个简单的列表视图可点击。 (不是listfragment)
所以,当你点击一个项目它去有关项目的新detailpage。

我的列表视图工作,但我似乎无法弄清楚如何获得项目点击。我很新到Android编程..

我的应用程序在4个选项卡分开,这是其中之一。

现在的问题是:如何使项目在ListView(在一个片段)点击?

我的片段类:

 进口android.os.Bundle;
  进口android.support.v4.app.Fragment;
  进口android.view.LayoutInflater;
  进口android.view.View;
  进口android.view.ViewGroup;
  进口android.widget.ArrayAdapter;
  进口android.widget.ListView;  公共类BiblioFragment扩展片段{最终的String []项目=新的String [] {机器人,iPhone,的WindowsMo​​bile
        黑莓,WebOS的,Ubuntu的,Windows7的,最大OS X
   的Linux,OS / 2};@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    查看查看= inflater.inflate(R.layout.fragment_biblio,集装箱,FALSE);    ListView控件列表=(ListView控件)view.findViewById(R.id.listView1);
    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(getActivity(),android.R.layout.simple_list_item_1,项目);
    list.setAdapter(适配器);    返回视图。
}

}

XML

 < ListView控件
    机器人:ID =@ + ID / ListView1的
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真正的>
< /&的ListView GT;


解决方案

您需要将OnItemClickListener添加到列表中。

  list.setOnItemClickListener(新OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
                长ARG3){
            // TODO自动生成方法存根
           Log.v(TAG,CLICKED行数:+ ARG2);
        }    });

您单击的行是ARG2。

I am trying to create a simple clickable listview in a fragment. (not a listfragment) So when you click a item it goes to a new detailpage about that item.

I got the listview working but I cant seem to figure out how to get the items clickable. I am very new to android programming..

My application is seperated in 4 tabs, this is one of them.

The question is: How do I make items in a listview (in a fragment) clickable?

My fragment class:

  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;

  public class BiblioFragment extends Fragment {

final String[] items = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
   "Linux", "OS/2" };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_biblio, container, false);

    ListView list = (ListView)view.findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
    list.setAdapter(adapter);

    return view;       
}

}

XML

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

解决方案

You need to add an OnItemClickListener to your list.

list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
           Log.v("TAG", "CLICKED row number: " + arg2);
        }

    });

The row you clicked is in arg2.

这篇关于在片段可点击列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆