在Horizo​​ntalScrollView中检测LinearLayout的所选项目

时间:2012-10-29 10:35:02

标签: android textview android-linearlayout android-alertdialog horizontalscrollview

我在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 programmaticaly:

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标题?

1 个答案:

答案 0 :(得分:0)

我通过以这种方式更改我将LinearView添加到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;
    }
};

这解决了我的问题。