在网格开头从左侧添加 GridView 项目 (android)
Adding GridView items from the left in beginning of grid (android)
我想创建一个类似于 Instagram 的带有图库的应用程序,其中将照片添加到图库会创建一个缩略图,该缩略图出现在 GridView 的开头,而不是结尾。
这是我的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bUploadImage"
android:text="Upload Image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="1dp"
android:horizontalSpacing="3dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:rotationY="180"
/>
</LinearLayout>
</RelativeLayout>
图像适配器
public class ImageAdapter extends ArrayAdapter {
private Context mContext;
private ArrayList<Image> items;
public ImageAdapter(Context context, int textViewResourceId, ArrayList<Image> items) {
super(context, textViewResourceId, items);
this.mContext = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(480, 500));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap((items.get(position)).getBitmap());
return imageView;
}
public void notifyDatasetChanged(ArrayList<Image> items) {
this.items = items;
}
}
主要活动
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int RESULT_LOAD_IMAGE = 1;
private ImageAdapter adapter;
private ArrayList<Image> items = new ArrayList<Image>();
Button bUploadImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bUploadImage = (Button)findViewById(R.id.bUploadImage);
bUploadImage.setOnClickListener(this);
GridView gridview = (GridView) findViewById(R.id.gridview);
adapter = new ImageAdapter(this,0,items);
gridview.setAdapter(adapter);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bUploadImage:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
Image i = new Image();
i.setBitmap(bitmap);
items.add(i);
} catch(Exception e){
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
adapter.notifyDatasetChanged(items);
}
}
}
尝试指定它应该去的位置
list.add(0, yourObject);
将当前位于该位置的元素(如果有)和任何后续元素向右移动。
请注意,您的 notifyDatasetChanged
实际上没有通知任何内容。它只是改变了对数组的引用,但 ArrayAdapter
的内部列表没有改变。你只需要调用 adapter.add(bitmap)
它会自己触发 notifyDatasetChanged
。所有工作都已在 ArrayAdapter
实现中完成。
要解决您的问题,请将您的适配器更改为如下所示:
public class ImageAdapter extends ArrayAdapter<Bitmap> {
public ImageAdapter(Context context, int textViewResourceId, ArrayList<Bitmap> items) {
super(context, textViewResourceId, items);
Bitmap bm = BitmapFactory.decodeResource(context.getResources(),
android.R.drawable.btn_plus);
//set btn_plus as first element in array
insert(bm, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(getContext());
imageView.setLayoutParams(new GridView.LayoutParams(480, 500));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(getItem(position));
return imageView;
}
}
还要跟踪您需要订阅 setOnItemClickListener
而不是 setOnClickListener
的 GridView
项目的点击事件:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
return;
}
//Handle click on image
}
});
我想创建一个类似于 Instagram 的带有图库的应用程序,其中将照片添加到图库会创建一个缩略图,该缩略图出现在 GridView 的开头,而不是结尾。
这是我的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bUploadImage"
android:text="Upload Image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="1dp"
android:horizontalSpacing="3dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:rotationY="180"
/>
</LinearLayout>
</RelativeLayout>
图像适配器
public class ImageAdapter extends ArrayAdapter {
private Context mContext;
private ArrayList<Image> items;
public ImageAdapter(Context context, int textViewResourceId, ArrayList<Image> items) {
super(context, textViewResourceId, items);
this.mContext = context;
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(480, 500));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap((items.get(position)).getBitmap());
return imageView;
}
public void notifyDatasetChanged(ArrayList<Image> items) {
this.items = items;
}
}
主要活动
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int RESULT_LOAD_IMAGE = 1;
private ImageAdapter adapter;
private ArrayList<Image> items = new ArrayList<Image>();
Button bUploadImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bUploadImage = (Button)findViewById(R.id.bUploadImage);
bUploadImage.setOnClickListener(this);
GridView gridview = (GridView) findViewById(R.id.gridview);
adapter = new ImageAdapter(this,0,items);
gridview.setAdapter(adapter);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bUploadImage:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
Image i = new Image();
i.setBitmap(bitmap);
items.add(i);
} catch(Exception e){
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
adapter.notifyDatasetChanged(items);
}
}
}
尝试指定它应该去的位置
list.add(0, yourObject);
将当前位于该位置的元素(如果有)和任何后续元素向右移动。
请注意,您的 notifyDatasetChanged
实际上没有通知任何内容。它只是改变了对数组的引用,但 ArrayAdapter
的内部列表没有改变。你只需要调用 adapter.add(bitmap)
它会自己触发 notifyDatasetChanged
。所有工作都已在 ArrayAdapter
实现中完成。
要解决您的问题,请将您的适配器更改为如下所示:
public class ImageAdapter extends ArrayAdapter<Bitmap> {
public ImageAdapter(Context context, int textViewResourceId, ArrayList<Bitmap> items) {
super(context, textViewResourceId, items);
Bitmap bm = BitmapFactory.decodeResource(context.getResources(),
android.R.drawable.btn_plus);
//set btn_plus as first element in array
insert(bm, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(getContext());
imageView.setLayoutParams(new GridView.LayoutParams(480, 500));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(getItem(position));
return imageView;
}
}
还要跟踪您需要订阅 setOnItemClickListener
而不是 setOnClickListener
的 GridView
项目的点击事件:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
return;
}
//Handle click on image
}
});