我在 HorizontalScrollView 中有 LinearLayout,它是水平可滚动的。
<HorizontalScrollView
android:id="@+id/scrollMessageFiles"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:layout_below="@+id/editMessage"
android:orientation="horizontal"
android:weightSum="1.0" >
<LinearLayout
android:id="@+id/panelMessageFiles"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFFFFF"
>
</LinearLayout>
</HorizontalScrollView>
我接下来以编程方式将 TextView 添加到我的 LinearLayout 和 ScrollView 中:
public void addFiles()
{
if(!FileManagerActivity.getFinalAttachFiles().isEmpty())
{
for (File file: FileManagerActivity.getFinalAttachFiles())
{
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
line.setBackgroundColor(0xAA345556);
informationView = new TextView(this);
informationView.setId(102);
informationView.setTextColor(Color.BLACK);
informationView.setTextSize(12);
informationView.setCompoundDrawablesWithIntrinsicBounds(
0, R.drawable.file_icon, 0, 0);
informationView.setText(file.getName().toString());
layout.addView(informationView, 0);
layout.addView(line, 1);
}
}
}
它工作正常。我想检测在HorizontalScrollView 中选择了LinearLayout 的哪个项目。我下一个方法:
layout.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
final CharSequence[] items = {"Open", "Delete", "Details"};
final AlertDialog.Builder builder = new AlertDialog.Builder(NewMessageActivity.this);
int childCount = layout.getChildCount()/2;
for (int i = 0; i < childCount; i++)
{
final View child = layout.getChildAt(i);
if (child instanceof TextView)
builder.setTitle(((TextView)child).getText().toString());
}
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
AlertDialog alert = builder.create();
alert.show();
return false;
}
});
但是 AlertBuilder 的标题设置为列表中的第一项,而不是我选择的那些。如何检测选择了哪个项目并将其名称设置为 AlertDialog 标题?