java - ListFragment OnItemClickListener

标签 java android android-listfragment

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

<?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,这样当用户单击列表项上的任意位置时,就会启动一个新的 Intent。

@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 进行测试)。我哪里错了? 谢谢

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

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;
    }

}

最佳答案

尝试扩展 ListFragment(而不仅仅是一个普通的 Fragment)。然后你可以覆盖onListItemClicked:

    public void onListItemClick(ListView l, View v, int pos, long id)

这会为您提供列表、 View 、位置和 ID,因此您应该能够做任何您需要的事情。

关于java - ListFragment OnItemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17996343/

相关文章:

android - 生成 Android.mk 的工具

android - 当 ListFragment 为空时,设置空文本()不显示

android - 无法在Android Fragment中加载简单的ListView

java - 如何区分 kotlin 中绑定(bind)的可调用成员引用和相同类型的函数?

java - 构建路径和源码包有什么区别?

java - 如何建立通用日志框架

java - 涉及标识符 R 的错误(将一个项目嵌入另一个项目时)

java - 如何从短信中过滤/提取代码 (Android)

android - 任务 ':app:compileDebugJavaWithJavac' 执行失败。机器人工作室

android - 处理屏幕旋转上的 fragment 重复(带有示例代码)