Domanda

I am using an activity that displays several fragments depending on the button clicked. I have a implemented a customlistview in a Fragment but for some reason it won't get selected at all.

It seems like the onItemClicklistener() does not respond at all even though I have set a layout on row selection. Here is the XML for my listview:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" 
        android:descendantFocusability="blocksDescendants"       
        android:clickable="true"

        />

</LinearLayout>

Here is the layout for each row:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" 

    >
    <!-- ListRow Left sied Thumbnail image -->

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="3dip" 

        >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="55dip"
            android:layout_height="60dip"
            android:layout_weight="1"
            android:focusable="false"           
            android:src="@drawable/marker_blog_orange" 
            />
    </LinearLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Location here"
        android:textColor="#10bcc9"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" 
        android:focusable="false"

        />

    <!-- Artist Name -->
    <!-- 
    <TextView
        android:id="@+id/artist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="user here"
        android:textColor="#343434"
        android:textSize="10dip" />
-->

    <!-- Rightend-->

    <TextView
        android:id="@+id/duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/title"
        android:layout_marginRight="5dip"
        android:gravity="right"
        android:text="distance"
        android:textColor="#10bcc9"
        android:textSize="10dip"
        android:textStyle="bold" 
        android:focusable="false"

        />

    <!-- Rightend Arrow -->

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:src="@drawable/arrow" />

    <RatingBar
        android:id="@+id/ratingB1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/thumbnail"
        android:layout_toLeftOf="@+id/imageView1"
        android:numStars="5"
        android:focusable="false"
        android:layout_below="@id/title"/>

</RelativeLayout>

Finally here is the class where I retrive some data from a server and add the onitemclicklistner():

public class CustomizedListView extends Fragment implements OnItemClickListener{
    // All static variables


    static final String KEY_ID = "id";
    static final String KEY_TITLE = "";
    static final String Area ="Area";
    static final String KEY_lat = "";
    static final String KEY_lng = "";
    static final String KEY_THUMB_URL = "thumb_url";
    static final String MetersAway="";

    ListView list;
    LazyAdapter adapter;

    HashMap<String, String> marker;

    Context context;

    private float rtn;
    public String TAG;

        public View inst = null;
        ArrayList<HashMap<String, String>> locationList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        final View rootView = inflater.inflate(R.layout.main, container, false);


        locationList = new ArrayList<HashMap<String, String>>();
        list=(ListView)rootView.findViewById(R.id.list);
        retData();
        list.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
            Toast.makeText(getActivity(), "yoho",Toast.LENGTH_SHORT).show();

                    }
                }); 


        return rootView;
    }   

    public void retData(){

        new Retrieve().execute();//extends asyntask and gets data from my server

    }

}
È stato utile?

Soluzione

in your setOnItemClickListener the OnItemClickListener() is placed inside the fragment.

Do it like this :

public class CustomizedListView extends Fragment implements OnItemClickListener{

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

        //the things you want to declare and call 

        View rootView = inflater.inflate(R.layout.main, container, false);
        list=(ListView)rootView.findViewById(R.id.list);
        list.setOnClickListener(this);

        return rootView;
        }


    @Override
    public void OnClick(View v) {
         Toast.makeText(getActivity(), "yoho",Toast.LENGTH_SHORT).show();
    }

Also read http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:descendantFocusability I am not sure if the android:descendantFocussablity is causing the listView not to gain focus

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top