Java “动态”复选框添加到HorizontalScrollView中的每个图像视图,并删除选中的图像视图

Java “动态”复选框添加到HorizontalScrollView中的每个图像视图,并删除选中的图像视图,java,android,xml,Java,Android,Xml,我有Horizontalscrollview,并动态添加了ImageView,但只需长按,我希望在每个图像的右上角滚动视图中的每个图像上都出现复选框,并删除选中的图像视图 此外,创建的新图像视图最后显示在scrollview中。如何在HorizontalScrollView中显示首先创建的最新图像视图 及 主要活动: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too

我有Horizontalscrollview,并动态添加了ImageView,但只需长按,我希望在每个图像的右上角滚动视图中的每个图像上都出现复选框,并删除选中的图像视图

此外,创建的新图像视图最后显示在scrollview中。如何在HorizontalScrollView中显示首先创建的最新图像视图

主要活动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <ImageView
            android:id="@+id/bigimageview"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_weight="0.40"
            android:cropToPadding="false"
            android:soundEffectsEnabled="false"
            android:scaleType="fitXY"
            android:layout_marginTop="20dp" />



        <HorizontalScrollView

            android:id="@+id/scrollView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#FFFFFF"
            android:fillViewport="true"
            android:measureAllChildren="false"
            android:scrollbars="none"


            >

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

                <Button
                    android:id="@+id/cApartment"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:padding="15dp"
                    android:layout_gravity="center"
                    android:textColor="#EEEEBB"
                    android:text="Add Image"
                    android:textSize="8dp"/>


                <View
                    android:id="@+id/horizontalview"
                    android:layout_width="1dp"
                    android:layout_height="fill_parent"
                    android:background="#EEEEEE" />



            </LinearLayout>

        </HorizontalScrollView>
    </LinearLayout>

</LinearLayout>

主要活动:

   package com.example.asad.sample;

   import android.content.Intent;
   import android.database.Cursor;
   import android.graphics.BitmapFactory;
   import android.net.Uri;
   import android.provider.MediaStore;
   import android.support.v7.app.AppCompatActivity;
   import android.os.Bundle;
   import android.util.Log;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.widget.Button;
   import android.widget.HorizontalScrollView;
   import android.widget.ImageView;
   import android.widget.LinearLayout;
   import android.view.ViewGroup.LayoutParams;
   import com.google.android.gms.appindexing.Action;
   import com.google.android.gms.appindexing.AppIndex;
   import com.google.android.gms.appindexing.Thing;
   import com.google.android.gms.common.api.GoogleApiClient;

   import static com.example.asad.sample.R.drawable.img1;

   public class MainActivity extends AppCompatActivity {


private  int i=1,j=1;
private static int RESULT_LOAD_IMAGE = 1;
private LinearLayout linearLayout2;
private  String[] arr = new String[10];
int[] ids=new int[15];
private int k=1;
private  int g=1;
private View view;
private ImageView img1;


/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;


public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_main);


    Button CApartment = (Button) findViewById(R.id.cApartment);

    img1 = (ImageView) findViewById(R.id.bigimageview);
    view=(View)findViewById(R.id.horizontalview);
    linearLayout2 = (LinearLayout) findViewById(R.id.insidescroll);
    CApartment.setOnClickListener((new View.OnClickListener() {
        public void onClick(View v) {



            //Calling Validate Function
           Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);

          }
         }
        ));

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        // Now we need to set the GUI ImageView data with data read from the picked file.

        setimage(imagePath);


        // At the end remember to close the cursor or you will end with the RuntimeException!
        cursor.close();
    }
}

public void setimage(String imagePath)
{
    final ImageView imageView = new ImageView(MainActivity.this);
    imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
  //  imageView.setMaxHeight(100);
 //   imageView.setMaxWidth(60);

    imageView.setLayoutParams(new LayoutParams(220, 220));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    ids[i] = g;
    imageView.setId(ids[i]);

    arr[j]=imageView.getDrawable().toString();
    linearLayout2.addView(imageView);

    img1.setScaleType(ImageView.ScaleType.CENTER_CROP);
    img1.setImageDrawable(imageView.getDrawable());


//    Toast.makeText(MainActivity.this,"string"+      ids[i],Toast.LENGTH_SHORT).show();
    k=i;
    i++;
    j++;
    g++;

    imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


    for(int l=1 ; l<=k ;l++) {



        if (v.getId() == ids[l]) {

            ImageView imag = (ImageView) findViewById(ids[l]);
            imag.getDrawable();

            //  Toast.makeText(MainActivity.this, imag.getDrawable().toString(), Toast.LENGTH_SHORT).show();
            if (imag.getDrawable().toString() == (arr[l])) ;
            {
                //  Toast.makeText(MainActivity.this, "string" + arr[l], Toast.LENGTH_SHORT).show();
                //  Toast.makeText(MainActivity.this, "string" + arr[l], Toast.LENGTH_SHORT).show();


                img1.setImageDrawable(imag.getDrawable());

                //       Toast.makeText(MainActivity.this, "ids" + ids[l], Toast.LENGTH_SHORT).show();
                //      Toast.makeText(MainActivity.this, "string" + arr[l], Toast.LENGTH_SHORT).show();
            }

        }
    }



   }
  });



    imageView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            //your stuff

            Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
            return true;
        }
    });

}



