1

I have a list fragment displaying my custom list items which consist of a relative layout containing images and text etc.

<?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>

I am trying to set up an OnItemClickListener so when the user clicks anywhere on the list item, a new Intent is started.

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

            }
    });
}

I am attempting this as shown above but can't seem to get the OnItemClickListener to work (just testing with Toast instead of Intent for now). Where am I going wrong? Thanks

EDIT: Added custom list adapter code

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

}
user1356791
  • 165
  • 6
  • 16

4 Answers4

1

Try extending ListFragment (instead of just a normal Fragment). Then you can override onListItemClicked:

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

This gives you the list, view, position, and id, so you should be able to do whatever you need.

Nick
  • 6,375
  • 5
  • 36
  • 53
1

Could you try setting the HozizontalScrollView to not get focus with the XML attribute

android:focusable="false"
AnthonyK
  • 51
  • 4
0

If you have a TextView inside the row then that will take focus when the row is clicked. This will mean that onListItemClick is not called. You either need to have an OnClickListener on the TextView or remove focus from the TextView.

See ListFragment OnListItemClick not being called

Community
  • 1
  • 1
MungoRae
  • 1,912
  • 1
  • 16
  • 25
0

Try this sample project for costum ListFragment. It is a very good example with a costum ListFragment showing 2 types of views for phone and tablets. http://s000.tinyupload.com/index.php?file_id=09444774931653041243

Courtsey http://jcrazyandroider.blogspot.in/2015/01/download-links.html

Ashish Augustine
  • 1,784
  • 4
  • 20
  • 50