如何使列表视图的文本视图可点击
Posted
技术标签:
【中文标题】如何使列表视图的文本视图可点击【英文标题】:how to make textview of the listview clickable 【发布时间】:2017-02-01 04:56:42 【问题描述】:我有一个自定义的listview
包含一些Textviews
。我将列表视图设置为adapter
,如下所示:
BestandTypAdapter bestandTypAdapter = new BestandTypAdapter(getActivity(), R.layout.bestand_type_liste, dataList);
li.setAdapter(bestandTypAdapter);
如下所示,在 bestand_type_liste 布局的代码中,TextView 是可点击的
bestand_type_liste:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal"
android:id="@+id/layout_depots_typ"
android:padding="5dip" >
<TableRow
android:paddingRight="10dip"
android:paddingLeft="10dip"
>
<TextView
android:id="@+id/BESTAND_TYP_NAME"
android:layout_
android:layout_
android:layout_gravity="left"
android:layout_weight="0.50"
android:clickable="true"/> <<=================
在adapter
的getView()
方法中,我使用与名称TexView
关联的onClickListener
,这样当单击名称时,其位置会显示在logCat
上,如下所示:
holder.name.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Log.i("", "position_ : " + pos);
);
问题是,当我点击listview
项目时,什么也没有出现,如何解决这个问题
【问题讨论】:
您不需要将android:clickable="true"
设置为textView
。
只是一个问题:为什么是“holder.name.set...”而不是“myTextView.set...”?
【参考方案1】:
尝试为 TextView 添加这个属性:
android:focusable="false"
【讨论】:
其实问题依然存在【参考方案2】:Array adapter 的例子,这个是主要活动。
public class ListViewMain extends AppCompatActivity
private ArrayList<BrandModel> alBrand;
private ListView list;
private CustomAdapter custAdapter;
private AdapterView.OnItemClickListener messageClickedHandler;
private View vHeader;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setupDefaults();
setupEvents();
public void init()
//list view
list = (ListView) findViewById(R.id.listView);
//Array list
alBrand = new ArrayList<>();
//Custom Adapter
custAdapter = new CustomAdapter(this, R.layout.textlistview, alBrand);
//View
vHeader = getLayoutInflater().inflate(R.layout.listviewheader, null, false);
public void setupDefaults()
addBrandAndAdapter();
public void addBrandAndAdapter()
alBrand.add(new BrandModel(getResources().getString(R.string.And), R.drawable.android));
alBrand.add(new BrandModel(getResources().getString(R.string.Mac), R.drawable.apple));
alBrand.add(new BrandModel(getResources().getString(R.string.Tizen), R.drawable.insta));
alBrand.add(new BrandModel(getResources().getString(R.string.window), R.drawable.windows));
list.addHeaderView(vHeader);
list.setAdapter(custAdapter);
public void setupEvents()
messageClickedHandler = new AdapterView.OnItemClickListener()
public void onItemClick(AdapterView parent, View view, int position, long id)
Toast.makeText(getApplicationContext(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
System.out.println("Selected");
;
list.setOnItemClickListener(messageClickedHandler);
-
阵列适配器
public class CustomAdapter extends ArrayAdapter<BrandModel>
ArrayList<BrandModel> alBrand = new ArrayList<>();
public CustomAdapter(Context context, int tvResId, ArrayList<BrandModel> alObjects)
super(context, tvResId, alObjects);
alBrand = alObjects;
@Override
public int getCount()
return super.getCount();
@Override
public View getView(int position, View convertView, ViewGroup parent)
View add = convertView;
BrandHolder holder = null;
if (add == null)
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
add = inflater.inflate(R.layout.textlistview, null);
holder = new BrandHolder();
holder.tvName = (TextView) add.findViewById(R.id.tvName);
holder.ivImage = (ImageView) add.findViewById(R.id.ivIcon);
add.setTag(holder);
else
holder = (BrandHolder) add.getTag();
holder.tvName.setText(alBrand.get(position).getStrBrdName());
holder.ivImage.setImageResource(alBrand.get(position).getImgBrdLogo());
return add;
static class BrandHolder
TextView tvName;
ImageView ivImage;
【讨论】:
不要放入你的getView方法@user2121,或者请输入完整代码 或者在 getView() 方法中尝试 OnItemClickListener @user2121 我不能用它..不知道为什么..类声明如下:class BestandTypAdapter extends ArrayAdapter以上是关于如何使列表视图的文本视图可点击的主要内容,如果未能解决你的问题,请参考以下文章
如何在我绘制文本的自定义视图中使链接、电话号码可点击(与 textview 中的行为相同)?