Android Horizo​​ntal Gridview项目相互重叠

时间:2016-10-07 09:24:16

标签: android gridview overlap

大家好我在水平gridview上制作捕捉图像但是我遇到了一些问题。 gridview项目包含图像,但项目相互重叠我如何在这里做我的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zzbothtime.MainActivity" >

<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_below="@+id/seatLegendLayout" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/linearLayout_gridtableLayout"
            android:layout_width="900dp"
            android:layout_height="100dp"
            android:orientation="horizontal" >

            <GridView
                android:id="@+id/gridView1"
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_margin="4dp"
                android:columnWidth="100dp"
                android:gravity="center"
                android:horizontalSpacing="0dp"
                android:numColumns="auto_fit"
                android:scrollbarAlwaysDrawHorizontalTrack="true"
                android:scrollbarAlwaysDrawVerticalTrack="true"
                android:scrollbars="horizontal"
                android:stretchMode="columnWidth"
                android:verticalSpacing="1dp" >
            </GridView>
        </LinearLayout>
    </FrameLayout>
</HorizontalScrollView>

<Button
    android:id="@+id/btnAdd"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/horizontalScrollView1"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="33dp"
    android:onClick="btnAddOnClick"
    android:text="Ekle" />

MainActivityCode

public class MainActivity extends Activity {

@SuppressWarnings("deprecation")


private ArrayList<MyImage> images;
private ImageAdapter imageAdapter;
private GridView listView;
private Uri mCapturedImageURI;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
private Handler handler;
Dialog dialog ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dialog = new Dialog(this);
    dialog.setContentView(R.layout.popupchoose);
    dialog.setTitle("Alert Dialog View");

        images = new ArrayList<MyImage>();
        // Create the adapter to convert the array to views
        imageAdapter = new ImageAdapter(this, images);
        // Attach the adapter to a ListView
        listView = (GridView) findViewById(R.id.gridView1);
        listView.setAdapter(imageAdapter);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

                  Button btnExit = (Button) dialog.findViewById(R.id.btnExit);
                    btnExit.setOnClickListener(new View.OnClickListener() {
                        @Override public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.findViewById(R.id.btnChoosePath)
                            .setOnClickListener(new View.OnClickListener() {
                                @Override public void onClick(View v) {
                                    activeGallery();
                                }
                            });
                    dialog.findViewById(R.id.btnTakePhoto)
                            .setOnClickListener(new View.OnClickListener() {
                                @Override public void onClick(View v) {
                                    activeTakePhoto();
                                }
                            });

                    // show dialog on screen
                    dialog.show();
            }
        });

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub

                imageAdapter.remove(images.remove(position));

                return false;
            }
        });

}

  public void btnAddOnClick(View view) {
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.popupchoose);
        dialog.setTitle("Alert Dialog View");
        Button btnExit = (Button) dialog.findViewById(R.id.btnExit);
        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.findViewById(R.id.btnChoosePath)
                .setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {
                        activeGallery();
                    }
                });
        dialog.findViewById(R.id.btnTakePhoto)
                .setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {
                        activeTakePhoto();
                    }
                });

        // show dialog on screen
        dialog.show();
    }

  private void activeTakePhoto() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            String fileName = "temp.jpg";
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, fileName);
            mCapturedImageURI = getContentResolver()
                    .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            values);
            takePictureIntent
                    .putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    /**
     * to gallery
     */
    private void activeGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                      startActivityForResult(intent, RESULT_LOAD_IMAGE);
    }


    @Override 
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE &&
                        resultCode == RESULT_OK && null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver()
                            .query(selectedImage, filePathColumn, null, null,
                                    null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    MyImage image = new MyImage();
                    image.setTitle("Test");
                    image.setDescription(" add list view");
                    image.setDatetime(System.currentTimeMillis());
                    image.setPath(picturePath);
                    images.add(image);

                    imageAdapter = new ImageAdapter(this, images);
                    // Attach the adapter to a ListView
                    listView = (GridView) findViewById(R.id.gridView1);

                    listView.setAdapter(imageAdapter);


                //    listView.setSelection(imageAdapter.getCount()-1);

                    imageAdapter.notifyDataSetChanged();
                }
            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE &&
                        resultCode == RESULT_OK) {
                    String[] projection = {MediaStore.Images.Media.DATA};
                    Cursor cursor =
                            managedQuery(mCapturedImageURI, projection, null,
                                    null, null);
                    int column_index_data = cursor.getColumnIndexOrThrow(
                            MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    String picturePath = cursor.getString(column_index_data);
                    MyImage image = new MyImage();
                    image.setTitle("Test");
                    image.setDescription("list view");
                    image.setDatetime(System.currentTimeMillis());
                    image.setPath(picturePath);
                    images.add(image);

                    imageAdapter = new ImageAdapter(this, images);
                    // Attach the adapter to a ListView
                    listView = (GridView) findViewById(R.id.gridView1);

                    listView.setAdapter(imageAdapter);   

                 //   listView.setSelection(imageAdapter.getCount()-1);
                    imageAdapter.notifyDataSetChanged();                   

                }
        }
    }
}

myAdapter

public class ImageAdapter  extends ArrayAdapter<MyImage>{

  private static class ViewHolder {
        ImageView imgIcon;
        TextView description;
    }

public ImageAdapter(Context context, ArrayList<MyImage> images) {
    super(context,0, images);
    // TODO Auto-generated constructor stub
}

  @Override public View getView(int position, View convertView,
            ViewGroup parent) {
        // view lookup cache stored in tag
        ViewHolder viewHolder;
        // Check if an existing view is being reused, otherwise inflate the
        // item view
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_gallery_item, parent, false);
            /*
            viewHolder.description =
                    (TextView) convertView.findViewById(R.id.item_img_infor);
                    */
            viewHolder.imgIcon =(ImageView) convertView.findViewById(R.id.id_index_gallery_item_image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // Get the data item for this position
        MyImage image = getItem(position);

        final int THUMBSIZE = 300;
        //        viewHolder.imgIcon.setImageURI(Uri.fromFile(new File(image
        // .getPath())));
        viewHolder.imgIcon.setImageBitmap(ThumbnailUtils
                .extractThumbnail(BitmapFactory.decodeFile(image.getPath()),
                        THUMBSIZE, THUMBSIZE));      
         viewHolder.imgIcon.setScaleType(ImageView.ScaleType.FIT_XY);

        // Return the completed view to render on screen
        return convertView;
    }

}

MyImageClass

public class MyImage {

 private String title, description, path;
    private Calendar datetime;
    private long datetimeLong;
    protected SimpleDateFormat df = new SimpleDateFormat("MMMM d, yy  h:mm");


    public String getTitle() { return title; }  

    public Calendar getDatetime() { return datetime; }

      public void setDatetime(long datetimeLong) {
        this.datetimeLong = datetimeLong;
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(datetimeLong);
        this.datetime = cal;
    }
    public void setDatetime(Calendar datetime) { this.datetime = datetime; }

    public String getDescription() { return description; }

    public void setTitle(String title) { this.title = title; }

    public long getDatetimeLong() { return datetimeLong; }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setPath(String path) { this.path = path; }

    public String getPath() { return path; }

    @Override public String toString() {
        return "Title:" + title + "   " + df.format(datetime.getTime()) +
                "\nDescription:" + description + "\nPath:" + path;
    }

}

0 个答案:

没有答案