gpt4 book ai didi

android - ListView : How to display item selector when rows have clickable widgets?

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:13 25 4
gpt4 key购买 nike

虽然我很确定这个问题已经解决了一千多次,但我已经为此苦苦挣扎了一段时间。我已经查看了 SO 和其他地方的各种其他类似问题,但我无法解决这个问题。

核心问题是:

When I have clickable components in my list items, if I long-click on a row (for displaying the contextual action bar), the selector for the row does not appear. The long click is triggered though - there just isn't any visual feedback that the long click is happening.

请注意,仅当行布局包含可点击项目时才会出现此问题。这是我已经尝试过的事情的快速 list :

  • 在“ListView”上,我已将 drawSelectorOnTop 设置为 true(在 XML 和 getListView() 中都试过了
  • 在“ListView”上,我将 choiceMode 设置为 singleChoice(在 XML 和 getListView() 中都试过了
  • 在“ListView”上,我将 listSelector 设置为各种值 - 透明、白色等(在 XML 和 getListView() 中都尝试过) .
  • 当然,托管列表中各行的 LinearLayout 已将 longClickable 设置为 true。如果没有这个,长点击甚至不会被注册。

无论我做什么,长按列表项都不会显示任何类型的选择器,尽管确实发生了长按并且我确实收到了 OnItemLongClick 回调。

关于可能导致此问题的任何线索?


重现问题的代码:

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:longClickable="true"
android:orientation="horizontal" >

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@android:drawable/btn_star_big_on" />

</LinearLayout>

activity_main.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/configurations" >
</ListView>

</LinearLayout>

主 Activity .java

public class MainActivity extends ListActivity {

private ListView mListView;
private Context mContext;
private RowAdapter mAdapter;
private static final String LOG_TAG = "ListViewLongClick";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
mListView = getListView();
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setSelector(android.R.color.white);
mListView.setDrawSelectorOnTop(true);
mAdapter = new RowAdapter(mContext, R.layout.list_item, R.id.tvTitle,
getResources().getStringArray(R.array.configurations));
mListView.setAdapter(mAdapter);
mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {

Toast.makeText(mContext, "Long-click on item " + position,
Toast.LENGTH_SHORT).show();
return true;
}
});
}

static class RowAdapter extends ArrayAdapter<String> {

private int mResource;
private String[] configs;

public RowAdapter(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
this.mResource = resource;
this.configs = objects;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View viewToReturn = convertView;
if (convertView == null) {
viewToReturn = LayoutInflater.from(getContext()).inflate(
this.mResource, null, false);
}

TextView label = (TextView) viewToReturn.findViewById(R.id.tvTitle);
label.setText(configs[position]);
Button btn = (Button) viewToReturn.findViewById(R.id.btnGo);
btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Log.d(LOG_TAG, "Aha! You clicked on the star button");
}
});
return viewToReturn;
}

}

}

字符串.xml

<resources>

<string name="app_name">ListViewLongClick</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>

<string-array name="configurations">
<item >Phone-Port</item>
<item >Phone-Land</item>
<item >Tab7-Port</item>
<item >Tab7-Land</item>
<item >Tab10-Port</item>
<item >Tab10-Land</item>
</string-array>
</resources>

最佳答案

您应该像这样更改您的 list_items:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:longClickable="true"
android:orientation="horizontal"
android:background="@android:drawable/list_selector_background"
>

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@android:drawable/btn_star_big_on" />

</LinearLayout>

我在你的 LinearLayout 中添加了 android:background="@android:drawable/list_selector_background"

希望这对你有帮助 =)

关于android - ListView : How to display item selector when rows have clickable widgets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450521/

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