简体   繁体   English

片段中的可点击列表视图

[英]Clickable listview in fragment

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. 我使listview工作,但我似乎无法弄清楚如何使项目可单击。 I am very new to android programming.. 我是Android编程的新手。

My application is seperated in 4 tabs, this is one of them. 我的应用程序分为4个选项卡,这是其中之一。

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 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. 您需要将OnItemClickListener添加到列表中。

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. 您单击的行在arg2中。

@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);

list.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {
        Intent intent = new Intent(MainActivity.this, nextActivity.class);
        startActivity(intent);
        }
    });

return view;       

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM