gpt4 book ai didi

java - ListView 项目没有点击

转载 作者:行者123 更新时间:2023-11-30 02:06:10 28 4
gpt4 key购买 nike

我有 ListView fragment 。具有 web-html 链接的 ListViewItem 存在问题。不可点击。当 android:autoLink="web" "android:descendantFocusability="blocksDescendants" 没有帮助时,@drawable/items_selector 不工作并且不调用 onItemClick。并且 this解决方案不起作用。我需要像“视频群聊”这样的 ListView 项目或其他选择了工作项目和工作 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);
}
}
});

Activity 中:

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

但项目未被选中(无填充灰色)

enter image description here

最佳答案

如果你想点击整行,只需实现OnClickListener

如果您想单击整行和行内的链接(我想这就是您想要的):

  • 您必须创建一个自定义行项目
  • 创建一个接口(interface)
  • 将该接口(interface)监听器发送到适配器
  • 检查文本是否包含链接。如果是这样,请使用那个监听器发送你到链接

像这样:

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

在您的适配器上,您为 OnWebLinClick 监听器 添加了另一个参数:

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

检查文本是否包含链接并执行onWebLinkClick(urlOnTextView)并在webView中打开它

关于java - ListView 项目没有点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30632900/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com