ListView项目未单击

时间:2015-06-04 00:09:51

标签: java android listview android-listview listviewitem

我有ListView的片段。 ListViewItem有web-html链接的问题。不可点击。当 android:autoLink =“web” “android:descendantFocusability =”blocksDescendants“没有帮助时,@ drawable / items_selector不起作用且onItemClick不被调用。{{3解决方案不起作用。我需要列表视图项目,如“环聊”或其他应用程序选择的工作项和工作HTML链接等。请帮助我。 操作系统版本为Android 5.1.1

enter image description here

SIMPLE XML ......但也不行。

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

适配器代码:

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

我修改了适配器。

在适配器中:

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

活动:

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

但是未选择项目(没有填充灰色)

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您想点击整行,只需实施OnClickListener

如果你想点击行内的整行和链接(这就是你想要的):

  • 您必须创建custom row item
  • 创建interface
  • interface listener发送至adapter
  • 检查文本是否包含链接。如果是这样,请使用侦听器发送 你链接

类似的东西:

interface OnWebLinkClick(){
    void onWebLinkClick(String url);
}
在适配器上

OnWebLinClick Listener添加另一个参数:

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

检查文字是否包含链接并实施onWebLinkClick(urlOnTextView)并在webView

中打开

答案 1 :(得分:0)

要使TextView html链接可单击,请执行以下操作

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

如果这是一个ListView项目 - 我怀疑你必须在ListAdapter中执行此操作 - getView或等效方法。

或者您可以更新XML并添加以下内容 - 文档here

android:autoLink="web"