简体   繁体   中英

Make unclickable/touchable items in ListView

I have a ListView where I want to make some items unclickable (so no effect is shown by clicking) but some are. Its like a kind of header/section of the listView.

I found out that the method public boolean isEnabled(int position) can be overriden so that the return value false can make it untouchable. So I tried this :

@Override
public View getView(int position, View view, ViewGroup parent) 
{
    ...
        if ( Item should be unclickable)
        {
            clickable = false;
            isEnabled(position);
            ....
        }
        else
        {
            clickable = true;
            isEnabled(position);
            ....
        }
    ...
}


    @Override                                           
    public boolean isEnabled(int position ) {
        return clickable;
    }

But this is not working. It doesn't affect the ListView.

When I only call isEnabled when I want to make my item unclickable then everything in my listView is unclickable ...

xml of listView :

    <ListView
    android:id="@+id/listView_playlists"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:fastScrollEnabled="true"
    android:smoothScrollbar="true"
    android:focusable="false"
    android:drawSelectorOnTop="true"
    android:gravity="top" >

</ListView>

Can somebody help me ?

Edit Code:

@Override
public View getView(int position, View view, ViewGroup parent) {


    View rview = view;
    holder = null;


    if (rview == null)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        rview= inflater.inflate(R.layout.row_playlist, null, true);
        holder = new ViewHolder(rview);
        rview.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) rview.getTag();
    }



    if (AL_playlists.get(position).m_type.equalsIgnoreCase("header"))
    {
        clickable = false;
        isEnabled(position);
        holder.txtTitle.setText(AL_playlists.get(position).get_playlist_name());

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)holder.txtTitle.getLayoutParams();
        params.setMargins(0, 10, 0, 10); //substitute parameters for left, top, right, bottom

        holder.txtTitle.setLayoutParams(params);
        holder.txtTitle.setGravity(Gravity.CENTER);
        holder.imgAlbumart.setVisibility(View.GONE);
        holder.txtArtist.setVisibility(View.INVISIBLE);

    }
    else
    {
        clickable = true;
        isEnabled(position);
        mArtLoader.loadBitmap(AL_playlists.get(position).get_picture_path() ,holder.imgAlbumart);

        holder.txtTitle.setText(AL_playlists.get(position).get_playlist_name());
        holder.txtArtist.setText(String.valueOf(AL_playlists.get(position).get_list_size()));
    }

    return rview;

    }

@Override                                           
public boolean isEnabled(int position ) {

    if (clickable == true)
        return true;
    else
        return false;


}

@Override   
public boolean areAllItemsEnabled ()
{
    return false;
}

Try:

ListView myListView = (ListView)findViewById(R.id.listView);
myListView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> ad, View view, int pos, long arg) {
    if(pos == 3) {
        view.findViewById(R.id.listView).setClickable(false);
    }
}
});

Try it

       @Override
    public View getView(int position, View view, ViewGroup parent) 
    {
        ...

        ...
    }


        @Override                                           
        public boolean isEnabled(int position ) {
            if ( Item should be unclickable)
            {
                return = false;
            }
            else
            {
                return = true;
            }
        }

  @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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