首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListFragment OnItemClickListener

ListFragment OnItemClickListener
EN

Stack Overflow用户
提问于 2013-08-01 22:06:01
回答 4查看 1.3K关注 0票数 1

我有一个显示我的自定义列表项的列表片段,它由包含图像和文本等的相对布局组成。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@color/main_background"
    android:paddingLeft="20dp"
    android:paddingTop="10dp"
    android:paddingRight="20dp"
    android:paddingBottom="10dp"
    android:descendantFocusability="blocksDescendants" >  
    <ImageView
        android:id="@+id/source_image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@color/black"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/info" />
   <TextView
       android:id="@+id/item_description"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" 
       android:background="@color/white"
       android:textStyle="bold"
       android:layout_toRightOf="@+id/source_image"
       android:textColor="@color/black"
       android:paddingRight="5dp"
       android:paddingTop="5dp"
       android:paddingBottom="5dp"
       android:paddingLeft="5dp"
       android:textAlignment="center"
       android:text="@string/item_desc" />

       <ImageView
        android:id="@+id/arrow_image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="30dp"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/info"
        android:src="@drawable/arrow_right" />

   <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:layout_centerInParent="true"
        android:background="@color/main_background"
        android:layout_below="@+id/item_description"> 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_gravity="fill_horizontal"
            android:layout_marginRight="20dp"
            android:padding="5dp"
            android:orientation="horizontal" >
            <ImageView
                android:id="@+id/item_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/info"
                android:src="@drawable/chameleon" />
            <ImageView
                android:id="@+id/item_image2"
                android:paddingLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/info"
                android:gravity="center"
                android:src="@drawable/chameleon" />
            <ImageView
                android:id="@+id/item_image3"
                android:paddingLeft="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/info"
                android:gravity="center"
                android:src="@drawable/chameleon" />
        </LinearLayout>
   </HorizontalScrollView>

</RelativeLayout>

我正在尝试设置一个OnItemClickListener,这样当用户单击列表项上的任意位置时,就会启动一个新的意图。

代码语言:javascript
复制
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    //Populate list view with array objects
    adapter = new HomeListItemAdapter(items, getActivity());
    listView = getListView();
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
           @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast toast = Toast.makeText(listView.getContext(), "CLICKED", Toast.LENGTH_SHORT);
                toast.show();

            }
    });
}

我正在尝试这样做,但似乎不能让OnItemClickListener工作(现在只是用Toast测试,而不是Intent )。我哪里错了?谢谢

编辑:添加自定义列表适配器代码

代码语言:javascript
复制
public class HomeListItemAdapter extends BaseAdapter {

    private List<HomeListItem> items;
    private Context context;
    private int numItems = 0;

    public HomeListItemAdapter(final List<HomeListItem> items, Context context) {
        this.items = items;
        this.context = context;
        this.numItems = items.size();
    }

    public int getCount() {
        return numItems;
    }

    public HomeListItem getItem(int position) {
        return items.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the current list item
        final HomeListItem item = items.get(position);
        // Get the layout for the list item
        final RelativeLayout itemLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
        // Set the the item picture(s)
        ImageView itemImage = (ImageView) itemLayout.findViewById(R.id.item_image);
        itemImage.setImageDrawable(item.getItemPic());
        //Set the profile picture / source picture
        ImageView sourceImage = (ImageView) itemLayout.findViewById(R.id.source_image);
        sourceImage.setImageDrawable(item.getSourcePic());


        // Set the text label as defined in our list item
        TextView itemDesc = (TextView) itemLayout.findViewById(R.id.item_description);
        AssetManager am = context.getAssets();
        Typeface font = Typeface.createFromAsset(am, "Raleway-Thin.ttf");  
        itemDesc.setTypeface(font); 
        itemDesc.setText(item.getText());

        return itemLayout;
    }

}
EN

回答 4

Stack Overflow用户

发布于 2013-08-01 22:27:00

尝试扩展ListFragment (而不仅仅是普通的片段)。然后,您可以重写onListItemClicked

代码语言:javascript
复制
    public void onListItemClick(ListView l, View v, int pos, long id)

这将为您提供列表、视图、位置和id,因此您应该能够执行所需的任何操作。

票数 1
EN

Stack Overflow用户

发布于 2013-08-01 22:39:49

您可以尝试将HozizontalScrollView设置为不使用XML属性获得焦点吗

代码语言:javascript
复制
android:focusable="false"
票数 1
EN

Stack Overflow用户

发布于 2013-08-01 22:39:17

如果行中有一个TextView,那么当单击该行时,它将获得焦点。这将意味着不会调用onListItemClick。您需要在TextView上有一个OnClickListener,或者从TextView中删除焦点。

请参阅ListFragment OnListItemClick not being called

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17996343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档