0

我在 Horizo​​ntalScrollView 中有 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);


              }

      }

}

它工作正常。我想检测在Horizo​​ntalScrollView 中选择了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 标题?

4

1 回答 1

0

我通过以这种方式将 TextView 添加到 LinearLayout 来解决这个问题:

public void addFiles()
 {

    if(!FileManagerActivity.getFinalAttachFiles().isEmpty())
    {
        TextView tv[] = new TextView[FileManagerActivity.getFinalAttachFiles().size()];
             for (int i = 0; i< FileManagerActivity.getFinalAttachFiles().size();i++)
              {


                    View line = new View(this);
                    line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
                    line.setBackgroundColor(0xAA345556);
                    tv[i] = new TextView(this);
                    tv[i].setId(i);
                    tv[i].setTextColor(Color.BLACK);
                    tv[i].setTextSize(12);
                    tv[i].setCompoundDrawablesWithIntrinsicBounds(
                            0, R.drawable.file_icon, 0, 0);
                    tv[i].setText(FileManagerActivity.getFinalAttachFiles().get(i).getName().toString());
                    tv[i].setOnLongClickListener(onclicklistener);
                    layout.addView(tv[i], 0);
                    layout.addView(line, 1);


              }

      }



}

我将 setOnLongClickListener 添加到我的 TextView 的数组中,其长度等于我的列表长度。我的听众:

OnLongClickListener onclicklistener = new OnLongClickListener() {


    public boolean onLongClick(View arg0) {
        final CharSequence[] items = {"Open", "Delete", "Details"};


        final AlertDialog.Builder builder = new AlertDialog.Builder(NewMessageActivity.this);


        builder.setTitle(((TextView)arg0).getText().toString());
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item == 1)
                    {  
                       FileManagerActivity.getFinalAttachFiles().remove(item);
                       layout.invalidate();
                    }

            }
        });
        AlertDialog alert = builder.create();
        alert.show();
        return false;
    }
};

这解决了我的问题。

于 2012-10-29T13:13:20.733 回答