GPPSoft
GPPSoft

Reputation: 480

ListView item is not clicking

I have fragment with ListView. A problem with ListViewItem having web-html links. Is not clickable. @drawable/items_selector is not working and onItemClick is not call when android:autoLink="web" "android:descendantFocusability="blocksDescendants" is not helped. And this solution not works. I need listview item like a "hangouts" or other apps whith work item selected and working html links and etc. Please help me. OS version is Android 5.1.1

enter image description here

SIMPLE XML... But not works too.

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:descendantFocusability="blocksDescendants" 
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/items_selector"
  android:orientation="vertical"
  android:padding="2dp">

<TextView
    android:id="@+id/themeText"
    android:autoLink="web"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/layout_margin"
    android:paddingLeft="@dimen/layout_margin"
    android:paddingRight="@dimen/layout_margin"
    android:text="Bla bla bla"/>

</LinearLayout>

Adapter code:

 private class ThemesAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private ArrayList<ForumThemeItem> mThemes;

    public ThemesAdapter(Context context, ArrayList<ForumThemeItem> themes) {
        this.mThemes = themes;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        if (mThemes != null) {
            return mThemes.size();
        }
        return 0;
    }

    public void add(ArrayList<ForumThemeItem> themes){
        mThemes.addAll(themes);
    }

    public ForumThemeItem getItem(int position) {
        return mThemes.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ForumThemeItem mTheme = getItem(position);
        ThemesViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ThemesViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_theme, null);
            convertView.findViewById(R.id.ratingLayout).setVisibility(View.GONE);
            viewHolder.themePart = (TextView)convertView.findViewById(R.id.themePart);
            viewHolder.themeText = (TextView)convertView.findViewById(R.id.themeText);
            viewHolder.themeDate = (TextView)convertView.findViewById(R.id.themeDate);
            viewHolder.themeDateUpdate = (TextView)convertView.findViewById(R.id.themeDateUpdate);
            viewHolder.userNick = (TextView)convertView.findViewById(R.id.userNick);
            viewHolder.vote = (ImageView)convertView.findViewById(R.id.vote);
            viewHolder.viewsCount =(TextView)convertView.findViewById(R.id.viewsCount);
            viewHolder.replyCount =(TextView)convertView.findViewById(R.id.replyCount);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ThemesViewHolder) convertView.getTag();
        }


        viewHolder.vote.setVisibility(mTheme.isVote() ? View.VISIBLE : View.GONE);
        viewHolder.themeText.setText(Html.fromHtml(mTheme.getText()));
        viewHolder.userNick.setText(mTheme.getNick());
        viewHolder.themeDate.setText(mTheme.getCreateDate()+",");
        viewHolder.themeDateUpdate.setText(String.format("обн.: %s",mTheme.getDateUpdate()));
        viewHolder.viewsCount.setText(String.format("%s",mTheme.getViewsCount()));
        viewHolder.replyCount.setText(String.format("%s", mTheme.getRepliesCount()));

        if(mTheme.getPartSubName()==null){
            viewHolder.themePart.setText(mTheme.getPartName());
        }else{
            viewHolder.themePart.setText(String.format("%s (%s)",mTheme.getPartName(),mTheme.getPartSubName()));
        }

        if(!mTheme.isAnonymously()){
            switch (mTheme.getGender()){
                case "f":
                    viewHolder.userNick.setTextColor(getResources().getColor(R.color.woman));
                    break;
                case "m":
                    viewHolder.userNick.setTextColor(getResources().getColor(R.color.man));
                    break;
                case "n":
                    viewHolder.userNick.setTextColor(getResources().getColor(R.color.it));
                    break;
            }
        }else{
            viewHolder.userNick.setTextColor(getResources().getColor(R.color.default_text_color));
        }

        return convertView;
    }

    class ThemesViewHolder {
        TextView themePart;
        TextView themeDate;
        TextView themeDateUpdate;
        TextView themeText;
        TextView userNick;
        ImageView vote;
        TextView viewsCount;
        TextView replyCount;
    }
}

I modify adapter.

In adapter:

  viewHolder.themeText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mTextClickListener!=null){
                    mTextClickListener.onTextClick(position);
                }
            }
        });

In activity:

@Override
public void onTextClick(int position) {
    mThemesList.performItemClick(mThemesList.getAdapter().getView(position, null, null), position, position);
}

But item is not selected(no fill gray)

enter image description here

Upvotes: 1

Views: 125

Answers (2)

hrskrs
hrskrs

Reputation: 4480

If you want to click entire row, just implement OnClickListener.

If you want to click entire row and links inside the row (which i thing this is what you want):

  • You have to create a custom row item
  • Create an interface
  • Send that interface listener to the adapter
  • Check if the text contains link. If so use that listener to send you to the link

something like this:

interface OnWebLinkClick(){
    void onWebLinkClick(String url);
}

on your adapter you add another parameter for the OnWebLinClick Listener:

public ThemesAdapter(Context context, ArrayList<ForumThemeItem> themes, OnWebLinClick mListener){
   this.mListener = mListener;   
} 

Check if the Text contains Link and implement the onWebLinkClick(urlOnTextView) and open it in webView

Upvotes: 1

Zain
Zain

Reputation: 2372

To make the TextView html links clickable do the following

textView.setText(Html.fromHtml("your html text here"));

If this is a ListView item - I suspect you will have to do this in the ListAdapter - getView or equivalent method.

Or you could update your XML and add the following - docs here

android:autoLink="web"

Upvotes: 0

Related Questions