禁用单击 ListView 中的一个数据适配器
Disabling click for one data adapter in ListView
我正在使用 CWAC MergeAdapter - https://github.com/commonsguy/cwac-merge 在 ListView
中添加两个数据适配器。像这样:
MergeAdapter mergeAdapter = new MergeAdapter();
mergeAdapter.addAdapter(yourFirstAdapter);
mergeAdapter.addAdapter(yourSecondAdapter);
list.setAdapter(mergeAdapter);
我还为 ListView
设置了项目点击监听器。然而,我想要的是只有来自 adapter1 的数据应该是可点击的。我该如何实施。我到目前为止是这样的:
ListView list = (ListView) findViewById(R.id.lv);
RelativeLayout secondAdapterlistRowLayout = (RelativeLayout) findViewById(R.id.secondrow);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long mallId) {
//go to the shops view
if (adapterView.equals(secondAdapterlistRowLayout)){
//do nothing
}
else {
Intent intent = new Intent(getActivity(), ShopActivity.class);
intent.putExtra("MALL_ID", (int) mallId);
startActivity(intent);
}
}
});
XML -- row2 -- adapter2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F0F0"
android:clickable="false"
android:id="@+id/secondrow">
....
</RelativeLayout>
基本上我想这样做:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long mallId) {
if(R.id.secondrow is clicked){ then start a new activity }
}
}
我发现更简单的方法是获取被单击的资源名称,然后将其与行的资源名称(即第二行)进行比较
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long mallId) {
if (getResources().getResourceEntryName(view.getId()).equals("secondrow"){
//then do something
}
//otherwise do nothing
}
}
在适配器视图中,对您的项目使用 setTag() 方法,并在您点击列表视图中的任何项目时识别该标签。
我正在使用 CWAC MergeAdapter - https://github.com/commonsguy/cwac-merge 在 ListView
中添加两个数据适配器。像这样:
MergeAdapter mergeAdapter = new MergeAdapter();
mergeAdapter.addAdapter(yourFirstAdapter);
mergeAdapter.addAdapter(yourSecondAdapter);
list.setAdapter(mergeAdapter);
我还为 ListView
设置了项目点击监听器。然而,我想要的是只有来自 adapter1 的数据应该是可点击的。我该如何实施。我到目前为止是这样的:
ListView list = (ListView) findViewById(R.id.lv);
RelativeLayout secondAdapterlistRowLayout = (RelativeLayout) findViewById(R.id.secondrow);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long mallId) {
//go to the shops view
if (adapterView.equals(secondAdapterlistRowLayout)){
//do nothing
}
else {
Intent intent = new Intent(getActivity(), ShopActivity.class);
intent.putExtra("MALL_ID", (int) mallId);
startActivity(intent);
}
}
});
XML -- row2 -- adapter2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F0F0"
android:clickable="false"
android:id="@+id/secondrow">
....
</RelativeLayout>
基本上我想这样做:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long mallId) {
if(R.id.secondrow is clicked){ then start a new activity }
}
}
我发现更简单的方法是获取被单击的资源名称,然后将其与行的资源名称(即第二行)进行比较
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long mallId) {
if (getResources().getResourceEntryName(view.getId()).equals("secondrow"){
//then do something
}
//otherwise do nothing
}
}
在适配器视图中,对您的项目使用 setTag() 方法,并在您点击列表视图中的任何项目时识别该标签。