[英]Android ListView add not clickable TextView
I have list view: 我有列表视图:
<ListView android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And I add some items to it: 我在其中添加一些项目:
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < items.length(); ++i) {
try {
list.add(items[i].getString("title"));
} catch (JSONException e) {
e.printStackTrace();
}
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
But now I want to add some TextViews
(at example if(i==5)
) to the list, like a label. 但是现在我想向列表中添加一些
TextViews
(例如if(i==5)
),例如标签。 It should not be clickable. 它不应该是可点击的。 How can I add it?
如何添加?
listview.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3)
{
long pos = arg0.getItemIdAtPosition(arg2);
if(pos != 5)
{
//add textview
}
else
{
//nothing
}
}
});
You should use a custom row layout instead of android.R.layout.simple_list_item_1
, for example a row.xml you have one text view whose visibility is set to gone by default: 您应该使用自定义的行布局,而不是
android.R.layout.simple_list_item_1
,例如,一个row.xml拥有一个文本视图,默认情况下其可见性设置为:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/nonclickable_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/normal_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Put it in res/layout/ folder, then call R.layout.row
instead of android.R.layout.simple_list_item_1
when you create your StableArrayAdapter. 将其放在res / layout /文件夹中,然后在创建StableArrayAdapter时调用
R.layout.row
而不是android.R.layout.simple_list_item_1
。 In the getView() method 在getView()方法中
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent,false);
}
if(int position==5) {
TextView tv = (TextView)convertView.findViewById(R.id.nonclickable_text);
tv.setVisibility(View.VISIBLE);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.