Kul*_*bey 48 android material-design android-recyclerview
我有一个Recycler View,其中包含从内部存储加载的图像.我想点击时突出显示所选项目.我尝试了很多东西,但它没有用.实际上我需要的是当我点击Recycler View中的任何项目时,Item必须进入My ArrayList,它也应该突出显示,当我点击或说取消选择时它必须再次变为正常.这是我的代码:
public class Images extends Fragment {
private List<ImageHolder> imageList;
Cursor imageCursor;
RecyclerView recyclerView;
MyImageAdapter adapter;
ActionButton clickButton;
List<String> listofImages;
List<Integer> pos;
int columnIndex;
StringBuilder stringBuilder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootlayout = inflater.inflate(R.layout.image, container, false);
listofImages=new ArrayList<String>();
pos=new ArrayList<Integer>();
stringBuilder=new StringBuilder();
ContentResolver imageResolver = getActivity().getContentResolver();
Uri imageUri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String projection[]={MediaStore.Images.Thumbnails._ID,MediaStore.Images.Media.TITLE};
imageCursor = getActivity().managedQuery(imageUri, projection, null, null, null);
clickButton= (ActionButton) rootlayout.findViewById(R.id.action_button);
recyclerView = (RecyclerView) rootlayout.findViewById(R.id.recycler_view_image);
adapter = new MyImageAdapter(getActivity(), getImageList());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(),recyclerView,new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
TextView tv= (TextView) view.findViewById(R.id.list_text_all);
int flag=0;
String[] projection = {MediaStore.Images.Media.DATA};
imageCursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null);
columnIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
imageCursor.moveToPosition(position);
// Get image filename
String imagePath = imageCursor.getString(columnIndex);
if (listofImages.contains(imagePath)){
Log.d("Contains Test","Yes");
listofImages.remove(imagePath);
pos.remove(position);
} else {
listofImages.add(imagePath);
pos.add(position);
Log.d("Contains Test","No");
}
String s=listofImages.size()+" "+imagePath;
Log.d("Inserted",s);
}
@Override
public void onLongClick(View view, int position) {}
}));
clickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i=0;i<listofImages.size();i++){
stringBuilder.append(listofImages.get(i)+"\n");
}
Toast.makeText(getActivity(),stringBuilder,Toast.LENGTH_LONG).show();
}
});
return rootlayout;
}
public List<ImageHolder> getImageList() {
imageList=new ArrayList<ImageHolder>();
if(imageCursor!=null && imageCursor.moveToFirst()){
int titleColumn = imageCursor.getColumnIndex
(android.provider.MediaStore.Images.Media.TITLE);
int idColumn = imageCursor.getColumnIndex
(android.provider.MediaStore.Images.Media._ID);
do {
ImageHolder img=new ImageHolder();
img.id=imageCursor.getLong(idColumn);
img.title=imageCursor.getString(titleColumn);
img.iconid= imageCursor.getInt(idColumn);
imageList.add(img);
}
while (imageCursor.moveToNext());
}
return imageList;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的适配器类:
public class MyImageAdapter extends RecyclerView.Adapter<MyImageAdapter.MyViewHolder> {
Context context;
private LayoutInflater inflater;
List<ImageHolder> data= Collections.emptyList();
private ClickListener clickListener;
int width,height;
public MyImageAdapter(Context context, List<ImageHolder> data1) {
inflater = LayoutInflater.from(context);
this.data=data1;
this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.all_row, parent, false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
try{
ImageHolder current=data.get(position);
holder.title.setText(current.title);
Log.d("Imageid:"+current.iconid,"");
Uri IMAGE_URI = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + current.iconid);
Bitmap bitmap = Bitmap.createScaledBitmap(decodeUri(IMAGE_URI), 200, 200, true);
holder.img.setImageBitmap(bitmap);
}
catch(Exception e){}
}
public void deleteRecyclerData(int position){
data.remove(position);
notifyItemRemoved(position);
}
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
context.getContentResolver().openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 100;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(
context.getContentResolver().openInputStream(selectedImage), null, o2);
}
@Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title;
// TextView artist;
ImageView img;
CheckBox checkBox;
public MyViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.list_text_all);
img= (ImageView) itemView.findViewById(R.id.list_image_all);
img.setOnClickListener(this);
}
@Override
public void onClick(View v) {}
}
public interface ClickListener{
public void itemClicked(View view, int position);
}
}
Run Code Online (Sandbox Code Playgroud)
reV*_*rse 87
您可以使用StateListDrawable来实现所需的效果.
例
使用以下内容在目录中创建新的Drawable资源文件drawable
:
selector_row.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Color when the row is selected -->
<item android:drawable="@android:color/darker_gray" android:state_pressed="false" android:state_selected="true" />
<!-- Standard background color -->
<item android:drawable="@android:color/white" android:state_selected="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
现在只需将其StateListDrawable
用作行的布局中的背景RecyclerView
row_recyclerview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_row">
<!-- row content -->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
现在,onClick()
只要调用适配器中的方法,您只需执行以下操作:
// myBackground is the RelativeLayout root of your row
myBackground.setSelected(true);
Run Code Online (Sandbox Code Playgroud)
只要你打电话,行的背景就会有颜色(在这种情况下是darker_gray)myBackground.setSelected(false)
.当然,您应该创建一个SparseBooleanArray,以便知道哪个行被选中,哪个不是,因为滚动时将重复使用这些行.
编辑:记住所选项目SparseBooleanArray
背后的想法是记住所选项目.下面是如何使用它的示例:
public class MyImageAdapter extends RecyclerView.Adapter<MyImageAdapter.MyViewHolder> {
private SparseBooleanArray selectedItems;
// Other stuff [...]
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// Set the selected state of the row depending on the position
holder.myBackground.setSelected(selectedItems.get(position, false));
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
@Override
public void onClick(View v) {
// Save the selected positions to the SparseBooleanArray
if (selectedItems.get(getAdapterPosition(), false)) {
selectedItems.delete(getAdapterPosition());
myBackground.setSelected(false);
}
else {
selectedItems.put(getAdapterPosition(), true);
myBackground.setSelected(true);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
amo*_*the 36
在RecyclerView中没有像ListView和GridView那样的选择器,但你尝试下面对我有用的东西
创建一个drawable选择器,如下所示
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@color/blue" />
</shape>
</item>
<item android:state_pressed="false">
<shape>
<solid android:color="@android:color/transparent" />
</shape>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
然后将此drawable设置为RecyclerView行布局的背景
android:background="@drawable/selector"
Run Code Online (Sandbox Code Playgroud)
Tus*_*uss 20
您可以将其添加到row_item.xml
android:clickable="true"
android:background="?attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:background="?attr/selectableItemBackground"
<!-- row content -->
Run Code Online (Sandbox Code Playgroud)
如果Android版本是Lolipop或更高版本,选择器带有涟漪.其他版本的亮点.希望能帮助到你
归档时间: |
|
查看次数: |
84670 次 |
最近记录: |