/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
public Action getIndexApiAction() {
    Thing object = new Thing.Builder()
            .setName("Main Page") // TODO: Define a title for the content shown.
            // TODO: Make sure this auto-generated URL is correct.
            .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
            .build();
    return new Action.Builder(Action.TYPE_VIEW)
            .setObject(object)
            .setActionStatus(Action.STATUS_TYPE_COMPLETED)
            .build();
}

@Override
public void onStart() {
    super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    AppIndex.AppIndexApi.start(client, getIndexApiAction());
}

@Override
public void onStop() {
    super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    AppIndex.AppIndexApi.end(client, getIndexApiAction());
    client.disconnect();
}
package com.example.asad.sample;
导入android.content.Intent;
导入android.database.Cursor;
导入android.graphics.BitmapFactory;
导入android.net.Uri;
导入android.provider.MediaStore;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.Button;
导入android.widget.HorizontalScrollView;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.view.ViewGroup.LayoutParams;
导入com.google.android.gms.appindexing.Action;
导入com.google.android.gms.AppIndex.AppIndex;
导入com.google.android.gms.appindexing.Thing;
导入com.google.android.gms.common.api.GoogleAppClient;
导入静态com.example.asad.sample.R.drawable.img1;
公共类MainActivity扩展了AppCompatActivity{
私有整数i=1,j=1;
私有静态int结果\加载\图像=1;
私人直线布局直线布局2;
私有字符串[]arr=新字符串[10];
int[]ids=新的int[15];
私有int k=1;
私有int g=1;
私人视野;
私有图像视图img1;
/**
*注意:这是自动生成的,用于实现应用程序索引API。
*看https://g.co/AppIndexing/AndroidStudio 了解更多信息。
*/
私人谷歌客户;
创建公共void(Bundle){
super.onCreate(bundle);
setContentView(R.layout.activity_main);
Button CApartment=(Button)findViewById(R.id.CApartment);
img1=(ImageView)findViewById(R.id.bigimageview);
视图=(视图)findViewById(R.id.horizontalview);
linearLayout2=(LinearLayout)findViewById(R.id.insidescroll);
cappartment.setOnClickListener((新视图.OnClickListener(){
公共void onClick(视图v){
//调用验证函数
意图i=新意图(Intent.ACTION\u PICK、MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
startActivityForResult(i,结果加载图像);
}
}
));
//注意:这是自动生成的,用于实现应用程序索引API。
//看https://g.co/AppIndexing/AndroidStudio 了解更多信息。
client=new GoogleApiClient.Builder(this.addApi(AppIndex.API).build();
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
//这里我们需要检查触发的活动是否是图像库。
//如果是,requestCode将与LOAD_IMAGE_RESULTS值匹配。
//如果resultCode是RESULT_OK,并且有一些数据,我们知道已拾取图像。
if(requestCode==RESULT\u LOAD\u IMAGE&&resultCode==RESULT\u OK&&data!=null){
//让我们读取拾取的图像数据-其URI
Uri pickeImage=data.getData();
//让我们使用内容解析器读取拾取的图像路径
字符串[]文件路径={MediaStore.Images.Media.DATA};
Cursor Cursor=getContentResolver().query(pickeImage,filePath,null,null);
cursor.moveToFirst();
String imagePath=cursor.getString(cursor.getColumnIndex(filePath[0]);
//现在我们需要使用从拾取的文件读取的数据设置GUI ImageView数据。
setimage(imagePath);
//最后请记住关闭光标,否则将以RuntimeException结束!
cursor.close();
}
}
public void setimage(字符串imagePath)
{
最终图像视图=新图像视图(MainActivity.this);
setImageBitmap(BitmapFactory.decodeFile(imagePath));
//imageView.setMaxHeight(100);
//imageView.setMaxWidth(60);
setLayoutParams(新的LayoutParams(220220));
imageView.setScaleType(imageView.ScaleType.CENTER\U裁剪);
ids[i]=g;
setId(id[i]);
arr[j]=imageView.getDrawable().toString();
linearLayout2.添加视图(imageView);
img1.setScaleType(ImageView.ScaleType.CENTER\U裁剪);
setImageDrawable(imageView.getDrawable());
//Toast.makeText(MainActivity.this,“string”+ids[i],Toast.LENGTH_SHORT).show();
k=i;
i++;
j++;
g++;
imageView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){

对于(int l=1;l,您应该使用ListView,而不是使用ScrollView和LinearLayout,在这里您可以很容易地在任何位置添加和删除项目。 这可能对您有所帮助:

检查这里,您是否看到我是如何在列表视图中执行此操作的?